Browse Source

v1.0 通过readme可以查看版本信息

lifuquan 1 year ago
commit
51923ea380

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+**/target

+ 55 - 0
README.md

@@ -0,0 +1,55 @@
+# PushMessage
+
+使用联通集团能力商店发布的能力向钉钉群推送消息。
+
+## 使用方式
+
+### 引入依赖
+
+```xml
+<dependency>
+    <groupId>com.nokia</groupId>
+    <artifactId>push-message-starter</artifactId>
+    <version>1.0</version>
+</dependency>
+```
+
+### 添加配置
+
+- properties配置
+
+```properties
+pushmessage.enable=true
+pushmessage.url=http://10.244.18.105:8000/api/chinaUnicom/microservice/notice/pushMessage/v1
+pushmessage.appId=ENWaB7YdUD
+pushmessage.appSecret=oz4OgKBaMNwi4LWfLPbhrPbbuCS8T0Rb
+pushmessage.systemId=10000078
+pushmessage.moduleId=20000156
+pushmessage.busiCode=30000111
+# https://oapi.dingtalk.com/robot/send?access_token=b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf
+pushmessage.accessToken=b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf
+pushmessage.prefix=CUC:
+```
+
+- yml配置
+
+```yml
+pushmessage:
+  enable: true
+  url: http://10.244.18.105:8000/api/chinaUnicom/microservice/notice/pushMessage/v1
+  appId: ENWaB7YdUD
+  appSecret: oz4OgKBaMNwi4LWfLPbhrPbbuCS8T0Rb
+  systemId: 10000078
+  moduleId: 20000156
+  busiCode: 30000111
+  # 在钉钉群添加机器人的时候可以得到accessToken和prefix
+  accessToken: b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf
+  prefix: "CUC:"
+```
+
+## 版本更新记录
+
+### v1.0
+
+1. 支持发送text和markdown形式的数据
+2. 添加钉钉机器人时必须采用自定义关键词方式设置安全方式

+ 81 - 0
pom.xml

@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-parent</artifactId>
+        <version>2.6.14</version>
+        <relativePath />
+    </parent>
+
+    <groupId>com.nokia</groupId>
+    <artifactId>push-message-starter</artifactId>
+    <version>1.0</version>
+
+    <packaging>jar</packaging>
+
+    <properties>
+        <!-- 跳过测试代码 -->
+        <skipTests>true</skipTests>
+        <!-- 指定java的版本 -->
+        <java.version>1.8</java.version>
+        <!-- 文件拷贝时的编码 -->
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <!-- 指定maven-compiler-plugin的配置属性开始 -->
+        <!-- 编译时的编码 -->
+        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
+        <!-- 指定编译的版本 -->
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
+        <!-- 指定maven-compiler-plugin的配置属性结束 -->
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
+        <!-- spring-boot-starter -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+        <!-- spring-boot-autoconfigure -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+        </dependency>
+        <!-- spring-boot-configuration-processor -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <!--lombok-->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+
+    <distributionManagement>
+        <repository>
+            <id>nokia</id>
+            <url>http://nokia.tianhaikj.tk:18081/artifactory/maven-local-releases</url>
+        </repository>
+        <snapshotRepository>
+            <id>nokia</id>
+            <url>http://nokia.tianhaikj.tk:18081/artifactory/maven- ocal-snapshots</url>
+        </snapshotRepository>
+    </distributionManagement>
+</project>

+ 27 - 0
src/main/java/com/nokia/pushmessage/config/PushMessageAutoConfiguration.java

@@ -0,0 +1,27 @@
+package com.nokia.pushmessage.config;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import com.nokia.pushmessage.service.PushMessageService;
+
+/**
+ * 自动配置
+ */
+@Configuration
+@EnableConfigurationProperties(PushMessageProperties.class)
+public class PushMessageAutoConfiguration {
+
+    public PushMessageAutoConfiguration() {
+        System.out.println("=====PushMessageAutoConfiguration=====");
+    }
+
+    @Bean
+    @ConditionalOnProperty(name = "pushmessage.enable", havingValue = "true", matchIfMissing = false)
+    public PushMessageService pushMessageService() {
+        System.out.println("======PushMessageAutoConfiguration.pushMessageService=======");
+        return new PushMessageService();
+    }
+}

