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

113 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-10-13 08:13:46 +00:00
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
const { default: axios } = require("axios");
const Crypto = require("crypto");
class DingDing extends NotificationProvider {
name = "DingDing";
/**
* @inheritdoc
*/
2021-10-13 08:13:46 +00:00
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
2024-12-21 06:56:15 +00:00
const mentionAll = notification.mentioning === "everyone"
const mobileList = notification.mentioning === "specify-mobiles" ? notification.mobileList.split(",") : []
const userList = notification.mentioning === "specify-users" ? notification.userList.split(",") : []
const mentionStr = [...mobileList || [], ...userList || []].map(item => `@${item}`).join(" ")
2021-10-13 08:13:46 +00:00
try {
if (heartbeatJSON != null) {
notification.mobileList
2021-10-14 08:24:03 +00:00
let params = {
msgtype: "markdown",
markdown: {
title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`,
2024-12-21 06:56:15 +00:00
text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}${'\n\n' + mentionStr}`,
},
at: {
isAtAll: mentionAll,
atUserIds: userList,
atMobiles: mobileList
2021-10-13 08:13:46 +00:00
}
};
if (await this.sendToDingDing(notification, params)) {
2021-10-13 08:13:46 +00:00
return okMsg;
}
} else {
2021-10-14 08:24:03 +00:00
let params = {
msgtype: "text",
text: {
2024-12-21 06:56:15 +00:00
content: `${msg}${'\n' + mentionStr}`
},
at: {
isAtAll: mentionAll,
atUserIds: userList,
atMobiles: mobileList
2021-10-13 08:13:46 +00:00
}
};
if (await this.sendToDingDing(notification, params)) {
2021-10-13 08:13:46 +00:00
return okMsg;
}
}
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
/**
* Send message to DingDing
* @param {BeanModel} notification Notification to send
* @param {object} params Parameters of message
* @returns {Promise<boolean>} True if successful else false
*/
2021-10-13 08:13:46 +00:00
async sendToDingDing(notification, params) {
2021-10-14 08:24:03 +00:00
let timestamp = Date.now();
2021-10-13 08:13:46 +00:00
2021-10-14 08:24:03 +00:00
let config = {
2021-10-13 08:13:46 +00:00
method: "POST",
headers: {
"Content-Type": "application/json",
},
url: `${notification.webHookUrl}&timestamp=${timestamp}&sign=${encodeURIComponent(this.sign(timestamp, notification.secretKey))}`,
data: JSON.stringify(params),
};
2021-10-14 08:24:03 +00:00
let result = await axios(config);
2022-04-17 07:43:03 +00:00
if (result.data.errmsg === "ok") {
2021-10-13 08:13:46 +00:00
return true;
}
throw new Error(result.data.errmsg);
2021-10-13 08:13:46 +00:00
}
/**
* DingDing sign
* @param {Date} timestamp Timestamp of message
* @param {string} secretKey Secret key to sign data with
* @returns {string} Base64 encoded signature
*/
2021-10-14 08:24:03 +00:00
sign(timestamp, secretKey) {
2021-10-13 08:13:46 +00:00
return Crypto
2021-10-14 08:24:03 +00:00
.createHmac("sha256", Buffer.from(secretKey, "utf8"))
.update(Buffer.from(`${timestamp}\n${secretKey}`, "utf8"))
2021-10-13 08:13:46 +00:00
.digest("base64");
}
/**
* Convert status constant to string
* @param {const} status The status constant
* @returns {string} Status
*/
2021-10-13 08:13:46 +00:00
statusToString(status) {
switch (status) {
case DOWN:
return "DOWN";
case UP:
return "UP";
default:
return status;
}
}
}
module.exports = DingDing;