pull/4516/merge
DC 2 months ago committed by GitHub
commit 0fa588113c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,7 +3,6 @@ const axios = require("axios");
const { DOWN, UP } = require("../../src/util"); const { DOWN, UP } = require("../../src/util");
class WeCom extends NotificationProvider { class WeCom extends NotificationProvider {
name = "WeCom"; name = "WeCom";
/** /**
@ -13,13 +12,15 @@ class WeCom extends NotificationProvider {
let okMsg = "Sent Successfully."; let okMsg = "Sent Successfully.";
try { 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 = { let config = {
headers: { 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); await axios.post(WeComUrl, body, config);
return okMsg; return okMsg;
} catch (error) { } catch (error) {
@ -30,27 +31,66 @@ class WeCom extends NotificationProvider {
/** /**
* Generate the message to send * Generate the message to send
* @param {object} heartbeatJSON Heartbeat details (For Up/Down only) * @param {object} heartbeatJSON Heartbeat details (For Up/Down only)
* @param {object} monitorJSON Monitor details
* @param {string} msg General message * @param {string} msg General message
* @returns {object} Message * @returns {object} Message
*/ */
composeMessage(heartbeatJSON, msg) { composeMessage(heartbeatJSON, monitorJSON, msg) {
let title; if (heartbeatJSON != null) {
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) { return {
title = "UptimeKuma Monitor Up"; msgtype: "template_card",
} template_card: {
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) { card_type: "text_notice",
title = "UptimeKuma Monitor Down"; main_title: {
} title: this.statusToString(
if (msg != null) { heartbeatJSON["status"],
title = "UptimeKuma Message"; 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
},
},
};
} }
return { return {
msgtype: "text", msgtype: "text",
text: { text: {
content: title + 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; module.exports = WeCom;

Loading…
Cancel
Save