1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-12 10:13:46 +02:00
uptime-kuma/server/notification-providers/dingding.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-10-13 10:13:46 +02: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";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
try {
if (heartbeatJSON != null) {
2021-10-14 10:24:03 +02:00
let params = {
msgtype: "markdown",
markdown: {
2021-10-13 10:13:46 +02:00
title: monitorJSON["name"],
text: `## [${this.statusToString(heartbeatJSON["status"])}] \n > ${heartbeatJSON["msg"]} \n > Time(UTC):${heartbeatJSON["time"]}`,
}
};
if (this.sendToDingDing(notification, params)) {
return okMsg;
}
} else {
2021-10-14 10:24:03 +02:00
let params = {
msgtype: "text",
text: {
2021-10-14 10:24:03 +02:00
content: msg
2021-10-13 10:13:46 +02:00
}
};
if (this.sendToDingDing(notification, params)) {
return okMsg;
}
}
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
async sendToDingDing(notification, params) {
2021-10-14 10:24:03 +02:00
let timestamp = Date.now();
2021-10-13 10:13:46 +02:00
2021-10-14 10:24:03 +02:00
let config = {
2021-10-13 10:13:46 +02: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 10:24:03 +02:00
let result = await axios(config);
2021-10-13 10:13:46 +02:00
if (result.data.errmsg == "ok") {
return true;
}
return false;
}
/** DingDing sign */
2021-10-14 10:24:03 +02:00
sign(timestamp, secretKey) {
2021-10-13 10:13:46 +02:00
return Crypto
2021-10-14 10:24:03 +02:00
.createHmac("sha256", Buffer.from(secretKey, "utf8"))
.update(Buffer.from(`${timestamp}\n${secretKey}`, "utf8"))
2021-10-13 10:13:46 +02:00
.digest("base64");
}
statusToString(status) {
switch (status) {
case DOWN:
return "DOWN";
case UP:
return "UP";
default:
return status;
}
}
}
module.exports = DingDing;