فهرست منبع

dingtalk 发送客户端,支持集成

zhzhenqin 3 سال پیش
والد
کامیت
3583e046c1

+ 54 - 34
spring-boot-dingtalk-robot-starter/pom.xml

@@ -6,7 +6,19 @@
 
     <groupId>cn.snowheart</groupId>
     <artifactId>spring-boot-dingtalk-robot-starter</artifactId>
-    <version>1.0.3.RELEASE</version>
+    <version>1.0.4.RELEASE</version>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>8</source>
+                    <target>8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
     <packaging>jar</packaging>
 
     <name>dingtalk-robot</name>
@@ -25,6 +37,12 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
             <version>${spring-boot.version}</version>
+            <exclusions>
+                <exclusion>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                    <groupId>org.springframework.boot</groupId>
+                </exclusion>
+            </exclusions>
             <optional>true</optional>
         </dependency>
         <!-- SpringBoot configuration prompt. -->
@@ -45,7 +63,7 @@
                     <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
                         <artifactId>maven-source-plugin</artifactId>
-                        <version>2.2.1</version>
+                        <version>3.2.1</version>
                         <executions>
                             <execution>
                                 <phase>package</phase>
@@ -55,34 +73,6 @@
                             </execution>
                         </executions>
                     </plugin>
-                    <!-- Javadoc -->
-                    <plugin>
-                        <groupId>org.apache.maven.plugins</groupId>
-                        <artifactId>maven-javadoc-plugin</artifactId>
-                        <version>2.9.1</version>
-                        <executions>
-                            <execution>
-                                <phase>package</phase>
-                                <goals>
-                                    <goal>jar</goal>
-                                </goals>
-                            </execution>
-                        </executions>
-                    </plugin>
-                    <!-- GPG -->
-                    <plugin>
-                        <groupId>org.apache.maven.plugins</groupId>
-                        <artifactId>maven-gpg-plugin</artifactId>
-                        <version>1.5</version>
-                        <executions>
-                            <execution>
-                                <phase>verify</phase>
-                                <goals>
-                                    <goal>sign</goal>
-                                </goals>
-                            </execution>
-                        </executions>
-                    </plugin>
                     <!--Compiler-->
                     <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
@@ -97,11 +87,34 @@
                             <showWarnings>false</showWarnings>
                         </configuration>
                     </plugin>
-                    <!--Release-->
+                    <!-- 生成 sha1 校验文件 -->
                     <plugin>
-                        <groupId>org.apache.maven.plugins</groupId>
-                        <artifactId>maven-release-plugin</artifactId>
-                        <version>2.5.3</version>
+                        <groupId>net.nicoulaj.maven.plugins</groupId>
+                        <artifactId>checksum-maven-plugin</artifactId>
+                        <version>1.11</version>
+                        <executions>
+                            <execution>
+                                <id>checksum-maven-plugin-files</id>
+                                <phase>install</phase>
+                                <goals>
+                                    <goal>files</goal>
+                                </goals>
+                            </execution>
+                        </executions>
+                        <configuration>
+                            <fileSets>
+                                <fileSet>
+                                    <directory>${project.build.directory}</directory>
+                                    <includes>
+                                        <include>*.jar</include>
+                                        <include>*.tar.gz</include>
+                                    </includes>
+                                </fileSet>
+                            </fileSets>
+                            <algorithms>
+                                <algorithm>SHA-1</algorithm>
+                            </algorithms>
+                        </configuration>
                     </plugin>
                 </plugins>
             </build>
@@ -153,5 +166,12 @@
         </developer>
     </developers>
 
+    <distributionManagement>
+        <repository>
+            <id>yiidata-maven-release</id>
+            <name>Nexus Release Repository</name>
+            <url>http://www.yiidata.net/maven/repository/maven-releases/</url>
+        </repository>
+    </distributionManagement>
 
 </project>

+ 34 - 2
spring-boot-dingtalk-robot-starter/src/main/java/cn/snowheart/dingtalk/robot/starter/autoconfiguration/DingTalkRobotAutoConfiguration.java

@@ -1,6 +1,8 @@
 package cn.snowheart.dingtalk.robot.starter.autoconfiguration;
 
 import cn.snowheart.dingtalk.robot.starter.client.DingTalkRobotClient;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.AutoConfigureOrder;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.context.annotation.Bean;
@@ -8,14 +10,38 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.web.client.RestTemplate;
 
 /**
- * DingTalk机器人的AutoConfiguration,默认自动化加载一些配置
+ * DingTalk机器人的AutoConfiguration,默认自动化加载一些配置。 这个类需要后加载
  *
  * @author Wanxiang Liu
  * @version 1.0.0
  */
 @Configuration
