Add DingDing notification

This commit is contained in:
wuwenjing 2021-10-13 16:13:46 +08:00
parent 57a76e6129
commit 843992c410
4 changed files with 99 additions and 0 deletions

View File

@ -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}&timestamp=${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;

View File

@ -20,6 +20,7 @@ const Telegram = require("./notification-providers/telegram");
const Webhook = require("./notification-providers/webhook"); const Webhook = require("./notification-providers/webhook");
const Feishu = require("./notification-providers/feishu"); const Feishu = require("./notification-providers/feishu");
const AliyunSms = require("./notification-providers/aliyun-sms"); const AliyunSms = require("./notification-providers/aliyun-sms");
const DingDing = require("./notification-providers/dingding");
class Notification { class Notification {
@ -33,6 +34,7 @@ class Notification {
const list = [ const list = [
new Apprise(), new Apprise(),
new AliyunSms(), new AliyunSms(),
new DingDing(),
new Discord(), new Discord(),
new Teams(), new Teams(),
new Gotify(), new Gotify(),

View File

@ -0,0 +1,16 @@
<template>
<div class="mb-3">
<label for="WebHookUrl" class="form-label">{{ $t("WebHookUrl") }}<span style="color: red;"><sup>*</sup></span></label>
<input id="WebHookUrl" v-model="$parent.notification.webHookUrl" type="text" class="form-control" required>
<label for="secretKey" class="form-label">{{ $t("SecretKey") }}<span style="color: red;"><sup>*</sup></span></label>
<input id="secretKey" v-model="$parent.notification.secretKey" type="text" class="form-control" required>
<div class="form-text">
<p>For safety, must use secret key</p>
<i18n-t tag="p" keypath="Read more:">
<a href="https://developers.dingtalk.com/document/robots/custom-robot-access" target="_blank">https://developers.dingtalk.com/document/robots/custom-robot-access</a>
</i18n-t>
</div>
</div>
</template>

View File

@ -19,6 +19,7 @@ import Line from "./Line.vue";
import Mattermost from "./Mattermost.vue"; import Mattermost from "./Mattermost.vue";
import Matrix from "./Matrix.vue"; import Matrix from "./Matrix.vue";
import AliyunSMS from "./AliyunSms.vue"; import AliyunSMS from "./AliyunSms.vue";
import DingDing from "./DingDing.vue";
/** /**
* Manage all notification form. * Manage all notification form.
@ -47,6 +48,7 @@ const NotificationFormList = {
"line": Line, "line": Line,
"mattermost": Mattermost, "mattermost": Mattermost,
"matrix": Matrix, "matrix": Matrix,
"DingDing":DingDing
} }
export default NotificationFormList export default NotificationFormList