快速上手 SpringBoot 釘釘消息推送
“ ?SpringBoot 釘釘通知處理器。”

之前介紹的 基于 Prometheus+Grafana+Alertmanager+飛書通知的智能監(jiān)控平臺 中提到我們有時候會把一些信息推送到工作交流平臺----飛書(或釘釘), 上一篇專題介紹一下飛書推送組件快速上手 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 釘釘通知配置
創(chuàng)建你自己的釘釘 WebHook 機(jī)器人, 在 application.yml 中配置通知的 webhook 地址
javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
enabled: true # 是否開啟通知, 用于不同環(huán)境下的區(qū)分(開發(fā), 測試, 生產(chǎn)), 默認(rèn)為 true
2.2 抑制策略
當(dāng)我們需要對通知進(jìn)行抑制時(如: 通過飛書通知一些接口異常、服務(wù)宕機(jī)等信息, 有時候并不需要一直推送通知消息), 此時, 就可以通過抑制策略進(jìn)行通知消息的抑制!
javafamily:
notify:
dingtalk:
hook-url: https://oapi.dingtalk.com/robot/send?access_token=5221404563667b04140f92e5820e6213fcdc2fe6a97560fe1f233fd468ef8e75
inhibit:
enabled: on # 默認(rèn)為 off
ttl: 1h # 代表同一個消息, 1h 只推送一次
通過指定
inhibit屬性進(jìn)行抑制配置, 目前支持的屬性有:
enabled: 是否開啟抑制
ttl: 抑制時效(同樣的通知多久發(fā)送一次)
通知抑制是通過 javafamily-cache 組件 提供組件服務(wù)與配置, 因此,
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 緩存相關(guān)配置
max-size: 500
weak-keys: on
soft-values: on
record-stats: on
需要注意,
cache.time-to-live與inhibit.ttl如果都配置, 則inhibit.ttl優(yōu)先級更高(生效).
更多配置請查看 JavaFamilyClub/javafamily-cache
2.3 restTemplate 配置
發(fā)送 webhook 請求底層是通過封裝的
resttemplate進(jìn)行請求, 而restTemplate是通過 javafamily-resttemplate-starter 提供組件服務(wù)與配置, 因此,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. 創(chuàng)建 Request, 發(fā)送通知
-
Text 通知
@Test
void testNotifyText() {
final String response = dingTalkNotifyHandler.notify(
DingTalkTextNotifyRequest.of("這是一個測試數(shù)據(jù)!"));
System.out.println(response);
}

-
At 通知
@Test
void testNotifyTextAt() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "異常" : "正常";
String content = "測試數(shù)據(jù)時次: " + dataTime
+ "\n應(yīng)收收據(jù)個數(shù): " + shouldCount
+ "\n實收數(shù)據(jù)個數(shù): " + actualCount
+ "\n監(jiān)控狀態(tài): **" + status + "**";
final String response = dingTalkNotifyHandler.notify(
DingTalkTextNotifyRequest.of(content, false, "18829346477"));
System.out.println(response);
}

-
Link通知
@Test
void testNotifyLink() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "異常" : "正常";
String content = "數(shù)據(jù)時次: " + dataTime
+ "\n應(yīng)收收據(jù)個數(shù): " + shouldCount
+ "\n實收數(shù)據(jù)個數(shù): " + actualCount
+ "\n監(jiān)控狀態(tài): **" + 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);
}

-
Markdown 通知
@Test
void testNotifyMarkdown() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "異常" : "正常";
String content = "@18829346477 數(shù)據(jù)時次: " + dataTime
+ "\n應(yīng)收收據(jù)個數(shù): " + shouldCount
+ "\n實收數(shù)據(jù)個數(shù): " + actualCount
+ "\n監(jiān)控狀態(tài): **" + status + "**";
DingTalkMarkDownRequest request = DingTalkMarkDownRequest.of("項目更新通知(測試)",
content, false, "18829346477");
final String response = dingTalkNotifyHandler.notify(request);
System.out.println(response);
}

-
單按鈕通知
@Test
void testNotifySingleCard() {
String dataTime = "2022-06-05 23:00:00";
int shouldCount = 20, actualCount = 20;
String status = actualCount < shouldCount ? "異常" : "正常";
String content = "數(shù)據(jù)時次: " + dataTime
+ "\n應(yīng)收收據(jù)個數(shù): " + shouldCount
+ "\n實收數(shù)據(jù)個數(shù): " + actualCount
+ "\n監(jiān)控狀態(tài): **" + status + "**";
final DingTalkSingleBtnCardRequest request
= DingTalkSingleBtnCardRequest.of("測試xxx數(shù)據(jù)監(jiān)控", content,
"立即前往系統(tǒng)查看",
"https://github.com/orgs/JavaFamilyClub/projects/3");
final String response = dingTalkNotifyHandler.notify(request);
System.out.println(response);
}

-
多按鈕通知
@Test
void testNotifyMultiCard() {
String content = "測試 xxx 點(diǎn)位 (33.3, 107.7) 已經(jīng)添加至用戶點(diǎn)位庫, 是否加入資源池?";
final DingTalkMultiBtnCardRequest request
= DingTalkMultiBtnCardRequest.of("點(diǎn)位審核", 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);
}

-
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);
}

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
? ? ? ?
? ? ? ? 如果有任何相關(guān)的問題都可以加入 QQ/微信群一起討論, 學(xué)習(xí), 進(jìn)步. 此外如果有任何對于本公眾號的意見和建議也歡迎大家留言積極批評指正, 最后, 愿你我都能成為更好的自己.?
? ? ? ? 我是帥帥, 一個集帥氣, 幽默與內(nèi)涵, 并且熱愛編程, 擁抱開源, 喜歡烹飪與旅游的暖男, 我們下期再見.?拜了個拜!
? ? ? ??老規(guī)矩別忘了哦,?點(diǎn)擊原文鏈接跳轉(zhuǎn)到我們官方的博客平臺哦.
悄悄話
— — — —
每文一句
— — — —
Don't aim for success if you really want it. Just stick to what you love and believe in, and it will come naturally.
少一些功利主義的追求, 多一些不為什么的堅持.
日常求贊
— — — —
? ? ? 你們白漂的力量就是我拖更的史詩級動力, 點(diǎn)贊, 評論, 再看, 贊賞, 看都看到這了, 隨便點(diǎn)一個咯.
關(guān)注加好友
拉你進(jìn)大佬交流群
— — — — — — — — — — — — — — — —
