From 82298366f5c870afe3db93878470b94cd615667e Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期五, 19 十二月 2025 14:03:57 +0800
Subject: [PATCH] 整合钉钉和企业微信

---
 src/main/java/com/gs/dingtalk/service/SimpleExample.java |   95 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 75 insertions(+), 20 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..f8cab58 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;
 
 
 /**
@@ -55,7 +61,7 @@
         try {
             Long timestamp = System.currentTimeMillis();
             System.out.println(timestamp);
-            String secret = DataAcquisitionConfiguration.SECRET;
+            String secret = DataAcquisitionConfiguration.TALK_APP_SECRET;
             String stringToSign = timestamp + "\n" + secret;
             Mac mac = Mac.getInstance("HmacSHA256");
             mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
@@ -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);
     }
 
     /**
@@ -132,8 +164,8 @@
     public String getAccessToken() throws Exception {
         Client client = createClient();
         GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
-                .setAppKey(DataAcquisitionConfiguration.APP_KEY)
-                .setAppSecret(DataAcquisitionConfiguration.APP_SECRET);
+                .setAppKey(DataAcquisitionConfiguration.TALK_APP_KEY)
+                .setAppSecret(DataAcquisitionConfiguration.TALK_APP_SECRET);
         try {
             GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
             return accessToken.body.getAccessToken();
@@ -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,7 +207,8 @@
         return new Client(config);
     }
 
-    public OapiV2UserGetbymobileResponse getOapiV2UserGetbymobileResponse(SendDingtalk s, String accessToken) {
+
+        public OapiV2UserGetbymobileResponse getOapiV2UserGetbymobileResponse(SendDingtalk s, String accessToken) {
         DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
         OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
         req.setMobile(s.getTelephone());
@@ -182,4 +222,19 @@
         }
         return rsp;
     }
+
+    public OapiV2UserGetbymobileResponse getOapiV2UserGetbymobileResponse(String mobile, String accessToken) {
+        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
+        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
+        req.setMobile(mobile);
+        req.setSupportExclusiveAccountSearch(true);
+        OapiV2UserGetbymobileResponse rsp = null;
+
+        try {
+            rsp = client.execute(req, accessToken);
+        } catch (ApiException e) {
+            throw new RuntimeException(e);
+        }
+        return rsp;
+    }
 }

--
Gitblit v1.9.3