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";
|
|
|
|
|
2023-08-11 09:46:41 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-10-13 10:13:46 +02:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 15:21:15 +02:00
|
|
|
const okMsg = "Sent Successfully.";
|
2021-10-13 10:13:46 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON != null) {
|
2021-10-14 10:24:03 +02:00
|
|
|
let params = {
|
2021-10-14 01:34:24 +02:00
|
|
|
msgtype: "markdown",
|
|
|
|
markdown: {
|
2021-11-23 14:36:22 +02:00
|
|
|
title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`,
|
2023-05-12 16:14:59 +02:00
|
|
|
text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`,
|
2024-04-27 17:16:39 +02:00
|
|
|
},
|
|
|
|
"at": {
|
|
|
|
"isAtAll": notification.mentioning === "everyone"
|
2021-10-13 10:13:46 +02:00
|
|
|
}
|
|
|
|
};
|
2024-03-15 16:02:55 +02:00
|
|
|
if (await this.sendToDingDing(notification, params)) {
|
2021-10-13 10:13:46 +02:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-14 10:24:03 +02:00
|
|
|
let params = {
|
2021-10-14 01:34:33 +02:00
|
|
|
msgtype: "text",
|
|
|
|
text: {
|
2021-10-14 10:24:03 +02:00
|
|
|
content: msg
|
2021-10-13 10:13:46 +02:00
|
|
|
}
|
|
|
|
};
|
2024-03-15 16:02:55 +02:00
|
|
|
if (await this.sendToDingDing(notification, params)) {
|
2021-10-13 10:13:46 +02:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Send message to DingDing
|
2023-08-11 09:46:41 +02:00
|
|
|
* @param {BeanModel} notification Notification to send
|
|
|
|
* @param {object} params Parameters of message
|
2024-03-15 16:02:55 +02:00
|
|
|
* @returns {Promise<boolean>} True if successful else false
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2021-10-13 10:13:46 +02:00
|
|
|
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}×tamp=${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);
|
2022-04-17 09:43:03 +02:00
|
|
|
if (result.data.errmsg === "ok") {
|
2021-10-13 10:13:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-01-19 21:29:13 +02:00
|
|
|
throw new Error(result.data.errmsg);
|
2021-10-13 10:13:46 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* DingDing sign
|
|
|
|
* @param {Date} timestamp Timestamp of message
|
|
|
|
* @param {string} secretKey Secret key to sign data with
|
2023-08-11 09:46:41 +02:00
|
|
|
* @returns {string} Base64 encoded signature
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
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");
|
|
|
|
}
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Convert status constant to string
|
|
|
|
* @param {const} status The status constant
|
2023-08-11 09:46:41 +02:00
|
|
|
* @returns {string} Status
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2021-10-13 10:13:46 +02:00
|
|
|
statusToString(status) {
|
2022-04-16 21:24:53 +02:00
|
|
|
// TODO: Move to notification-provider.js to avoid repetition in classes
|
2021-10-13 10:13:46 +02:00
|
|
|
switch (status) {
|
|
|
|
case DOWN:
|
|
|
|
return "DOWN";
|
|
|
|
case UP:
|
|
|
|
return "UP";
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DingDing;
|