mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-23 23:04:04 +00:00
Use template_card instead of text messages for the WeCom notification provider
This commit is contained in:
parent
0254e72177
commit
c70ec1ea50
1 changed files with 68 additions and 13 deletions
|
@ -12,13 +12,16 @@ class WeCom extends NotificationProvider {
|
||||||
const okMsg = "Sent Successfully.";
|
const okMsg = "Sent Successfully.";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
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(`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${notification.weComBotKey}`, body, config);
|
await axios.post(WeComUrl, body, config);
|
||||||
return okMsg;
|
return okMsg;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
|
@ -28,24 +31,76 @@ 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 = "UptimeKuma Message";
|
if (heartbeatJSON != null) {
|
||||||
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) {
|
const templateCard = {
|
||||||
title = "UptimeKuma Monitor Up";
|
card_type: "text_notice",
|
||||||
}
|
main_title: {
|
||||||
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) {
|
title: this.statusToString(
|
||||||
title = "UptimeKuma Monitor Down";
|
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 {
|
return {
|
||||||
msgtype: "text",
|
msgtype: "text",
|
||||||
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;
|
module.exports = WeCom;
|
||||||
|
|
Loading…
Reference in a new issue