From 7add28ebd98e9fac584c0c131f6c0dd07d73a55b Mon Sep 17 00:00:00 2001 From: xx Date: Sat, 21 Dec 2024 14:42:34 +0800 Subject: [PATCH 1/6] feat: improve dingtalk webhook mention add mention specify --- server/notification-providers/dingding.js | 24 ++++++++++++++++++----- src/components/notifications/DingDing.vue | 10 ++++++++++ src/lang/en.json | 4 ++++ src/lang/zh-CN.json | 4 ++++ src/lang/zh-HK.json | 8 +++++++- src/lang/zh-TW.json | 4 ++++ 6 files changed, 48 insertions(+), 6 deletions(-) diff --git a/server/notification-providers/dingding.js b/server/notification-providers/dingding.js index c66f270a7..795018700 100644 --- a/server/notification-providers/dingding.js +++ b/server/notification-providers/dingding.js @@ -11,17 +11,26 @@ class DingDing extends NotificationProvider { */ async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { const okMsg = "Sent Successfully."; - + const mentionAll = notification.mentioning === "everyone" + const mobileList = notification.mentioning === "specify-mobiles" ? notification.mobileList.split(",") : [] + const userList = notification.mentioning === "specify-users" ? notification.userList.split(",") : [] + const mentionStr = [...mobileList || [], ...userList || [] ].map(item => `@${item}`).join(" ") + console.log('mobileList: ',mobileList) + console.log('userList: ',userList) + console.log('mentionStr: ',mentionStr) try { if (heartbeatJSON != null) { + notification.mobileList let params = { msgtype: "markdown", markdown: { title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`, - text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`, + text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${'\n\n'+mentionStr}`, }, - "at": { - "isAtAll": notification.mentioning === "everyone" + at: { + isAtAll: mentionAll, + atUserIds: userList, + atMobiles: mobileList } }; if (await this.sendToDingDing(notification, params)) { @@ -31,7 +40,12 @@ class DingDing extends NotificationProvider { let params = { msgtype: "text", text: { - content: msg + content: `${msg}${'\n'+mentionStr}` + }, + at: { + isAtAll: mentionAll, + atUserIds: userList, + atMobiles: mobileList } }; if (await this.sendToDingDing(notification, params)) { diff --git a/src/components/notifications/DingDing.vue b/src/components/notifications/DingDing.vue index 710677fd7..3ed974d6e 100644 --- a/src/components/notifications/DingDing.vue +++ b/src/components/notifications/DingDing.vue @@ -19,8 +19,18 @@ +
+ + +
+
+ + +
diff --git a/src/lang/en.json b/src/lang/en.json index 641dae02e..3a80e37c7 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -688,9 +688,13 @@ "Don't mention people": "Don't mention people", "Mention group": "Mention {group}", "Mention Mobile List": "Mention mobile list", - "Mention User List": "Mention user ID List", - "Dingtalk Mobile List": "Mobile List (use comma separator)", - "Dingtalk User List": "User List (use comma separator)", + "Mention User List": "Mention user id list", + "Dingtalk Mobile List": "Mobile list", + "Dingtalk User List": "User ID list", + "Enter a list of userId": "Enter a list of userId", + "Enter a list of mobile": "Enter a list of mobile", + "Invalid mobile": "Invalid mobile [{mobile}]", + "Invalid userId": "Invalid userId [{userId}]", "Device Token": "Device Token", "Platform": "Platform", "Huawei": "Huawei", From 25adf97b2babc62b8029203976cd7e6b24db7b29 Mon Sep 17 00:00:00 2001 From: xx Date: Tue, 24 Dec 2024 09:54:33 +0800 Subject: [PATCH 6/6] fix: trim str, clear choise when change option --- server/notification-providers/dingding.js | 7 ++++--- src/components/notifications/DingDing.vue | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/server/notification-providers/dingding.js b/server/notification-providers/dingding.js index 39f8b1711..cfacd45ed 100644 --- a/server/notification-providers/dingding.js +++ b/server/notification-providers/dingding.js @@ -14,14 +14,15 @@ class DingDing extends NotificationProvider { const mentionAll = notification.mentioning === "everyone"; const mobileList = notification.mentioning === "specify-mobiles" ? notification.mobileList : []; const userList = notification.mentioning === "specify-users" ? notification.userList : []; - const mentionStr = [ ...mobileList || [], ...userList || [] ].map(item => `@${item}`).join(" "); + const finalList = [ ...mobileList || [], ...userList || [] ]; + const mentionStr = finalList.length > 0 ? "\n" : "" + finalList.map(item => `@${item}`).join(" "); try { if (heartbeatJSON != null) { let params = { msgtype: "markdown", markdown: { title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`, - text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${"\n\n" + mentionStr}`, + text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${mentionStr}`, }, at: { isAtAll: mentionAll, @@ -36,7 +37,7 @@ class DingDing extends NotificationProvider { let params = { msgtype: "text", text: { - content: `${msg}${"\n" + mentionStr}` + content: `${msg}${mentionStr}` }, at: { isAtAll: mentionAll, diff --git a/src/components/notifications/DingDing.vue b/src/components/notifications/DingDing.vue index d5227bb39..a5fd3c82b 100644 --- a/src/components/notifications/DingDing.vue +++ b/src/components/notifications/DingDing.vue @@ -16,7 +16,7 @@
- @@ -94,10 +94,20 @@ export default { if (typeof this.$parent.notification.userList === "undefined") { this.$parent.notification.userList = []; } else { - this.mobileOpts = this.$parent.notification.mobileList; + this.userIdOpts = this.$parent.notification.userList; } }, methods: { + onMentioningChange(e) { + if (e.target.value === "specify-mobiles") { + this.$parent.notification.userList = []; + } else if (e.target.value === "specify-users") { + this.$parent.notification.mobileList = []; + } else { + this.$parent.notification.userList = []; + this.$parent.notification.mobileList = []; + } + }, addMobile(mobile) { const trimmedMobile = mobile.trim(); const chinaMobileRegex = /^1[3-9]\d{9}$/;