+ 34 - 0
src/main/java/com/nokia/pushmessage/config/PushMessageProperties.java

@@ -0,0 +1,34 @@
+package com.nokia.pushmessage.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import lombok.Data;
+
+/**
+ * 配置文件中应配置的
+ */
+@Data
+@ConfigurationProperties(prefix = "pushmessage")
+public class PushMessageProperties {
+
+    // api-url,不增加转发时应该时能力商店的url
+    private String url;
+    // 能力商店的appid
+    private String appId;
+    // 能力商店正式环境密钥
+    private String appSecret;
+    // 系统编号
+    private String systemId;
+    // 模块编号
+    private String moduleId;
+    // 业务编码
+    private String busiCode;
+    // 钉钉群机器人accessToken,通过添加webhook的机器人可以查询到,也可使用已添加的机器人
+    private String accessToken;
+    // 钉钉机器人前缀
+    private String prefix;
+
+    public PushMessageProperties() {
+        System.out.println("==================PushMessageProperties====================");
+    }
+}

+ 225 - 0
src/main/java/com/nokia/pushmessage/service/PushMessageService.java

@@ -0,0 +1,225 @@
+package com.nokia.pushmessage.service;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.lang.Nullable;
+import org.springframework.util.DigestUtils;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.nokia.pushmessage.config.PushMessageProperties;
+
+/**
+ * 使用RestTemplate调用能力商店钉钉消息推送服务API接口
+ * 
+ * 此接口实际调用webhook接口的机器人实现
+ * 
+ * markdown方式使用<font color='red'>内容</font>方式修改字体颜色,在pc端钉钉可生效,在安卓端钉钉上不生效。
+ */
+public class PushMessageService {
+
+    @Autowired
+    private PushMessageProperties properties;
+
+    private RestTemplate restTemplate = new RestTemplate();
+
+    private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
+
+    public PushMessageService() {
+        System.out.println("===PushMessageService===");
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     */
+    public void sendTextMessage(String message) {
+        sendTextMessage(message, null, null);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     * @param atList
+     */
+    public void sendTextMessage(String message, @Nullable List<Integer> atList) {
+        sendTextMessage(message, atList, null);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     * @param atAll
+     */
+    public void sendTextMessage(String message, @Nullable Boolean atAll) {
+        sendTextMessage(message, null, atAll);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param messge
+     * @param atList
+     * @param atAll
+     */
+    public void sendTextMessage(String messge, @Nullable List<Integer> atList, @Nullable Boolean atAll) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("MSG_TYPE", "text");
+        Map<String, Object> data = new HashMap<>();
+        map.put("DATA", data);
+        Map<String, Object> text = new HashMap<>();
+        data.put("TEXT", text);
+        messge = messge.startsWith(properties.getPrefix()) ? messge : properties.getPrefix() + messge;
+        text.put("CONTENT", messge);
+        if (atList != null) {
+            data.put("AT_LIST", atList);
+        }
+        if (atAll != null) {
+            data.put("AT_ALL", atAll);
+        }
+        sendMessage(map);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     */
+    public void sendMarkdownMessage(String messge) {
+        sendMarkdownMessage(messge, null, null, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param title
+     */
+    public void sendMarkdownMessage(String messge, String title) {
+        sendMarkdownMessage(messge, title, null, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param atList
+     */
+    public void sendMarkdownMessage(String messge, List<Integer> atList) {
+        sendMarkdownMessage(messge, null, atList, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param atAll
+     */
+    public void sendMarkdownMessage(String messge, @Nullable Boolean atAll) {
+        sendMarkdownMessage(messge, null, null, atAll);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param title
+     * @param atList
+     * @param atAll
+     */
+    public void sendMarkdownMessage(String messge, @Nullable String title, @Nullable List<Integer> atList,
+            @Nullable Boolean atAll) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("MSG_TYPE", "markdown");
+        Map<String, Object> data = new HashMap<>();
+        map.put("DATA", data);
+        Map<String, Object> markdown = new HashMap<>();
+        data.put("MARK_DOWN", markdown);
+        title = title == null || "".equals(title) ? properties.getPrefix() : title;
+        title = title.startsWith(properties.getPrefix()) ? title : properties.getPrefix() + title;
+        markdown.put("TITLE", title);
+        markdown.put("TEXT", messge);
+        if (atList != null) {
+            data.put("AT_LIST", atList);
+        }
+        if (atAll != null) {
+            data.put("AT_ALL", atAll);
+        }
+        sendMessage(map);
+    }
+
+    private void sendMessage(Map<String, Object> reqMap) {
+        JsonNode respon = restTemplate.postForObject(properties.getUrl(), getRequest(reqMap), JsonNode.class);
+        if (respon == null) {
+            throw new RuntimeException("调用结果为空");
+        }
+        JsonNode node = respon.get("UNI_BSS_HEAD");
+        if ("00000".equals(node.get("RESP_CODE").textValue())) {
+            node = respon.get("UNI_BSS_BODY").get("PUSH_MESSAGE_RSP");
+            if (node != null && "0000".equals(node.get("STATUS").textValue())) {
+                node = node.get("RSP");
+                if (node != null && "0000".equals(node.get("RSP_CODE").textValue())) {
+                    // 只有到这里才说明调用成功了
+                    return;
+                }
+            }
+        }
+        throw new RuntimeException(respon.toString());
+    }
+
+    private Map<String, Object> getRequest(Map<String, Object> reqMap) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("UNI_BSS_HEAD", getUniBssHead());
+        map.put("UNI_BSS_BODY", getUniBssBody(reqMap));
+        return map;
+    }
+
+    private Map<String, Object> getUniBssBody(Map<String, Object> reqMap) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("PUSH_MESSAGE_REQ", reqMap);
+        reqMap.put("ACCESS_TOKEN", properties.getAccessToken());
+        reqMap.put("BUSI_CODE", properties.getBusiCode());
+        reqMap.put("MODULE_ID", properties.getModuleId());
+        reqMap.put("SYSTEM_ID", properties.getSystemId());
+        return map;
+    }
+
+    private Map<String, Object> getUniBssHead() {
+        Map<String, Object> map = new HashMap<>();
+        Date date = new Date();
+        String timeStamp = getTimeStamp(date);
+        String transId = getTransId(date);
+        map.put("APP_ID", properties.getAppId());
+        map.put("TIMESTAMP", timeStamp);
+        map.put("TRANS_ID", transId);
+        map.put("TOKEN", getToken(timeStamp, transId));
+        return map;
+    }
+
+    private String getToken(String timeStamp, String transId) {
+        String beforeEncode = "APP_ID" + properties.getAppId() + "TIMESTAMP" + timeStamp + "TRANS_ID" + transId
+                + properties.getAppSecret();
+        return DigestUtils.md5DigestAsHex(beforeEncode.getBytes());
+    }
+
+    private String getTransId(Date date) {
+        return (new SimpleDateFormat("yyyyMMddHHmmssSSS" + getRandomNumber())).format(date);
+    }
+
+    private String getTimeStamp(Date date) {
+        return dateFormat1.format(date);
+    }
+
+    private int getRandomNumber() {
+        return (int) ((Math.random() * 9D + 1D) * 100000D);
+    }
+}

+ 1 - 0
src/main/resources/META-INF/spring.factories

@@ -0,0 +1 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.nokia.pushmessage.config.PushMessageAutoConfiguration