快速上手 SpringBoot 钉钉消息推送

JavaFamily

共 14179字,需浏览 29分钟

 ·

2023-07-10 09:33

 SpringBoot 钉钉通知处理器。

5c1e66fc21d1d69afde3bf768a0e14c1.webp

之前介绍的 基于 Prometheus+Grafana+Alertmanager+飞书通知的智能监控平台 中提到我们有时候会把一些信息推送到工作交流平台----飞书(或钉钉), 上一篇专题介绍一下飞书推送组件快速上手 SpringBoot 飞书消息推送 ---- feishu-notification-spring-boot-starter. 本文将专题介绍钉钉推送组件----dingtalk-notification-spring-boot-starter.

1. 引入依赖

  • Maven Central Release (Maven 中央仓库正式版)

              <dependency>
<groupId>club.javafamily</groupId>
<artifactId>dingtalk-notification-spring-boot-starter</artifactId>
<version>2.3.2-beta.13</version>
</dependency>
  • Maven Central Snapshot (Maven SNAPSHOT 仓库新功能尝鲜)

                 <!-- Snapshot 库需确保 snapshots 是被允许的 -->
<repositories>
<repository>
<id>maven-central</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>club.javafamily</groupId>
<artifactId>dingtalk-notification-spring-boot-starter</artifactId>
<version>2.3.2-SNAPSHOT</version>
</dependency>
</dependencies>

2. 配置

2.1 钉钉通知配置

创建你自己的钉钉 WebHook 机器人, 在 application.yml 中配置通知的 webhook 地址

              javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
enabled: true # 是否开启通知, 用于不同环境下的区分(开发, 测试, 生产), 默认为 true

2.2 抑制策略

当我们需要对通知进行抑制时(如: 通过飞书通知一些接口异常、服务宕机等信息, 有时候并不需要一直推送通知消息), 此时, 就可以通过抑制策略进行通知消息的抑制!

              javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
inhibit:
enabled: on # 默认为 off
ttl: 1h # 代表同一个消息, 1h 只推送一次

通过指定 inhibit 属性进行抑制配置, 目前支持的属性有:

  • enabled: 是否开启抑制

  • ttl: 抑制时效(同样的通知多久发送一次)

通知抑制是通过 javafamily-cache 组件 提供组件服务与配置, 因此, dingtalk-notification-spring-boot-starter 同样支持 JavaFamilyClub/javafamily-cache 组件的全部配置. 如:

              javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
inhibit:
enabled: on
ttl: 3s

cache:
type: caffeine # redis
key-prefix: demo- # 缓存 key 前缀
time-to-live: 20s # 缓存 expire 时间
caffeine: # caffeine 缓存相关配置
max-size: 500
weak-keys: on
soft-values: on
record-stats: on

需要注意, cache.time-to-liveinhibit.ttl 如果都配置, 则 inhibit.ttl 优先级更高(生效).

更多配置请查看 JavaFamilyClub/javafamily-cache

2.3 restTemplate 配置

发送 webhook 请求底层是通过封装的 resttemplate 进行请求, 而 restTemplate 是通过 javafamily-resttemplate-starter 提供组件服务与配置, 因此, dingtalk-notification-spring-boot-starter 天生支持 javafamily-resttemplate-starter 组件的全部配置.

如: 配置代理(支持 http 及 socks 代理)

              javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
http:
proxy:
type: http # type: socks
host: 192.168.56.27
port: 10080

更多 restTemplate 的配置请参考: javafamily-resttemplate-starter

3. 注入 DingTalkNotifyHandler

              @SpringBootTest
public class DingTalkNotifyTests {

@Autowired
private DingTalkNotifyHandler dingTalkNotifyHandler;

4. 创建 Request, 发送通知

  • Text 通知

                 @Test
void testNotifyText() {
final String response = dingTalkNotifyHandler.notify(
DingTalkTextNotifyRequest.of("这是一个测试数据!"));

System.out.println(response);
}


e68de7333557587c3d54b9614d094888.webp


  • At 通知

                 @Test
void testNotifyTextAt() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "异常" : "正常";

String content = "测试数据时次: " + dataTime
+ "\n应收收据个数: " + shouldCount
+ "\n实收数据个数: " + actualCount
+ "\n监控状态: **" + status + "**";

final String response = dingTalkNotifyHandler.notify(
DingTalkTextNotifyRequest.of(content, false, "18829346477"));

System.out.println(response);
}


5dc1224bac1a68f5d5cd2b3d2d1277d5.webp

  • Link通知

                 @Test
void testNotifyLink() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "异常" : "正常";

String content = "数据时次: " + dataTime
+ "\n应收收据个数: " + shouldCount
+ "\n实收数据个数: " + actualCount
+ "\n监控状态: **" + status + "**";

