2021-10-13 01:13:46 -07: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) {
|
|
|
|
var params = {
|
2021-10-13 16:34:24 -07:00
|
|
|
msgtype: "markdown",
|
|
|
|
markdown: {
|
2021-10-13 01:13:46 -07: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 {
|
|
|
|
var params = {
|
2021-10-13 16:34:33 -07:00
|
|
|
msgtype: "text",
|
|
|
|
text: {
|
2021-10-13 01:13:46 -07:00
|
|
|
content:msg
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.sendToDingDing(notification, params)) {
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendToDingDing(notification, params) {
|
|
|
|
var timestamp=Date.now()
|
|
|
|
|
|
|
|
var config = {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
url: `${notification.webHookUrl}×tamp=${timestamp}&sign=${encodeURIComponent(this.sign(timestamp, notification.secretKey))}`,
|
|
|
|
data: JSON.stringify(params),
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = await axios(config);
|
|
|
|
if (result.data.errmsg == "ok") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DingDing sign */
|
|
|
|
sign(timestamp,secretKey) {
|
|
|
|
return Crypto
|
|
|
|
.createHmac("sha256",Buffer.from(secretKey, 'utf8'))
|
|
|
|
.update(Buffer.from(`${timestamp}\n${secretKey}`, 'utf8'))
|
|
|
|
.digest("base64");
|
|
|
|
}
|
|
|
|
|
|
|
|
statusToString(status) {
|
|
|
|
switch (status) {
|
|
|
|
case DOWN:
|
|
|
|
return "DOWN";
|
|
|
|
case UP:
|
|
|
|
return "UP";
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DingDing;
|