+@AutoConfigureOrder(Short.MAX_VALUE)
 public class DingTalkRobotAutoConfiguration {
 
+    /**
+     * 钉钉机器人WebHook地址的默认前缀
+     */
+    @Value("${dingtalk.robot.prefix:https://oapi.dingtalk.com/robot/send}")
+    private String urlPrefix;
+    /**
+     * 钉钉机器人WebHook地址的access_token
+     */
+    @Value("${dingtalk.robot.access-token:}")
+    private String accessToken;
+
+    /**
+     * 钉钉机器人是否启用加签,默认false,启用加签需设置secret_token
+     */
+    @Value("${dingtalk.robot.secret.secret-enabled:false}")
+    private boolean secretEnable;
+
+    /**
+     * 钉钉机器人WebHook地址的secret_token,群机器人加签用
+     */
+    @Value("${dingtalk.robot.secret.secret-token:null}")
+    private String secretToken;
+
     /**
      * 创建一个RestTemplate客户端,并注册到Spring容器,供后续程序调用
      * @return RestTemplate客户端
@@ -34,6 +60,12 @@ public class DingTalkRobotAutoConfiguration {
     @ConditionalOnMissingBean
     @ConditionalOnBean(name = "dingTalkRobotRestTemplate")
     public DingTalkRobotClient dingTalkRobotClient() {
-        return new DingTalkRobotClient();
+        final DingTalkRobotClient dingTalkRobotClient = new DingTalkRobotClient();
+        dingTalkRobotClient.setRestTemplate(dingTalkRobotRestTemplate());
+        dingTalkRobotClient.setUrlPrefix(urlPrefix);
+        dingTalkRobotClient.setSecretEnable(secretEnable);
+        dingTalkRobotClient.setAccessToken(accessToken);
+        dingTalkRobotClient.setSecretToken(secretToken);
+        return dingTalkRobotClient;
     }
 }

+ 34 - 14
spring-boot-dingtalk-robot-starter/src/main/java/cn/snowheart/dingtalk/robot/starter/client/DingTalkRobotClient.java

@@ -1,15 +1,19 @@
 package cn.snowheart.dingtalk.robot.starter.client;
 
-import cn.snowheart.dingtalk.robot.starter.entity.*;
+import cn.snowheart.dingtalk.robot.starter.entity.ActionCardButton;
+import cn.snowheart.dingtalk.robot.starter.entity.ActionCardMessage;
+import cn.snowheart.dingtalk.robot.starter.entity.BaseMessage;
+import cn.snowheart.dingtalk.robot.starter.entity.DingTalkResponse;
+import cn.snowheart.dingtalk.robot.starter.entity.FeedCardMessage;
+import cn.snowheart.dingtalk.robot.starter.entity.FeedCardMessageItem;
+import cn.snowheart.dingtalk.robot.starter.entity.LinkMessage;
+import cn.snowheart.dingtalk.robot.starter.entity.MarkdownMessage;
+import cn.snowheart.dingtalk.robot.starter.entity.TextMessage;
 import cn.snowheart.dingtalk.robot.starter.exception.DingTalkException;
 import cn.snowheart.dingtalk.robot.starter.type.HideAvatarType;
 import cn.snowheart.dingtalk.robot.starter.type.ResponseCodeType;
-import org.apache.tomcat.util.codec.binary.Base64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -20,6 +24,8 @@ import javax.crypto.spec.SecretKeySpec;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 import java.util.List;
 import java.util.Map;
 
@@ -33,31 +39,25 @@ import java.util.Map;
 public class DingTalkRobotClient {
     private final static Logger log = LoggerFactory.getLogger(DingTalkRobotClient.class);
 
-    @Autowired
-    @Qualifier("dingTalkRobotRestTemplate")
     private RestTemplate restTemplate;
 
     /**
      * 钉钉机器人WebHook地址的默认前缀
      */
-    @Value("${dingtalk.robot.prefix:https://oapi.dingtalk.com/robot/send}")
     private String urlPrefix;
     /**
      * 钉钉机器人WebHook地址的access_token
      */
-    @Value("${dingtalk.robot.access-token}")
     private String accessToken;
 
     /**
      * 钉钉机器人是否启用加签,默认false,启用加签需设置secret_token
      */
-    @Value("${dingtalk.robot.secret.secret-enabled:false}")
     private boolean secretEnable;
 
     /**
      * 钉钉机器人WebHook地址的secret_token,群机器人加签用
      */
-    @Value("${dingtalk.robot.secret.secret-token:null}")
     private String secretToken;
 
     /**
@@ -93,9 +93,9 @@ public class DingTalkRobotClient {
         try {
             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");
+            mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
+            byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
+            String sign = URLEncoder.encode(new String(Base64.getEncoder().encode(signData)), "UTF-8");
             log.debug("【发送钉钉群消息】获取到签名sign = {}", sign);
             return sign;
         } catch (Exception e) {
@@ -405,4 +405,24 @@ public class DingTalkRobotClient {
     public String getWebhook(String accessToken, String secretToken) {
         return getWebhook(accessToken, secretToken, true);
     }
+
+    public void setRestTemplate(RestTemplate restTemplate) {
+        this.restTemplate = restTemplate;
+    }
+
+    public void setUrlPrefix(String urlPrefix) {
+        this.urlPrefix = urlPrefix;
+    }
+
+    public void setAccessToken(String accessToken) {
+        this.accessToken = accessToken;
+    }
+
+    public void setSecretEnable(boolean secretEnable) {
+        this.secretEnable = secretEnable;
+    }
+
+    public void setSecretToken(String secretToken) {
+        this.secretToken = secretToken;
+    }
 }