Helius
2021-01-26 cde91db7a91dbd1406b3eb700880ff8343de7ccb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.matrix.core.tools;
 
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
 
/**
 * 钉钉机器人工具
 * 
 * @author 李广林
 * @email 935090232@qq.com
 * @date 2018年5月9日
 */
public class DingDingRobotUtil {
 
    /**
     * 发送text类型的消息
     * 
     * @author admin
     * @email 935090232@qq.com
     * @date 2018年6月11日
     * @param dingdingRobotToken
     * @param content
     * @param atMobile
     */
    public static void sendText(String dingdingRobotToken, String content, String atMobile) {
        String at = "";
        if (atMobile != null) {
            at = "\"at\":{\"atMobiles\":\"[" + atMobile + "]\"   , \"isAtAll\": false}";
        }
        content = content.replace("\"", "\\\"");
        String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\":\"" + content + "\"}   ," + at + "}";
        sendMsg(dingdingRobotToken, textMsg);
    }
 
    /**
     * 发送链接消息
     * 
     * @author admin
     * @email 935090232@qq.com
     * @date 2018年6月11日
     * @param dingdingRobotToken
     * @param content
     * @param title
     * @param picUrl
     * @param messageUrl
     */
    public static void sendLink(String dingdingRobotToken, String content, String title, String picUrl,
            String messageUrl) {
        content = content.replace("\"", "\\\"");
        String textMsg = "{ \"msgtype\": \"link\", \"link\": {\"text\":\"" + content + "\" , \"title\":\"" + title
                + "\" , \"picUrl\":\"" + picUrl + "\" , \"messageUrl\":\"" + messageUrl + "\"  }   }";
        sendMsg(dingdingRobotToken, textMsg);
    }
 
    private static void sendMsg(String dingdingRobotToken, String content) {
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(dingdingRobotToken);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        StringEntity se = new StringEntity(content, "utf-8");
        httppost.setEntity(se);
        HttpResponse response;
        try {
            response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
 
}