Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use template_card instead of text messages for the WeCom notification provider #5286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 68 additions & 13 deletions server/notification-providers/wecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, while the other PR was dormant, we noticed that there are ways in which the url should be handled.

Instead of using monitorJSON["url"], could you extract the adress like this

const address = this.extractAddress(monitorJSON)

and then only check adress?

Example in another notification provider:

const address = this.extractAddress(monitorJSON);

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;
Loading