DingTalkLinkRequest request = DingTalkLinkRequest.of("项目更新通知(测试)",
content,
"https://github.com/orgs/JavaFamilyClub/projects/3",
"https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF");

final String response = dingTalkNotifyHandler.notify(request);

System.out.println(response);
}


a5bbdd5c3890fde6eebe4a618e296b0a.webp

  • Markdown 通知

                 @Test
void testNotifyMarkdown() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "异常" : "正常";

String content = "@18829346477 数据时次: " + dataTime
+ "\n应收收据个数: " + shouldCount
+ "\n实收数据个数: " + actualCount
+ "\n监控状态: **" + status + "**";

DingTalkMarkDownRequest request = DingTalkMarkDownRequest.of("项目更新通知(测试)",
content, false, "18829346477");

final String response = dingTalkNotifyHandler.notify(request);

System.out.println(response);
}


4e5ad4f61b80a5182559436c6461ad94.webp

  • 单按钮通知

                 @Test
void testNotifySingleCard() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "异常" : "正常";

String content = "数据时次: " + dataTime
+ "\n应收收据个数: " + shouldCount
+ "\n实收数据个数: " + actualCount
+ "\n监控状态: **" + status + "**";

final DingTalkSingleBtnCardRequest request
= DingTalkSingleBtnCardRequest.of("测试xxx数据监控", content,
"立即前往系统查看",
"https://github.com/orgs/JavaFamilyClub/projects/3");

final String response = dingTalkNotifyHandler.notify(request);

System.out.println(response);
}

cfe69e773e7ec5f50d14eb4ab515e77f.webp

  • 多按钮通知

                 @Test
void testNotifyMultiCard() {
String content = "测试 xxx 点位 (33.3, 107.7) 已经添加至用户点位库, 是否加入资源池?";

final DingTalkMultiBtnCardRequest request
= DingTalkMultiBtnCardRequest.of("点位审核", content,
CardBtn.builder()
.title("接受")
.actionURL("https://github.com/orgs/JavaFamilyClub/projects/3")
.build(),
CardBtn.builder()
.title("拒绝")
.actionURL("https://github.com/JavaFamilyClub/javafamily-utils/actions")
.build());

final String response = dingTalkNotifyHandler.notify(request);

System.out.println(response);
}

7166ad1b86b20804cf96f35e42e091d1.webp

  • Feed Card 通知

                 @Test
void testNotifyFeedCard() {
final DingTalkFeedCardRequest request
= DingTalkFeedCardRequest.of(FeedCardRequestContentLink.builder()
.title("Notification Manager(测试)")
.messageURL("https://github.com/orgs/JavaFamilyClub/projects/3")
.picURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png")
.build(),
FeedCardRequestContentLink.builder()
.title("JavaFamily Utils")
.messageURL("https://github.com/JavaFamilyClub/javafamily-utils/actions")
.picURL("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF")
.build(),
FeedCardRequestContentLink.builder()
.title("JavaFamily Parent Bom")
.messageURL("https://github.com/JavaFamilyClub/javafamily-parent/actions")
.picURL("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF")
.build());

final String response = dingTalkNotifyHandler.notify(request);

System.out.println(response);
}

1a6d5b174f667877715f7e8884b53a47.webp

5. 示例代码

所有的示例代码都在 https://github.com/JavaFamilyClub/notification-manager/tree/main/examples

  • 组件使用示例: 组件使用示例项目:https://github.com/JavaFamilyClub/notification-manager/tree/main/examples/demo-notification-manager

  • 抑制通知示例: 抑制配置示例项目:https://github.com/JavaFamilyClub/notification-manager/tree/main/examples/demo-notification-manager-inhibit


       

        如果有任何相关的问题都可以加入 QQ/微信群一起讨论, 学习, 进步. 此外如果有任何对于本公众号的意见和建议也欢迎大家留言积极批评指正, 最后, 愿你我都能成为更好的自己. 


        我是帅帅, 一个集帅气, 幽默与内涵, 并且热爱编程, 拥抱开源, 喜欢烹饪与旅游的暖男, 我们下期再见. 拜了个拜!

        老规矩别忘了哦, 点击原文链接跳转到我们官方的博客平台哦.



悄悄话



你见过凌晨 4 点的月亮吗?


每文一句



Don't aim for success if you really want it. Just stick to what you love and believe in, and it will come naturally.

少一些功利主义的追求, 多一些不为什么的坚持.


日常求赞

      你们白漂的力量就是我拖更的史诗级动力, 点赞, 评论, 再看, 赞赏, 看都看到这了, 随便点一个咯.



关注加好友


拉你进大佬交流群




浏览 141
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报