KKSU
2024-04-17 d23645e976981bc9b670eea1d469fe8a36be309c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.xcong.excoin.utils.dingtalk;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author wzy
 * @date 2020-04-17 22:18
 **/
@Slf4j
public class DingTalkUtils {
 
    private static final String SECRET = "SECbc84fd6c2edb9f2f440f9f969981ca310ba553e7c7994cf68cf26e4607cc5943";
 
    private static DingTalkClient initClient() throws Exception {
        String url = "https://oapi.dingtalk.com/robot/send?access_token=54a0d627111f3667f7e98691c15becadb742c4da895e60f6d77392389e49658a";
        Long timestamp = System.currentTimeMillis();
        String sign = generateSign(timestamp);
        url = url + "&timestamp=" + timestamp + "&sign=" + sign;
        return new DefaultDingTalkClient(url);
    }
 
    public static void sendActionCard(int type) {
        log.info("send dingtalk");
 
        try {
            DingTalkClient client = initClient();
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("actionCard");
            OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
            actionCard.setTitle(DingTalkType.getName(type));
            actionCard.setBtnOrientation("1");
            actionCard.setText(DingTalkType.getName(type));
            List<OapiRobotSendRequest.Btns> btns = new ArrayList<>();
            OapiRobotSendRequest.Btns btn1 = new OapiRobotSendRequest.Btns();
            btn1.setTitle("查看详情");
            btn1.setActionURL("http://baidu.com");
            btns.add(btn1);
            actionCard.setBtns(btns);
 
            request.setActionCard(actionCard);
            OapiRobotSendResponse response = client.execute(request);
            log.info(JSONObject.toJSONString(response));
        } catch (Exception e) {
            log.error("#dingtalk send error#", e);
        }
    }
 
    public static void sendMsg(String title, String msg, Long id) {
        try {
            String text = "### {} \n\n {}";
            DingTalkClient client = initClient();
 
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("actionCard");
            OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();
            actionCard.setTitle("异常消息");
            actionCard.setBtnOrientation("0");
            actionCard.setText(StrUtil.format(text, title, msg));
            actionCard.setSingleURL("http://123.60.45.251/common/getExceptionMsg?id=" + id);
            actionCard.setSingleTitle("查询详情");
 
            request.setActionCard(actionCard);
            OapiRobotSendResponse response = client.execute(request);
            log.info(JSONObject.toJSONString(response));
        } catch (Exception e) {
            log.error("#dingtalk send error#", e);
        }
    }
 
 
    private static String generateSign(Long timestamp) throws Exception {
        String stringToToken = timestamp + "\n" + SECRET;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(SECRET.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToToken.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        return sign;
    }
 
    public static void main(String[] args) {
//        sendMsg("测试一下", "内容");
    }
 
}