企业微信机器人发送消息
利用企业微信机器人发送消息
代码
public static String WEBHOOK_TOKEN = "你的企业微信机器人的token"
public static void callWeChatBot(String content) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
httppost.addHeader("Content-Type", "application/json; charset=utf-8");
//这是发送文字的(发送其他的可以去企业微信api看其他类型的格式)
String textMsg = "{\n" +
" \"msgtype\": \"text\",\n" +
" \"text\": {\n" +
" \"content\": \"" + content + "\",\n" +
" }\n" +
"}";
StringEntity se = new StringEntity(textMsg, "utf-8");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
}
}
备忘
个人备忘使用,下次用到的时候可以来看一下