diff --git a/README.md b/README.md index 57aac23..92c3cfb 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,10 @@ Dinger一个是以SpringBoot框架为基础开发的消息发送中间件, 对 com.github.answerail dinger-spring-boot-starter - 1.0.0 + ${dinger.version} ``` +> [dinger.version版本号取值](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki/Dinger-1.1-Upgrade-Log) ### 配置文件配置 **使用钉钉群机器人配置** @@ -89,30 +90,35 @@ public class AppInit implements InitializingBean { } } ``` +更多功能请移步[Github Dinger wiki](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki) OR [Gitee Dinger wiki](https://gitee.com/jaemon/dingtalk-spring-boot-starter/wikis)   ## Documentation, Getting Started and Developer Guides -- [Dinger Wiki](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki) +- [Dinger Wiki-Github](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki) + +- [Dinger Wiki-Gitee](https://gitee.com/jaemon/dingtalk-spring-boot-starter/wikis) +   ## Upgrade Log -- [版本变更日志](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki/Dinger-Upgrade-Log) +- [版本变更日志-Github](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/wiki/Dinger-1.2-Upgrade-Log) + +- [版本变更日志-Gitee](https://gitee.com/jaemon/dingtalk-spring-boot-starter/wikis/Dinger-1.2-Upgrade-Log)   ## Feedback -*** -> 有问题欢迎提Issue -> -> [github issues](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/issues) -> -> [gitee issues](https://gitee.com/jaemon/dingtalk-spring-boot-starter/issues) +**有任何建议或问题欢迎提Issue~** + +- [Issues-Github](https://github.com/AnswerAIL/dingtalk-spring-boot-starter/issues) + +- [Issues-Gitee](https://gitee.com/jaemon/dingtalk-spring-boot-starter/issues) ***   diff --git a/src/main/java/com/github/jaemon/dinger/config/AutoBeanConfiguration.java b/src/main/java/com/github/jaemon/dinger/config/AutoBeanConfiguration.java new file mode 100644 index 0000000..5a621e1 --- /dev/null +++ b/src/main/java/com/github/jaemon/dinger/config/AutoBeanConfiguration.java @@ -0,0 +1,37 @@ +/* + * Copyright ©2015-2020 Jaemon. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jaemon.dinger.config; + +import com.github.jaemon.dinger.support.client.DingerHttpClient; +import com.github.jaemon.dinger.support.client.DingerHttpTemplate; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 实例化bean配置 + * + * @author Jaemon + * @since 1.0 + */ +@Configuration +@AutoConfigureAfter(DingerHttpClientConfig.class) +public class AutoBeanConfiguration { + @Bean + public DingerHttpClient dingerHttpClient() { + return new DingerHttpTemplate(); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/config/BeanConfiguration.java b/src/main/java/com/github/jaemon/dinger/config/BeanConfiguration.java index 14d3c64..cf30419 100644 --- a/src/main/java/com/github/jaemon/dinger/config/BeanConfiguration.java +++ b/src/main/java/com/github/jaemon/dinger/config/BeanConfiguration.java @@ -19,11 +19,10 @@ import com.github.jaemon.dinger.support.sign.DingerSignAlgorithm; import com.github.jaemon.dinger.multi.MultiDingerAlgorithmInjectRegister; import com.github.jaemon.dinger.support.*; -import com.github.jaemon.dinger.support.client.DingerHttpClient; -import com.github.jaemon.dinger.support.client.DingerHttpTemplate; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import static com.github.jaemon.dinger.constant.DingerConstant.MARKDOWN_MESSAGE; import static com.github.jaemon.dinger.constant.DingerConstant.TEXT_MESSAGE; @@ -35,13 +34,9 @@ * @since 1.0 */ @Configuration +@Import(AutoBeanConfiguration.class) public class BeanConfiguration { - @Bean - public DingerHttpClient dingerHttpClient() { - return new DingerHttpTemplate(); - } - /** * 默认Text消息格式配置 * diff --git a/src/main/java/com/github/jaemon/dinger/config/DingerHttpClientConfig.java b/src/main/java/com/github/jaemon/dinger/config/DingerHttpClientConfig.java index e05eb15..4e823e9 100644 --- a/src/main/java/com/github/jaemon/dinger/config/DingerHttpClientConfig.java +++ b/src/main/java/com/github/jaemon/dinger/config/DingerHttpClientConfig.java @@ -16,7 +16,7 @@ package com.github.jaemon.dinger.config; import com.github.jaemon.dinger.constant.DingerConstant; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.convert.DurationUnit; @@ -40,7 +40,7 @@ @Configuration @ConditionalOnMissingBean(name = DingerConstant.DINGER_REST_TEMPLATE) @ConfigurationProperties(prefix = DINGER_PROPERTIES_PREFIX + "http-client") -@AutoConfigureBefore(BeanConfiguration.class) +@AutoConfigureAfter(BeanConfiguration.class) public class DingerHttpClientConfig { /** diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionHandler.java b/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionHandler.java index a9591b1..d038413 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionHandler.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerDefinitionHandler.java @@ -18,6 +18,7 @@ import com.github.jaemon.dinger.core.annatations.DingerTokenId; import com.github.jaemon.dinger.core.annatations.DingerMarkdown; import com.github.jaemon.dinger.core.annatations.DingerText; +import com.github.jaemon.dinger.core.entity.DingerRequest; import com.github.jaemon.dinger.core.entity.MsgType; import com.github.jaemon.dinger.core.entity.enums.*; import com.github.jaemon.dinger.core.entity.xml.*; @@ -63,9 +64,16 @@ protected static DingerDefinition dingerTextHandler(DingerType dingerType, Dinge dingerDefinition.setDingerType(dingerType); dingerDefinition.setMessageSubType(MessageSubType.TEXT); + DingerRequest request; + + if (dinger.atAll()) { + request = DingerRequest.request(dinger.value(), true); + } else { + request = DingerRequest.request(dinger.value(), Arrays.asList(dinger.phones())); + } + MsgType msgType = dingerDefinition.messageSubType().msgType( - dingerType, dinger.value(), null, Arrays.asList(dinger.phones()), dinger.atAll() - ); + dingerType, request); dingerDefinition.setMessage(msgType); return dingerDefinition; @@ -90,9 +98,8 @@ protected static DingerDefinition dingerMarkdownHandler(DingerType dingerType, D dingerDefinition.setMessageSubType(MessageSubType.MARKDOWN); // markdown not support at all members - MsgType msgType = dingerDefinition.messageSubType().msgType( - dingerType, dinger.value(), dinger.title(), Arrays.asList(dinger.phones()), false - ); + DingerRequest request = DingerRequest.request(dinger.value(), dinger.title(), Arrays.asList(dinger.phones())); + MsgType msgType = dingerDefinition.messageSubType().msgType(dingerType, request); dingerDefinition.setMessage(msgType); return dingerDefinition; @@ -153,8 +160,14 @@ protected static DingerDefinition xmlHandler( String content = contentTag.map(e -> e.getContent()).orElse(""); String title = contentTag.map(e -> e.getTitle()).orElse("Dinger Title"); - MsgType message = dingerDefinitionType.messageSubType().msgType( - dingerDefinitionType.dingerType(), content, title, phones, atAll); + DingerRequest request; + if (atAll) { + request = DingerRequest.request(content, title, true); + } else { + request = DingerRequest.request(content, title, phones); + } + + MsgType message = dingerDefinitionType.messageSubType().msgType(dingerDefinitionType.dingerType(), request); dingerDefinition.setMessage(message); return dingerDefinition; diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerManagerBuilder.java b/src/main/java/com/github/jaemon/dinger/core/DingerManagerBuilder.java index 080aa87..7d75e6e 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerManagerBuilder.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerManagerBuilder.java @@ -15,15 +15,15 @@ */ package com.github.jaemon.dinger.core; -import com.github.jaemon.dinger.core.entity.enums.MessageSubType; +import com.github.jaemon.dinger.support.client.DingerHttpClient; import com.github.jaemon.dinger.support.sign.DingerSignAlgorithm; import com.github.jaemon.dinger.support.DingerExceptionCallback; import com.github.jaemon.dinger.support.CustomMessage; import com.github.jaemon.dinger.support.DingerAsyncCallback; import com.github.jaemon.dinger.support.DingerIdGenerator; -import com.github.jaemon.dinger.support.client.DingerHttpClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.client.RestTemplate; import java.util.concurrent.Executor; @@ -37,7 +37,8 @@ */ public class DingerManagerBuilder { @Autowired - DingerHttpClient dingerHttpClient; + @Qualifier(DINGER_REST_TEMPLATE) + RestTemplate dingerRestTemplate; @Autowired DingerExceptionCallback dingerExceptionCallback; @Autowired @@ -55,25 +56,27 @@ public class DingerManagerBuilder { Executor dingTalkExecutor; @Autowired DingerAsyncCallback dingerAsyncCallback; + @Autowired + DingerHttpClient dingerHttpClient; public DingerManagerBuilder() { } /** - * custom http client + * 自定义restTemplate客户端 * - * @param dingerHttpClient dingerHttpClient + * @param dingerRestTemplate restTemplate * @return this */ - public DingerManagerBuilder dingerHttpClient(DingerHttpClient dingerHttpClient) { - if (dingerHttpClient != null) { - this.dingerHttpClient = dingerHttpClient; + public DingerManagerBuilder dingerRestTemplate(RestTemplate dingerRestTemplate) { + if (dingerRestTemplate != null) { + this.dingerRestTemplate = dingerRestTemplate; } return this; } /** - * custom exception callback + * 自定义异常回调 * * @param dingerExceptionCallback dingerExceptionCallback * @return this @@ -86,7 +89,15 @@ public DingerManagerBuilder dingerExceptionCallback(DingerExceptionCallback ding } /** - * custom text message format for {@link MessageSubType#TEXT} + * 自定义text文本消息体-仅限手动发送方式 + * + *
+     *     // 该方式为手动发送消息体方式
+     *     dingerSender.send(...);
+     *
+     *     // 该方式为统一管理消息体方式
+     *     userDinger.success(...);
+     * 
* * @param textMessage textMessage * @return this @@ -99,7 +110,15 @@ public DingerManagerBuilder textMessage(CustomMessage textMessage) { } /** - * custom markdown message format for {@link MessageSubType#MARKDOWN} + * 自定义markdown消息体-仅限手动发送方式 + * + *
+     *     // 该方式为手动发送消息体方式
+     *     dingerSender.send(...);
+     *
+     *     // 该方式为统一管理消息体方式
+     *     userDinger.success(...);
+     * 
* * @param markDownMessage markDownMessage * @return this @@ -112,7 +131,7 @@ public DingerManagerBuilder markDownMessage(CustomMessage markDownMessage) { } /** - * custom sign algorithm + * 自定义签名算法,仅限钉钉签名算法更改情况下使用 * * @param dingerSignAlgorithm dingerSignAlgorithm * @return this @@ -125,7 +144,7 @@ public DingerManagerBuilder dingerSignAlgorithm(DingerSignAlgorithm dingerSignAl } /** - * custom id generator + * 自定义DingerId生成器,dingerId为每次调用返回体中的logid值 * * @param dingerIdGenerator dingerIdGenerator * @return this @@ -138,7 +157,7 @@ public DingerManagerBuilder dingerIdGenerator(DingerIdGenerator dingerIdGenerato } /** - * custom async executor + * 自定义异步执行线程池 * * @param dingTalkExecutor dingTalkExecutor * @return this @@ -151,7 +170,7 @@ public DingerManagerBuilder dingTalkExecutor(Executor dingTalkExecutor) { } /** - * custom async callback + * 自定义异步回调函数-用于异步发送时 * * @param dingerAsyncCallback dingerAsyncCallback * @return this diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerRobot.java b/src/main/java/com/github/jaemon/dinger/core/DingerRobot.java index c335da6..d2252e3 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerRobot.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerRobot.java @@ -49,49 +49,25 @@ public DingerRobot(DingerProperties dingerProperties, DingerManagerBuilder dingT @Override public DingerResponse send(MessageSubType messageSubType, DingerRequest request) { - return send(dingerProperties.getDefaultDinger(), messageSubType, request.getTitle(), request.getContent(), request.getPhones(), request.isAtAll()); + return send(dingerProperties.getDefaultDinger(), messageSubType, request); } @Override public DingerResponse send(DingerType dingerType, MessageSubType messageSubType, DingerRequest request) { - return send(dingerType, messageSubType, request.getTitle(), request.getContent(), request.getPhones(), request.isAtAll()); - } - - /** - * 发送预警消息到钉钉-艾特所有人 - * - *
-     *     markdown不支持艾特全部
-     * 
- * - * @param dingerType - * Dinger类型 {@link DingerType} - * @param messageSubType - * 消息类型{@link MessageSubType} - * @param title - * 副标题 - * @param content - * 消息内容 - * @param phones - * 艾特成员 - * @param atAll - * 是否艾特所有人 - * @return - * 响应报文 - * */ - private DingerResponse send(DingerType dingerType, MessageSubType messageSubType, String title, String content, List phones, boolean atAll) { CustomMessage customMessage = customMessage(messageSubType); String msgContent = customMessage.message( - dingerProperties.getProjectId(), title, content, phones + dingerProperties.getProjectId(), request ); + request.setContent(msgContent); MsgType msgType = messageSubType.msgType( - dingerType, msgContent, title, phones, atAll + dingerType, request ); return send(msgType); } + /** * @param message * 消息内容 @@ -121,6 +97,7 @@ protected DingerResponse send(T message) { if (dingerConfig) { dinger = new DingerProperties.Dinger(); BeanUtils.copyProperties(localDinger, dinger); + dinger.setAsync(localDinger.getAsyncExecute()); dinger.setRobotUrl(dingers.get(dingerType).getRobotUrl()); } else { dinger = dingers.get(dingerType); diff --git a/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java b/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java index 0acddeb..4fd802f 100644 --- a/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java +++ b/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java @@ -15,6 +15,7 @@ */ package com.github.jaemon.dinger.core.entity.enums; +import com.github.jaemon.dinger.core.entity.DingerRequest; import com.github.jaemon.dinger.dingtalk.entity.DingMarkDown; import com.github.jaemon.dinger.dingtalk.entity.DingText; import com.github.jaemon.dinger.core.entity.MsgType; @@ -37,7 +38,10 @@ public enum MessageSubType { TEXT { @Override - public MsgType msgType(DingerType dingerType, String content, String title, List phones, boolean atAll) { + public MsgType msgType(DingerType dingerType, DingerRequest request) { + String content = request.getContent(); + boolean atAll = request.isAtAll(); + List phones = request.getPhones(); if (dingerType == DingerType.DINGTALK) { Message message = new DingText(new DingText.Text(content)); @@ -63,7 +67,10 @@ public MsgType msgType(DingerType dingerType, String content, String title, List MARKDOWN { @Override - public MsgType msgType(DingerType dingerType, String content, String title, List phones, boolean atAll) { + public MsgType msgType(DingerType dingerType, DingerRequest request) { + String content = request.getContent(); + String title = request.getTitle(); + List phones = request.getPhones(); if (dingerType == DingerType.DINGTALK) { Message message = new DingMarkDown(new DingMarkDown.MarkDown(title, content)); @@ -90,18 +97,12 @@ public MsgType msgType(DingerType dingerType, String content, String title, List * * @param dingerType * Dinger类型 {@link DingerType} - * @param content - * 消息内容 - * @param title - * 消息标题(dingtalk-markdown) - * @param phones - * 艾特成员列表 - * @param atAll - * 是否艾特全部 + * @param request + * 消息请求体 {@link DingerRequest} * @return * 消息体 {@link MsgType} */ - public abstract MsgType msgType(DingerType dingerType, String content, String title, List phones, boolean atAll); + public abstract MsgType msgType(DingerType dingerType, DingerRequest request); public static boolean contains(String value) { return Arrays.stream(MessageSubType.values()).filter(e -> Objects.equals(e.name(), value)).count() > 0; diff --git a/src/main/java/com/github/jaemon/dinger/support/CustomMessage.java b/src/main/java/com/github/jaemon/dinger/support/CustomMessage.java index 7098bfd..1f4cf0e 100644 --- a/src/main/java/com/github/jaemon/dinger/support/CustomMessage.java +++ b/src/main/java/com/github/jaemon/dinger/support/CustomMessage.java @@ -15,9 +15,7 @@ */ package com.github.jaemon.dinger.support; -import com.github.jaemon.dinger.core.entity.enums.DingerType; - -import java.util.List; +import com.github.jaemon.dinger.core.entity.DingerRequest; /** * 自定义消息接口 @@ -32,15 +30,11 @@ public interface CustomMessage { * * @param projectId * projectId - * @param title - * 标题-{@link DingerType#DINGTALK}Markdown使用 - * @param content - * 内容 - * @param phones - * 艾特电话集 + * @param request + * 消息请求体 {@link DingerRequest} * @return * 消息内容字符串 */ - String message(String projectId, String title, String content, List phones); + String message(String projectId, DingerRequest request); } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java b/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java index ce48587..168df60 100644 --- a/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java +++ b/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java @@ -15,6 +15,8 @@ */ package com.github.jaemon.dinger.support; +import com.github.jaemon.dinger.core.entity.DingerRequest; + import java.text.MessageFormat; import java.util.List; @@ -27,7 +29,10 @@ public class MarkDownMessage implements CustomMessage { @Override - public String message(String projectId, String title, String content, List phones) { + public String message(String projectId, DingerRequest request) { + String content = request.getContent(); + String title = request.getTitle(); + List phones = request.getPhones(); // markdown在text内容里需要有@手机号 StringBuilder text = new StringBuilder(title); if (phones != null && !phones.isEmpty()) { diff --git a/src/main/java/com/github/jaemon/dinger/support/TextMessage.java b/src/main/java/com/github/jaemon/dinger/support/TextMessage.java index 644a257..9b24e26 100644 --- a/src/main/java/com/github/jaemon/dinger/support/TextMessage.java +++ b/src/main/java/com/github/jaemon/dinger/support/TextMessage.java @@ -15,8 +15,9 @@ */ package com.github.jaemon.dinger.support; +import com.github.jaemon.dinger.core.entity.DingerRequest; + import java.text.MessageFormat; -import java.util.List; /** * 默认Text消息格式 @@ -27,7 +28,8 @@ public class TextMessage implements CustomMessage { @Override - public String message(String projectId, String title, String content, List phones) { + public String message(String projectId, DingerRequest request) { + String content = request.getContent(); return MessageFormat.format( "【Dinger通知】 {0}\n- 内容: {1}.", projectId, content);