From 09fcc9929a430d812f57c1ea06e5562b403c9d8c Mon Sep 17 00:00:00 2001
From: 刘小明 <liuxm_a@163.com>
Date: 星期三, 18 九月 2024 09:48:03 +0800
Subject: [PATCH] dingding发送群消息demo
---
pms-parent/pms-global/pom.xml | 5 ++
pms-parent/pms-web-base/src/test/java/com/dy/pmsBase/PmsWebBaseApplicationTests.java | 11 +++++
pms-parent/pms-global/src/main/resources/application-global.yml | 7 +++
pms-parent/pom.xml | 8 ++++
pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DingDingUtils.java | 84 ++++++++++++++++++++++++++++++++++++++++++
5 files changed, 113 insertions(+), 2 deletions(-)
diff --git a/pms-parent/pms-global/pom.xml b/pms-parent/pms-global/pom.xml
index d419493..66bc894 100644
--- a/pms-parent/pms-global/pom.xml
+++ b/pms-parent/pms-global/pom.xml
@@ -21,5 +21,10 @@
<artifactId>pms-common</artifactId>
<version>1.0.0</version>
</dependency>
+ <dependency>
+ <groupId>com.aliyun</groupId>
+ <artifactId>alibaba-dingtalk-service-sdk</artifactId>
+ <version>2.0.0</version>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DingDingUtils.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DingDingUtils.java
new file mode 100644
index 0000000..0d89990
--- /dev/null
+++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DingDingUtils.java
@@ -0,0 +1,84 @@
+package com.dy.pmsGlobal.util;
+
+import com.dingtalk.api.DefaultDingTalkClient;
+import com.dingtalk.api.DingTalkClient;
+import com.dingtalk.api.request.OapiRobotSendRequest;
+import com.dingtalk.api.response.OapiRobotSendResponse;
+import com.taobao.api.ApiException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+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;
+
+@Slf4j
+@Component
+public class DingDingUtils {
+
+ @Value("${dingtalk.robot.access-token}")
+ private String custom_robot_token;
+
+// public static final String USER_ID= "<you need @ group user's userId>";
+ @Value("${dingtalk.at-all}")
+ private boolean at_all;
+
+ @Value("${dingtalk.robot.secret}")
+ private String secret;
+
+ @Value("${dingtalk.robot.url}")
+ private String url;
+
+ public OapiRobotSendResponse send(String msg) {
+ try {
+ Long timestamp = System.currentTimeMillis();
+ String sign = sign(timestamp, secret);
+ System.out.println(sign);
+ //sign瀛楁鍜宼imestamp瀛楁蹇呴』鎷兼帴鍒拌姹俇RL涓婏紝鍚﹀垯浼氬嚭鐜� 310000 鐨勯敊璇俊鎭�
+ DingTalkClient client = new DefaultDingTalkClient(url+"?sign="+sign+"×tamp="+timestamp);
+ OapiRobotSendRequest req = new OapiRobotSendRequest();
+ /**
+ * 鍙戦�佹枃鏈秷鎭�
+ */
+ //瀹氫箟鏂囨湰鍐呭
+ OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
+ text.setContent(msg);
+ //瀹氫箟 @ 瀵硅薄
+ OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
+ if(at_all) {
+ at.setIsAtAll(true);
+ }
+ //璁剧疆娑堟伅绫诲瀷
+ req.setMsgtype("text");
+ req.setText(text);
+ req.setAt(at);
+ OapiRobotSendResponse rsp = client.execute(req, custom_robot_token);
+ log.error("閽夐拤鍝嶅簲:"+rsp);
+ return rsp;
+ } 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;
+ }
+
+ private String sign(Long timestamp, String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
+ 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");
+ return sign;
+ }
+
+}
diff --git a/pms-parent/pms-global/src/main/resources/application-global.yml b/pms-parent/pms-global/src/main/resources/application-global.yml
index 690b37c..79deadf 100644
--- a/pms-parent/pms-global/src/main/resources/application-global.yml
+++ b/pms-parent/pms-global/src/main/resources/application-global.yml
@@ -43,7 +43,12 @@
cache-enabled: true #鎵撳紑鍏ㄥ眬缂撳瓨寮�鍏筹紙浜岀骇鐜锛夛紝榛樿鍊兼槸true
#default-enum-type-handler: com.dy.common.mybatis.envm.EnumCodeTypeHandler
-
+dingtalk:
+ robot:
+ url: https://oapi.dingtalk.com/robot/send
+ access-token: 5004482dce6b24d0e1e655b8e4376a6418cfcebce17ff792d0e68cbb63d6eb5b
+ secret: SEC985ad412f35ece68bb239cad88b6fba59b19461dd8d067e80fafa1270d6ab465
+ at-all: true
#涓嶈繘琛寀serToken杩囨护鐨刄RL锛孈ConfigurationProperties瑕佹眰tokennofilter涓墍鏈夊瓧姣嶉兘灏忓啓
tokennofilter:
urls:
diff --git a/pms-parent/pms-web-base/src/test/java/com/dy/pmsBase/PmsWebBaseApplicationTests.java b/pms-parent/pms-web-base/src/test/java/com/dy/pmsBase/PmsWebBaseApplicationTests.java
index 70e2ae5..c170e6a 100644
--- a/pms-parent/pms-web-base/src/test/java/com/dy/pmsBase/PmsWebBaseApplicationTests.java
+++ b/pms-parent/pms-web-base/src/test/java/com/dy/pmsBase/PmsWebBaseApplicationTests.java
@@ -1,13 +1,22 @@
package com.dy.pmsBase;
+import com.dingtalk.api.response.OapiRobotSendResponse;
+import com.dy.pmsGlobal.util.DingDingUtils;
import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PmsWebBaseApplicationTests {
-
+ private DingDingUtils dingDingUtils;
+ @Autowired
+ public void setDingDingUtils(DingDingUtils dingDingUtils) {
+ this.dingDingUtils = dingDingUtils;
+ }
@Test
void contextLoads() {
+ OapiRobotSendResponse rsp = dingDingUtils.send("鍚勪綅,杩欐槸涓�鏉℃祴璇曟秷鎭�");
+ System.out.println(rsp.getErrcode());
}
}
diff --git a/pms-parent/pom.xml b/pms-parent/pom.xml
index 572bc78..902c8ca 100644
--- a/pms-parent/pom.xml
+++ b/pms-parent/pom.xml
@@ -235,6 +235,14 @@
<type>pom</type>
<scope>import</scope>
</dependency>
+ <!--閽夐拤娑堟伅鎺ㄩ��-->
+ <dependency>
+ <groupId>com.aliyun</groupId>
+ <artifactId>alibaba-dingtalk-service-sdk</artifactId>
+ <version>2.0.0</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
<!-- vo po杞崲 -->
<dependency>
<groupId>org.mapstruct</groupId>
--
Gitblit v1.8.0