From 9f7368ceb60b8f5c635cf455914f435d8d782a90 Mon Sep 17 00:00:00 2001 From: 啊鑫 <t2856754968@163.com> Date: 星期六, 21 六月 2025 23:58:03 +0800 Subject: [PATCH] 添加钉钉推送消息功能 --- src/main/java/com/gs/xky/service/SimpleExample.java | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 239 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/gs/xky/service/SimpleExample.java b/src/main/java/com/gs/xky/service/SimpleExample.java new file mode 100644 index 0000000..18f7ee4 --- /dev/null +++ b/src/main/java/com/gs/xky/service/SimpleExample.java @@ -0,0 +1,239 @@ +package com.gs.xky.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.OapiRobotSendRequest; +import com.dingtalk.api.request.OapiV2UserGetbymobileRequest; +import com.dingtalk.api.response.OapiRobotSendResponse; +import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; +import com.google.gson.Gson; +import com.gs.xky.config.DataAcquisitionConfiguration; +import com.gs.xky.dto.DingTalkMessage; +import com.gs.xky.dto.DingTalkResponse; +import com.taobao.api.ApiException; +import lombok.RequiredArgsConstructor; +import org.apache.commons.codec.binary.Base64; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.List; + + +/** + * 鍙戦�佷釜浜烘秷鎭� + * + * @author tjx + * @date 2024/11/17 16:18 + */ +@Service +@RequiredArgsConstructor +@Transactional(rollbackFor = Exception.class) +public class SimpleExample { + + /** + * 缇ら噷鏈哄櫒浜哄彂閫佹秷鎭� + * + * @author tjx + * @date 2024/11/18 08:54 + */ + public DingTalkResponse chatSendMessage(String message) throws Exception { + try { + Long timestamp = System.currentTimeMillis(); + System.out.println(timestamp); + 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")); + byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); + String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); + System.out.println(sign); + + //sign瀛楁鍜宼imestamp瀛楁蹇呴』鎷兼帴鍒拌姹俇RL涓婏紝鍚﹀垯浼氬嚭鐜� 310000 鐨勯敊璇俊鎭� + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign=" + sign + "×tamp=" + timestamp); + OapiRobotSendRequest req = new OapiRobotSendRequest(); + /** + * 鍙戦�佹枃鏈秷鎭� + */ + //瀹氫箟鏂囨湰鍐呭 + OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); + text.setContent(message); + //瀹氫箟 @ 瀵硅薄 + OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); + at.setIsAtAll(true); + //璁剧疆娑堟伅绫诲瀷 + req.setMsgtype("text"); + req.setText(text); + req.setAt(at); + OapiRobotSendResponse rsp = client.execute(req, DataAcquisitionConfiguration.CUSTOM_ROBOT_TOKEN); + + Gson gson = new Gson(); + + return gson.fromJson(rsp.getBody(), DingTalkResponse.class); + } catch (ApiException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } catch (InvalidKeyException e) { + throw new RuntimeException(e); + } + + return null; + } + + /** + * 鍙戦�佹秷鎭� + */ + 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(); + + 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 + "\"}"); + + try { + BatchSendOTOResponse batchSendOTOResponse = client1.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions()); + + BatchSendOTOResponseBody body = batchSendOTOResponse.getBody(); + if (StrUtil.isNotEmpty(body.getProcessQueryKey())) { + return new DingTalkMessage(0, 0L, "null"); + } + + } 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); + + 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); + } + + /** + * 鑾峰彇 AccessToken + * + * @return AccessToken + * @throws Exception + */ + public String getAccessToken() throws Exception { + Client client = createClient(); + GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest() + .setAppKey(DataAcquisitionConfiguration.TALK_APP_KEY) + .setAppSecret(DataAcquisitionConfiguration.TALK_APP_SECRET); + try { + GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest); + return accessToken.body.getAccessToken(); + + } catch (TeaException err) { + if (!Common.empty(err.code) && !Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + System.out.println(err.code); + System.out.println(err.message); + } + } catch (Exception exception) { + TeaException err = new TeaException(exception.getMessage(), exception); + if (!Common.empty(err.code) && !Common.empty(err.message)) { + // err 涓惈鏈� code 鍜� message 灞炴�э紝鍙府鍔╁紑鍙戝畾浣嶉棶棰� + System.out.println(err.code); + System.out.println(err.message); + } + } + 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 + * + * @return Client + * @throws Exception + */ + public Client createClient() throws Exception { + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + 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(); +// req.setMobile(s.getTelephone()); +// req.setSupportExclusiveAccountSearch(true); +// OapiV2UserGetbymobileResponse rsp = null; +// +// try { +// rsp = client.execute(req, accessToken); +// } catch (ApiException e) { +// throw new RuntimeException(e); +// } +// 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