2021-10-13 09:13:46 +01: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 09:24:03 +01:00
|
|
|
let params = {
|
2021-10-14 00:34:24 +01:00
|
|
|
msgtype: "markdown",
|
|
|
|
markdown: {
|
2021-11-23 12:36:22 +00:00
|
|
|
title: `[${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]}`,
|
2023-05-12 15:04:44 +01:00
|
|
|
text: `## [${this.statusToString(heartbeatJSON["status"])}] ${monitorJSON["name"]} \n> ${heartbeatJSON["msg"]}\n> Time(${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`,
|
2021-10-13 09:13:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.sendToDingDing(notification, params)) {
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-14 09:24:03 +01:00
|
|
|
let params = {
|
2021-10-14 00:34:33 +01:00
|
|
|
msgtype: "text",
|
|
|
|
text: {
|
2021-10-14 09:24:03 +01:00
|
|
|
content: msg
|
2021-10-13 09:13:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.sendToDingDing(notification, params)) {
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:53 +01:00
|
|
|
/**
|
|
|
|
* Send message to DingDing
|
|
|
|
* @param {BeanModel} notification
|
|
|
|
* @param {Object} params Parameters of message
|
|
|
|
* @returns {boolean} True if successful else false
|
|
|
|
*/
|
2021-10-13 09:13:46 +01:00
|
|
|
async sendToDingDing(notification, params) {
|
2021-10-14 09:24:03 +01:00
|
|
|
let timestamp = Date.now();
|
2021-10-13 09:13:46 +01:00
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
let config = {
|
2021-10-13 09:13:46 +01: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 09:24:03 +01:00
|
|
|
let result = await axios(config);
|
2022-04-17 08:43:03 +01:00
|
|
|
if (result.data.errmsg === "ok") {
|
2021-10-13 09:13:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:53 +01:00
|
|
|
/**
|
|
|
|
* DingDing sign
|
|
|
|
* @param {Date} timestamp Timestamp of message
|
|
|
|
* @param {string} secretKey Secret key to sign data with
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-10-14 09:24:03 +01:00
|
|
|
sign(timestamp, secretKey) {
|
2021-10-13 09:13:46 +01:00
|
|
|
return Crypto
|
2021-10-14 09:24:03 +01:00
|
|
|
.createHmac("sha256", Buffer.from(secretKey, "utf8"))
|
|
|
|
.update(Buffer.from(`${timestamp}\n${secretKey}`, "utf8"))
|
2021-10-13 09:13:46 +01:00
|
|
|
.digest("base64");
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:24:53 +01:00
|
|
|
/**
|
|
|
|
* Convert status constant to string
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-10-13 09:13:46 +01:00
|
|
|
statusToString(status) {
|
2022-04-16 20:24:53 +01:00
|
|
|
// TODO: Move to notification-provider.js to avoid repetition in classes
|
2021-10-13 09:13:46 +01:00
|
|
|
switch (status) {
|
|
|
|
case DOWN:
|
|
|
|
return "DOWN";
|
|
|
|
case UP:
|
|
|
|
return "UP";
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DingDing;
|