开发前准备:添加自建应用
登录企业微信管理端 -> 应用与小程序 -> 应用 -> 自建,点击“创建应用”,设置应用logo、应用名称等信息,创建应用。
创建完成后,在管理端的应用列表里进入该应用,可以看到agentid、secret等信息,这些信息在使用企业微信API时会用到。
创建完成后,该应用会自动出现在可见范围内的成员的企业微信终端上(包括手机端、pc端、微工作台)
消息推送分为两步:
1获取access_token
2.构造消息体,发送消息
话不多说,直接放代码:
public class SendWeChatMessage {
private static String corpid = "你自己的corpid";
private static String corpsecret = "你自己的corpsecret";
// 获取访问权限码(access_token)URL GET请求
private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORPID&corpsecret=CORPSECRET";
// 发送消息URL POST请求
private final static String CREATE_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
// 获取企业微信用户userid POST请求
private final static String GET_USER_ID = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=";
//1.获取access_token
public static String getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
//官方获取access_token的url
String url = ACCESS_TOKEN_URL.replaceAll("CORPID",corpid).replaceAll("CORPSECRET", corpsecret );
JSONObject jsonObject = restTemplate.getForObject(url, JSONObject.class);
String accessToken = jsonObject.getString("access_token");
return accessToken;
}
//获取用户的id
public static String getUserId(String employeePhone) {
//获取access_token
String ACCESS_TOKEN = getAccessToken();
StringBuffer sb = new StringBuffer();
sb.append("{");
sb.append("\"mobile\":" + "\"" + employeePhone + "\",");
sb.append("}");
String json = sb.toString();
String result = "";
String url = GET_USER_ID + ACCESS_TOKEN;
try {
HttpsURLConnection http = getHttp(url);
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
result = new String(jsonBytes, "UTF-8");
System.out.println("==========result==" + result);
System.out.println("请求返回结果:" + result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject.getString("userid");
}
//2.构造消息体,并发送消息
public static void sendWeChatMessage(String employeePhone, String toparty, String totag, String content) {
//获取access_token
String ACCESS_TOKEN = getAccessToken();
Logger.info("ACCESS_TOKEN=====" + ACCESS_TOKEN);
//获取userId
String touser = getUserId(employeePhone);
StringBuffer sb = new StringBuffer();
//构造消息体
sb.append("{");
sb.append("\"touser\":" + "\"" + touser + "\",");
sb.append("\"toparty\":" + "\"" + toparty + "\",");
sb.append("\"totag\":" + "\"" + totag + "\",");
sb.append("\"msgtype\":" + "\"" + "text" + "\",");
sb.append("\"agentid\":" + "\"" + "1000002" + "\",");
sb.append("\"text\":" + "{");
sb.append("\"content\":" + "\"" + content + "\"},");
sb.append("\"debug\":" + "\"" + "1" + "\"");
sb.append("}");
//请求链接
String action = CREATE_SESSION_URL + ACCESS_TOKEN;
String json = sb.toString();
try {
HttpsURLConnection http = getHttp(action);
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
System.out.println("请求返回结果:" + result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static HttpsURLConnection getHttp(String action) throws Exception {
URL url = null;
HttpsURLConnection http = null;
try {
url = new URL(action);
http = (HttpsURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
http.setDoOutput(true);
http.setDoInput(true);
//连接超时30秒
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
//读取超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
http.connect();
} catch (Exception e) {
e.printStackTrace();
}
return http;
}
}
备注:因为公司用的play,我也第一次用,里面处理的post请求不太好,如果用的主流spring,springboot等,大家可以HttpClient处理,具体的网上去搜吧。