Louis Lam
3 years ago
committed by
GitHub
7 changed files with 237 additions and 1 deletions
@ -0,0 +1,108 @@ |
|||||
|
const NotificationProvider = require("./notification-provider"); |
||||
|
const { DOWN, UP } = require("../../src/util"); |
||||
|
const { default: axios } = require("axios"); |
||||
|
const Crypto = require("crypto"); |
||||
|
const qs = require("qs"); |
||||
|
|
||||
|
class AliyunSMS extends NotificationProvider { |
||||
|
name = "AliyunSMS"; |
||||
|
|
||||
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { |
||||
|
let okMsg = "Sent Successfully."; |
||||
|
|
||||
|
try { |
||||
|
if (heartbeatJSON != null) { |
||||
|
let msgBody = JSON.stringify({ |
||||
|
name: monitorJSON["name"], |
||||
|
time: heartbeatJSON["time"], |
||||
|
status: this.statusToString(heartbeatJSON["status"]), |
||||
|
msg: heartbeatJSON["msg"], |
||||
|
}); |
||||
|
if (this.sendSms(notification, msgBody)) { |
||||
|
return okMsg; |
||||
|
} |
||||
|
} else { |
||||
|
let msgBody = JSON.stringify({ |
||||
|
name: "", |
||||
|
time: "", |
||||
|
status: "", |
||||
|
msg: msg, |
||||
|
}); |
||||
|
if (this.sendSms(notification, msgBody)) { |
||||
|
return okMsg; |
||||
|
} |
||||
|
} |
||||
|
} catch (error) { |
||||
|
this.throwGeneralAxiosError(error); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
async sendSms(notification, msgbody) { |
||||
|
let params = { |
||||
|
PhoneNumbers: notification.phonenumber, |
||||
|
TemplateCode: notification.templateCode, |
||||
|
SignName: notification.signName, |
||||
|
TemplateParam: msgbody, |
||||
|
AccessKeyId: notification.accessKeyId, |
||||
|
Format: "JSON", |
||||
|
SignatureMethod: "HMAC-SHA1", |
||||
|
SignatureVersion: "1.0", |
||||
|
SignatureNonce: Math.random().toString(), |
||||
|
Timestamp: new Date().toISOString(), |
||||
|
Action: "SendSms", |
||||
|
Version: "2017-05-25", |
||||
|
}; |
||||
|
|
||||
|
params.Signature = this.sign(params, notification.secretAccessKey); |
||||
|
let config = { |
||||
|
method: "POST", |
||||
|
url: "http://dysmsapi.aliyuncs.com/", |
||||
|
headers: { |
||||
|
"Content-Type": "application/x-www-form-urlencoded", |
||||
|
}, |
||||
|
data: qs.stringify(params), |
||||
|
}; |
||||
|
|
||||
|
let result = await axios(config); |
||||
|
if (result.data.Message == "OK") { |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/** Aliyun request sign */ |
||||
|
sign(param, AccessKeySecret) { |
||||
|
let param2 = {}; |
||||
|
let data = []; |
||||
|
|
||||
|
let oa = Object.keys(param).sort(); |
||||
|
|
||||
|
for (let i = 0; i < oa.length; i++) { |
||||
|
let key = oa[i]; |
||||
|
param2[key] = param[key]; |
||||
|
} |
||||
|
|
||||
|
for (let key in param2) { |
||||
|
data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`); |
||||
|
} |
||||
|
|
||||
|
let StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`; |
||||
|
return Crypto |
||||
|
.createHmac("sha1", `${AccessKeySecret}&`) |
||||
|
.update(Buffer.from(StringToSign)) |
||||
|
.digest("base64"); |
||||
|
} |
||||
|
|
||||
|
statusToString(status) { |
||||
|
switch (status) { |
||||
|
case DOWN: |
||||
|
return "DOWN"; |
||||
|
case UP: |
||||
|
return "UP"; |
||||
|
default: |
||||
|
return status; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = AliyunSMS; |
@ -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) { |
||||
|
let 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 { |
||||
|
let params = { |
||||
|
msgtype: "text", |
||||
|
text: { |
||||
|
content: msg |
||||
|
} |
||||
|
}; |
||||
|
if (this.sendToDingDing(notification, params)) { |
||||
|
return okMsg; |
||||
|
} |
||||
|
} |
||||
|
} catch (error) { |
||||
|
this.throwGeneralAxiosError(error); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
async sendToDingDing(notification, params) { |
||||
|
let timestamp = Date.now(); |
||||
|
|
||||
|
let config = { |
||||
|
method: "POST", |
||||
|
headers: { |
||||
|
"Content-Type": "application/json", |
||||
|
}, |
||||
|
url: `${notification.webHookUrl}×tamp=${timestamp}&sign=${encodeURIComponent(this.sign(timestamp, notification.secretKey))}`, |
||||
|
data: JSON.stringify(params), |
||||
|
}; |
||||
|
|
||||
|
let 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; |
@ -0,0 +1,25 @@ |
|||||
|
<template> |
||||
|
<div class="mb-3"> |
||||
|
<label for="accessKeyId" class="form-label">{{ $t("AccessKeyId") }}<span style="color: red;"><sup>*</sup></span></label> |
||||
|
<input id="accessKeyId" v-model="$parent.notification.accessKeyId" type="text" class="form-control" required> |
||||
|
|
||||
|
<label for="secretAccessKey" class="form-label">{{ $t("SecretAccessKey") }}<span style="color: red;"><sup>*</sup></span></label> |
||||
|
<input id="secretAccessKey" v-model="$parent.notification.secretAccessKey" type="text" class="form-control" required> |
||||
|
|
||||
|
<label for="phonenumber" class="form-label">{{ $t("Phonenumber") }}<span style="color: red;"><sup>*</sup></span></label> |
||||
|
<input id="phonenumber" v-model="$parent.notification.phonenumber" type="text" class="form-control" required> |
||||
|
|
||||
|
<label for="templateCode" class="form-label">{{ $t("TemplateCode") }}<span style="color: red;"><sup>*</sup></span></label> |
||||
|
<input id="templateCode" v-model="$parent.notification.templateCode" type="text" class="form-control" required> |
||||
|
|
||||
|
<label for="signName" class="form-label">{{ $t("SignName") }}<span style="color: red;"><sup>*</sup></span></label> |
||||
|
<input id="signName" v-model="$parent.notification.signName" type="text" class="form-control" required> |
||||
|
|
||||
|
<div class="form-text"> |
||||
|
<p>Sms template must contain parameters: <br> <code>${name} ${time} ${status} ${msg}</code></p> |
||||
|
<i18n-t tag="p" keypath="Read more:"> |
||||
|
<a href="https://help.aliyun.com/document_detail/101414.html" target="_blank">https://help.aliyun.com/document_detail/101414.html</a> |
||||
|
</i18n-t> |
||||
|
</div> |
||||
|
</div> |
||||
|
</template> |
@ -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> |
Loading…
Reference in new issue