uptime-kuma/server/notification-providers/wecom.js

52 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2021-12-25 17:33:47 +00:00
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");
class WeCom extends NotificationProvider {
name = "WeCom";
/**
* @inheritdoc
*/
2021-12-25 17:33:47 +00:00
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
2021-12-25 17:33:47 +00:00
try {
let config = {
headers: {
"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);
2021-12-25 17:33:47 +00:00
return okMsg;
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
/**
* Generate the message to send
* @param {object} heartbeatJSON Heartbeat details (For Up/Down only)
* @param {string} msg General message
* @returns {object} Message
*/
2021-12-25 17:33:47 +00:00
composeMessage(heartbeatJSON, msg) {
let title = "UptimeKuma Message";
2022-04-17 08:43:03 +01:00
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) {
2021-12-25 17:33:47 +00:00
title = "UptimeKuma Monitor Up";
}
2022-04-17 08:43:03 +01:00
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) {
2021-12-25 17:33:47 +00:00
title = "UptimeKuma Monitor Down";
}
return {
msgtype: "text",
text: {
content: title + "\n" + msg
2021-12-25 17:33:47 +00:00
}
};
}
}
module.exports = WeCom;