From 16bbd0a2e072bafea1dc254fbbcf86768fb3ea48 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期一, 18 十一月 2024 14:57:18 +0800 Subject: [PATCH] 钉钉推送消息,通过机器人发送到个人 --- src/main/java/com/gs/dingtalk/service/SimpleExample.java | 72 ++++++++++++++++++++++++++++-------- 1 files changed, 56 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/gs/dingtalk/service/SimpleExample.java b/src/main/java/com/gs/dingtalk/service/SimpleExample.java index b8f0fb0..95b6e6b 100644 --- a/src/main/java/com/gs/dingtalk/service/SimpleExample.java +++ b/src/main/java/com/gs/dingtalk/service/SimpleExample.java @@ -1,18 +1,22 @@ package com.gs.dingtalk.service; +import cn.hutool.core.util.StrUtil; import com.aliyun.dingtalkoauth2_1_0.Client; import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest; import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponseBody; import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.Common; +import com.aliyun.teautil.models.RuntimeOptions; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; -import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiV2UserGetbymobileRequest; -import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; import com.dingtalk.api.response.OapiRobotSendResponse; import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; import com.google.gson.Gson; @@ -32,6 +36,8 @@ import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.List; /** @@ -100,27 +106,53 @@ /** * 鍙戦�佹秷鎭� */ - public DingTalkMessage sendMessage(String userIdList, String message) throws Exception { + public DingTalkMessage sendMessage(String userIdListStr, String message) throws Exception { + + if (!StrUtil.isNotEmpty(userIdListStr)) { + return new DingTalkMessage(1, 0L, "null"); + } + + String[] strArray = userIdListStr.split(","); + List<String> userIdList = Arrays.asList(strArray); String accessToken = getAccessToken(); - DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); - OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request(); - request.setAgentId(DataAcquisitionConfiguration.AGENT_ID); - request.setUseridList(userIdList); - request.setToAllUser(false); + com.aliyun.dingtalkrobot_1_0.Client client1 = createClient1(); + BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders(); + batchSendOTOHeaders.xAcsDingtalkAccessToken = accessToken; + BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest() + .setRobotCode("ding7n8fldhylh2rt2l2") + .setUserIds(userIdList) + .setMsgKey("sampleText") + .setMsgParam("{\"content\": \"" + message + "\"}"); - OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); - msg.setMsgtype("text"); - msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text()); - msg.getText().setContent(message); - request.setMsg(msg); + try { + BatchSendOTOResponse batchSendOTOResponse = client1.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions()); - OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken); + BatchSendOTOResponseBody body = batchSendOTOResponse.getBody(); + if (StrUtil.isNotEmpty(body.getProcessQueryKey())) { + return new DingTalkMessage(0, 0L, "null"); + } - Gson gson = new Gson(); + } catch (TeaException err) { + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + System.out.println(err.code); + System.out.println(err.message); - return gson.fromJson(rsp.getBody(), DingTalkMessage.class); + System.out.println(err.accessDeniedDetail.toString()); + } + + } catch (Exception _err) { + TeaException err = new TeaException(_err.getMessage(), _err); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + System.out.println(err.code); + System.out.println(err.message); + } + + } + + return new DingTalkMessage(1, 0L, "null"); + //return gson.fromJson(rsp.getBody(), DingTalkMessage.class); } /** @@ -155,6 +187,13 @@ return null; } + public static com.aliyun.dingtalkrobot_1_0.Client createClient1() throws Exception { + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + return new com.aliyun.dingtalkrobot_1_0.Client(config); + } + /** * 浣跨敤 Token 鍒濆鍖栬处鍙稢lient * @@ -168,6 +207,7 @@ return new Client(config); } + public OapiV2UserGetbymobileResponse getOapiV2UserGetbymobileResponse(SendDingtalk s, String accessToken) { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile"); OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest(); -- Gitblit v1.9.3