Java代码⽤指定的企业机器⼈发送消息
1.我们通过Java来向某个WebHook地址发送POST请求,并携带我们需要发送的消息
2.代码⽰例
搭建Maven项⽬,在l⽂件⾥引⼊httpclient依赖
1<dependency>
2<groupId>org.apache.httpcomponents</groupId>
3<artifactId>httpclient</artifactId>
4<version>4.5.3</version>
5</dependency>
以下是java代码的实现部分
1package com.demo;
2import org.apache.http.HttpResponse;
3import org.apache.http.HttpStatus;
4import org.apache.http.client.HttpClient;
5import org.apache.hods.HttpPost;
6import org.ity.StringEntity;
7import org.apache.http.impl.client.HttpClients;
8import org.apache.http.util.EntityUtils;
9代码转换
10public class ChatbotSend {
11//企业机器⼈的WebHook地址xxx
12public static String WEBHOOK_TOKEN = "xxx";
13
14public static void main(String args[]) throws Exception {
15        HttpClient httpclient = ateDefault();
16        HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
17        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
18//构建⼀个json格式字符串textMsg,其内容是接收⽅需要的参数和消息内容
19        String textMsg = "{\"msgtype\":\"text\",\"text\":{\"content\":\"你好,我是机器⼈\"},\"at\":{\"atMobiles\":[\"xxx\"],\"isAtAll\":false}}";
20        StringEntity se = new StringEntity(textMsg, "utf-8");
21        httppost.setEntity(se);
22        HttpResponse response = ute(httppost);
23if (StatusLine().getStatusCode() == HttpStatus.SC_OK) {
24            String result = Entity(), "utf-8");
25            System.out.println(result);
26        }
27    }
28 }
执⾏main⽅法即可发送消息

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。