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字段和timestamp字段必须拼接到请求URL上,否则会出现 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 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 初始化账号Client * * @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; } }