Add aliyun sms notification

This commit is contained in:
wuwenjing
2021-10-13 11:55:01 +08:00
parent 1e5ce92917
commit a2f2253221
5 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
const Core = require("@alicloud/pop-core");
class AliyunSMS extends NotificationProvider {
name = "AliyunSMS";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
try {
var client = new Core({
accessKeyId: notification.accessKeyId,
accessKeySecret: notification.secretAccessKey,
endpoint: "https://dysmsapi.aliyuncs.com",
apiVersion: "2017-05-25",
});
var params = {
PhoneNumbers: notification.phonenumber,
TemplateCode: notification.templateCode,
SignName: notification.signName,
TemplateParam: JSON.stringify({
name: "",
time: "",
status: "",
msg: msg,
}),
};
if (heartbeatJSON != null) {
params.TemplateParam = JSON.stringify({
name: monitorJSON["name"],
time: heartbeatJSON["time"],
status: this.statusToString(heartbeatJSON["status"]),
msg: heartbeatJSON["msg"],
});
}
var requestOption = {
method: "POST",
};
await client.request("SendSms", params, requestOption).then(
(result) => {
console.log(JSON.stringify(result));
return okMsg;
},
(ex) => {
console.log(ex);
}
);
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
statusToString(status) {
switch (status) {
case DOWN:
return "DOWN";
case UP:
return "UP";
default:
return status;
}
}
}
module.exports = AliyunSMS;

View File

@@ -19,6 +19,7 @@ const Teams = require("./notification-providers/teams");
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");
class Notification {
@@ -31,6 +32,7 @@ class Notification {
const list = [
new Apprise(),
new AliyunSms(),
new Discord(),
new Teams(),
new Gotify(),