From 843992c4104160b76439c455e414dbe6cb8a678c Mon Sep 17 00:00:00 2001 From: wuwenjing Date: Wed, 13 Oct 2021 16:13:46 +0800 Subject: [PATCH] Add DingDing notification --- server/notification-providers/dingding.js | 79 +++++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/DingDing.vue | 16 +++++ src/components/notifications/index.js | 2 + 4 files changed, 99 insertions(+) create mode 100644 server/notification-providers/dingding.js create mode 100644 src/components/notifications/DingDing.vue diff --git a/server/notification-providers/dingding.js b/server/notification-providers/dingding.js new file mode 100644 index 000000000..5a64f4aaf --- /dev/null +++ b/server/notification-providers/dingding.js @@ -0,0 +1,79 @@ +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 = { + msgtype:"markdown", + markdown:{ + 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 = { + msgtype:"text", + text:{ + 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; diff --git a/server/notification.js b/server/notification.js index 21bd2b1e6..658216f91 100644 --- a/server/notification.js +++ b/server/notification.js @@ -20,6 +20,7 @@ const Telegram = require("./notification-providers/telegram"); const Webhook = require("./notification-providers/webhook"); const Feishu = require("./notification-providers/feishu"); const AliyunSms = require("./notification-providers/aliyun-sms"); +const DingDing = require("./notification-providers/dingding"); class Notification { @@ -33,6 +34,7 @@ class Notification { const list = [ new Apprise(), new AliyunSms(), + new DingDing(), new Discord(), new Teams(), new Gotify(), diff --git a/src/components/notifications/DingDing.vue b/src/components/notifications/DingDing.vue new file mode 100644 index 000000000..3ee475c80 --- /dev/null +++ b/src/components/notifications/DingDing.vue @@ -0,0 +1,16 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 74fe0df38..ff0acc6a5 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -19,6 +19,7 @@ import Line from "./Line.vue"; import Mattermost from "./Mattermost.vue"; import Matrix from "./Matrix.vue"; import AliyunSMS from "./AliyunSms.vue"; +import DingDing from "./DingDing.vue"; /** * Manage all notification form. @@ -47,6 +48,7 @@ const NotificationFormList = { "line": Line, "mattermost": Mattermost, "matrix": Matrix, + "DingDing":DingDing } export default NotificationFormList