其他分享
首页 > 其他分享> > 企业微信机器人发送消息

企业微信机器人发送消息

作者:互联网

企业微信机器人发送消息

利用企业微信机器人发送消息

代码

 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);
        }
    }

备忘

个人备忘使用,下次用到的时候可以来看一下

标签:content,String,微信,机器人,发送,httppost,response
来源: https://blog.csdn.net/weixin_45037806/article/details/120485969