From b5eaac7d287e445701e2c755d5a295cc361539ff Mon Sep 17 00:00:00 2001 From: darkclip Date: Thu, 22 Feb 2024 16:11:35 +0800 Subject: [PATCH 1/7] add markdown message support for WeCom notification provider --- server/notification-providers/wecom.js | 65 ++++++++++++++++++-------- 1 file changed, 45 insertions(+), 20 deletions(-) mode change 100644 => 100755 server/notification-providers/wecom.js diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js old mode 100644 new mode 100755 index ea30b26d..774d975b --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -3,7 +3,6 @@ const axios = require("axios"); const { DOWN, UP } = require("../../src/util"); class WeCom extends NotificationProvider { - name = "WeCom"; /** @@ -13,13 +12,15 @@ class WeCom extends NotificationProvider { let okMsg = "Sent Successfully."; try { - let WeComUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + notification.weComBotKey; + let WeComUrl = + "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + + notification.weComBotKey; let config = { headers: { - "Content-Type": "application/json" - } + "Content-Type": "application/json", + }, }; - let body = this.composeMessage(heartbeatJSON, msg); + let body = this.composeMessage(heartbeatJSON, monitorJSON, msg); await axios.post(WeComUrl, body, config); return okMsg; } catch (error) { @@ -30,26 +31,50 @@ class WeCom extends NotificationProvider { /** * Generate the message to send * @param {object} heartbeatJSON Heartbeat details (For Up/Down only) + * @param {object} monitorJSON Monitor details * @param {string} msg General message * @returns {object} Message */ - composeMessage(heartbeatJSON, msg) { - let title; - if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) { - title = "UptimeKuma Monitor Up"; - } - if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) { - title = "UptimeKuma Monitor Down"; + composeMessage(heartbeatJSON, monitorJSON, msg) { + let params = {}; + if (heartbeatJSON != null) { + params = { + msgtype: "markdown", + markdown: { + content: `## [${this.statusToString( + heartbeatJSON["status"] + )}] ${monitorJSON["name"]} \n> ${ + heartbeatJSON["msg"] + } \n> Time (${heartbeatJSON["timezone"]}): ${ + heartbeatJSON["localDateTime"] + }`, + }, + }; + } else { + params = { + msgtype: "text", + text: { + content: msg, + }, + }; } - if (msg != null) { - title = "UptimeKuma Message"; + return params; + } + + /** + * Convert status constant to string + * @param {const} status The status constant + * @returns {string} + */ + statusToString(status) { + switch (status) { + case DOWN: + return "DOWN"; + case UP: + return "UP"; + default: + return status; } - return { - msgtype: "text", - text: { - content: title + msg - } - }; } } From 45a56d4131f3f06728fed26fad390befb2c86467 Mon Sep 17 00:00:00 2001 From: darkclip Date: Thu, 22 Feb 2024 16:18:51 +0800 Subject: [PATCH 2/7] add missing jsdoc description --- server/notification-providers/wecom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 774d975b..3048af07 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -64,7 +64,7 @@ class WeCom extends NotificationProvider { /** * Convert status constant to string * @param {const} status The status constant - * @returns {string} + * @returns {string} Status */ statusToString(status) { switch (status) { From cd0a99de96c3b3b2061df987e229d76dae53aaf9 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 22 Feb 2024 14:01:19 +0100 Subject: [PATCH 3/7] inlined `params` --- server/notification-providers/wecom.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 3048af07..25bc91de 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -36,9 +36,8 @@ class WeCom extends NotificationProvider { * @returns {object} Message */ composeMessage(heartbeatJSON, monitorJSON, msg) { - let params = {}; if (heartbeatJSON != null) { - params = { + return { msgtype: "markdown", markdown: { content: `## [${this.statusToString( @@ -50,15 +49,13 @@ class WeCom extends NotificationProvider { }`, }, }; - } else { - params = { - msgtype: "text", - text: { - content: msg, - }, - }; } - return params; + return { + msgtype: "text", + text: { + content: msg, + }, + }; } /** From 8ccd49928a395ecdf8ba3117293e18679cd85c5c Mon Sep 17 00:00:00 2001 From: darkclip Date: Thu, 22 Feb 2024 22:21:50 +0800 Subject: [PATCH 4/7] change message format to template_card --- server/notification-providers/wecom.js | 45 ++++++++++++++++++-------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 25bc91de..4ea54e06 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -38,15 +38,33 @@ class WeCom extends NotificationProvider { composeMessage(heartbeatJSON, monitorJSON, msg) { if (heartbeatJSON != null) { return { - msgtype: "markdown", - markdown: { - content: `## [${this.statusToString( - heartbeatJSON["status"] - )}] ${monitorJSON["name"]} \n> ${ - heartbeatJSON["msg"] - } \n> Time (${heartbeatJSON["timezone"]}): ${ - heartbeatJSON["localDateTime"] - }`, + msgtype: "template_card", + template_card: { + card_type: "text_notice", + source: { + desc: monitorJSON["name"], + }, + main_title: { + title: this.statusToString( + heartbeatJSON["status"], + monitorJSON["name"] + ), + }, + sub_title_text: heartbeatJSON["msg"], + horizontal_content_list: [ + { + keyname: "Timezone", + value: heartbeatJSON["timezone"], + }, + { + keyname: "Time", + value: heartbeatJSON["localDateTime"], + }, + ], + card_action: { + type: 1, + url: monitorJSON["url"], + }, }, }; } @@ -61,16 +79,17 @@ class WeCom extends NotificationProvider { /** * Convert status constant to string * @param {const} status The status constant + * @param {string} monitorName Name of monitor * @returns {string} Status */ - statusToString(status) { + statusToString(status, monitorName) { switch (status) { case DOWN: - return "DOWN"; + return `🔴 [${monitorName}] DOWN`; case UP: - return "UP"; + return `✅ [${monitorName}] UP`; default: - return status; + return "Notification"; } } } From 8a989e6e6dbfd62f4493877d79cc53d7341e25f9 Mon Sep 17 00:00:00 2001 From: darkclip Date: Fri, 23 Feb 2024 01:00:25 +0800 Subject: [PATCH 5/7] remove title bar --- server/notification-providers/wecom.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 4ea54e06..56201737 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -41,9 +41,6 @@ class WeCom extends NotificationProvider { msgtype: "template_card", template_card: { card_type: "text_notice", - source: { - desc: monitorJSON["name"], - }, main_title: { title: this.statusToString( heartbeatJSON["status"], From 865e32293b3f0b8bd546eea2534654d7f711b45b Mon Sep 17 00:00:00 2001 From: darkclip Date: Fri, 23 Feb 2024 01:46:24 +0800 Subject: [PATCH 6/7] always set card_action url, or notification wouldn't be sent; --- server/notification-providers/wecom.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 56201737..2f2136e4 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -60,7 +60,9 @@ class WeCom extends NotificationProvider { ], card_action: { type: 1, - url: monitorJSON["url"], + url: monitorJSON["url"] + ? monitorJSON["url"] + : "https://", }, }, }; From f95a4c0049efe7d37fc4c2cd30ad0737667ee210 Mon Sep 17 00:00:00 2001 From: DC Date: Fri, 8 Mar 2024 16:17:04 +0800 Subject: [PATCH 7/7] set default action URL to github when there's no monitor URL exist Co-authored-by: Frank Elsinga --- server/notification-providers/wecom.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 2f2136e4..d24a35ec 100755 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -62,7 +62,7 @@ class WeCom extends NotificationProvider { type: 1, url: monitorJSON["url"] ? monitorJSON["url"] - : "https://", + : "https://github.com/louislam/uptime-kuma", // both card_action and card_action.url are mandatory }, }, };