SpringBoot集成DingTalk钉钉机器人实现消息同步/异步预警推送1.0版本
开发环境说明
<dependency>
<groupId>com.github.answerailgroupId>
<artifactId>dingtalk-spring-boot-starterartifactId>
<version>2.0.0-RELEASEversion>
dependency>
spring:
dingtalk:
token-id: c60d4824e0ba4a30544e81212256789331d68b0085ed1a5b2279715741355fbc
project-id: ${
spring.application.name}
title: 预警通知
secret: APC3eb471b2761851d6ddd1abcndf2d97be21447d8818f1231c5ed61234as52d1w0
# xml方式配置, 注解方式不需要配置
dinger-locations: classpath*:dinger/*.xml
@SpringBootApplication
@MapperScan(basePackages = "com.jaemon.dt.mapper")
// 标识Dinger层扫描路径
@DingerScan(basePackages = "com.jaemon.dt.dinger")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
public interface OrderDinger {
@DingerText(value = "订单号${orderNum}下单成功啦, 下单金额${amt}", phones = {
"13520200906"})
DingTalkResult orderSuccess(
@Parameter("orderNum") String orderNo,
@Parameter("amt") BigDecimal amt
);
@DingerMarkdown(
value = "#### 下单失败啦 @13520200906\n - 订单号: ${orderNo}\n - 标识: ${flag}\n - 数量: ${num}",
title = "下单结果反馈",
phones = {
"13520200906"},
tokenId = @DingerTokenId(
value = "c60d4824e0ba4a30544e81212256789331d68b0085ed1a5b2279715741355fbc",
secret = "APC3eb471b2761851d6ddd1abcndf2d97be21447d8818f1231c5ed61234as52d1w0")
)
DingTalkResult orderFailed(String orderNo, int num, boolean flag);
}
配置文件新增dinger-locations路径配置
spring:
dingtalk:
dinger-locations: classpath*:dinger/*.xml
如: resources/dinger/OrderDinger.xml
OrderDinger.java接口层
public interface OrderDinger {
DingTalkResult orderSuccess(
@Parameter("orderNum") String orderNo,
@Parameter("amt") BigDecimal amt
);
DingTalkResult orderFailed(String orderNo, int num, boolean flag);
}
OrderDinger.xml配置
<dinger namespace="com.jaemon.dt.dinger.OrderDinger">
<message id="orderSuccess">
<body>
<type>texttype>
<content>
订单号${orderNum}下单成功啦, 下单金额${amt}
content>
<phones atAll="true" />
body>
<configuration>
<token-id
secret="APC3eb471b2761851d6ddd1abcndf2d97be21447d8818f1231c5ed61234as52d1w0"
decrypt-key="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeSpqEVuq2NVNDu2lJb"> iH178Wn85rI+Mlguimxml79o5/w/CVGEjVWULfJog
token-id>
<async-execute>trueasync-execute>
configuration>
message>
<message id="orderFailed">
<body>
<type>markdowntype>
<content title="下单结果反馈">
#### 下单失败啦 @13520200906
- 订单号: ${orderNo}
- 标识: ${flag}
- 数量: ${num}
content>
<phones>
<phone value="13520200906" />
phones>
body>
message>
dinger>
XXXDinger.xml 配置模板文档
@Slf4j
@RestController
public class TestController {
@Autowired
private OrderDinger orderDinger;
@GetMapping("/notify")
public void notify() {
DingTalkResult result = orderDinger.orderSuccess("20200906", BigDecimal.valueOf(10000));
log.info(JSON.toJSONString(result));
result = orderDinger.orderFailed("20200906", 10, false);
log.info(JSON.toJSONString(result));
}
}
钉钉收到的orderSuccess通知消息
钉钉收到的orderFailed通知消息