From c70ec1ea503a00262101cb70a4b9f6dfe6116575 Mon Sep 17 00:00:00 2001 From: darkclip Date: Thu, 31 Oct 2024 01:26:46 +0800 Subject: [PATCH 1/2] Use template_card instead of text messages for the WeCom notification provider --- server/notification-providers/wecom.js | 81 +++++++++++++++++++++----- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 1eb069095..2b509c8a9 100644 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -12,13 +12,16 @@ class WeCom extends NotificationProvider { const okMsg = "Sent Successfully."; try { + 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); - await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${notification.weComBotKey}`, body, config); + let body = this.composeMessage(heartbeatJSON, monitorJSON, msg); + await axios.post(WeComUrl, body, config); return okMsg; } catch (error) { this.throwGeneralAxiosError(error); @@ -28,24 +31,76 @@ 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 = "UptimeKuma Message"; - 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) { + if (heartbeatJSON != null) { + const templateCard = { + card_type: "text_notice", + 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"] + ? monitorJSON["url"] + : "https://github.com/louislam/uptime-kuma", // both card_action and card_action.url are mandatory + }, + }; + if (monitorJSON["url"]) { + templateCard["jump_list"] = [ + { + type: 1, + url: monitorJSON["url"], + title: "Monitor URL", + }, + ]; + } + return { + msgtype: "template_card", + template_card: templateCard, + }; } return { msgtype: "text", text: { - content: title + "\n" + msg - } + content: msg, + }, }; } + + /** + * Convert status constant to string + * @param {const} status The status constant + * @param {string} monitorName Name of monitor + * @returns {string} Status + */ + statusToString(status, monitorName) { + switch (status) { + case DOWN: + return `🔴 [${monitorName}] DOWN`; + case UP: + return `✅ [${monitorName}] UP`; + default: + return "Notification"; + } + } } module.exports = WeCom; From fafa1a78ee0bfd9445ddeb073851acbbc562d1be Mon Sep 17 00:00:00 2001 From: darkclip Date: Fri, 1 Nov 2024 14:58:06 +0800 Subject: [PATCH 2/2] use extractAddress() instead of monitorJSON["url"] for better url handling --- server/notification-providers/wecom.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server/notification-providers/wecom.js b/server/notification-providers/wecom.js index 2b509c8a9..529baa1b2 100644 --- a/server/notification-providers/wecom.js +++ b/server/notification-providers/wecom.js @@ -36,6 +36,7 @@ class WeCom extends NotificationProvider { * @returns {object} Message */ composeMessage(heartbeatJSON, monitorJSON, msg) { + const address = this.extractAddress(monitorJSON); if (heartbeatJSON != null) { const templateCard = { card_type: "text_notice", @@ -58,16 +59,16 @@ class WeCom extends NotificationProvider { ], card_action: { type: 1, - url: monitorJSON["url"] - ? monitorJSON["url"] + url: address + ? address : "https://github.com/louislam/uptime-kuma", // both card_action and card_action.url are mandatory }, }; - if (monitorJSON["url"]) { + if (address) { templateCard["jump_list"] = [ { type: 1, - url: monitorJSON["url"], + url: address, title: "Monitor URL", }, ];