liusheng
2023-05-25 bed24ea3fdeee959b85b699a82d53c5112adb219
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ruoyi.web.controller.smartor;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiUserGetByMobileRequest;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.response.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taobao.api.ApiException;
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class DingTalkService {
 
    /**
     * 获取AccessToken
     *
     * @return AccessToken
     * @throws ApiException
     */
    private static String getAccessToken() throws ApiException {
        DefaultDingTalkClient client =
                new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        //Appkey
        request.setAppkey("dingn8iip5ubj7clrrsv");
        //Appsecret
        request.setAppsecret("qlEK8D3oOVwGPOTiBQIBYTqQVlAfy9S_qQizEQFjJdSScwemWFryg4gbneu-NqWD");
        /*请求方式*/
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    }
 
    public static void sendNotification(String mobile, String park, String alarmCode, String severityDesc) throws ApiException {
        // Log.info("发送钉钉通知");
        String accessToken = getAccessToken();
        if (StringUtils.isBlank(mobile)) {
            return;
        }
        // DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
        DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
 
        OapiUserListidRequest request2 = new OapiUserListidRequest();
        request2.setDeptId(675909059L); // 设置要获取的部门 ID,这里以根部门为例
        // 设置部门 ID,如果获取所有员工手机号,可以设置为根部门的 ID
        OapiUserListidResponse response2 = client3.execute(request2, accessToken);
        String body = response2.getBody();
        // 解析为JSONObject
        JSONObject jsonObject = JSONObject.parseObject(body);
        // 提取出JSONArray
        JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list"));
        // 将JSONArray转为List列表
        String str = JSONObject.toJSONString(jsonArray);
        List<String> list = JSONObject.parseObject(str,List.class);
 
        //电话号码数组
        //   String[] split = mobile.split(",");
//        for (String s : split) {
//            DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
//            OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
//            req.setMobile(s);
//            req.setHttpMethod("GET");
//            OapiUserGetByMobileResponse rsp = client2.execute(req, accessToken);
//            //获取到Urid就是在公司里要发送到那个人的id
//            String urid = rsp.getUserid();
        for (String urid : list) {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
            request.setUseridList(urid);
            request.setAgentId(1779052814L);
            request.setToAllUser(false);
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
            msg.getOa().setMessageUrl("www.baidu.com");
            msg.getOa().setPcMessageUrl("www.baidu.com");
            msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
            msg.getOa().getHead().setText("text");
            msg.getOa().getHead().setBgcolor("FFBBBBBB");
            msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
            msg.getOa().getBody().setContent("msg信息");
            OapiMessageCorpconversationAsyncsendV2Request.Form formPark = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formPark.setKey(null);
            formPark.setValue(null);
            OapiMessageCorpconversationAsyncsendV2Request.Form formAlarmCode = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formAlarmCode.setKey("工工工a ");
            formAlarmCode.setValue(alarmCode);
            OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formSeverityDesc.setKey("林要要要要");
            formSeverityDesc.setValue(severityDesc);
            OapiMessageCorpconversationAsyncsendV2Request.Form formConner = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formConner.setKey("厅在在");
            ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
            objects.add(formPark);
            objects.add(formSeverityDesc);
            objects.add(formAlarmCode);
            objects.add(formConner);
            msg.getOa().getBody().setForm(objects);
            msg.setMsgtype("oa");
            request.setMsg(msg);
            //  log.info("获取发送通知消息体和获取发送通知人完成");
            OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request, accessToken);
            //  log.info("发送消息是否成功"+response.isSuccess());
            System.out.println(response.isSuccess());
            //  log.info("消息任务ID"+response.getTaskId());
            System.out.println(response.getTaskId());
        }
    }
 
    public static void main(String[] args) throws ApiException {
//        String appId = "wx1c5243d2337753f4";
//        String appSecret = "84a2186a0d175e88345267c716516cd3";
//        WeChatService weChatService = new WeChatService(appId, appSecret);
//        weChatService.sendMessageToFollowers("你在说啥呢????");
//        // new com.smartor.controller.BaseSmsaccountController().sendMessageToFollowers("AIJSl-IF6M05NzaXofPYbT5wgSP3bWH7I9OBmvJqsaU","www.baidu.com");
 
        sendNotification("17607179857", "1", "2", "3");
    }
}