From a2f22532213353da4dd5b6d1b543602102ddc7a6 Mon Sep 17 00:00:00 2001 From: wuwenjing Date: Wed, 13 Oct 2021 11:55:01 +0800 Subject: [PATCH] Add aliyun sms notification --- package.json | 1 + server/notification-providers/aliyun-sms.js | 70 +++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/AliyunSms.vue | 24 +++++++ src/components/notifications/index.js | 2 + 5 files changed, 99 insertions(+) create mode 100644 server/notification-providers/aliyun-sms.js create mode 100644 src/components/notifications/AliyunSms.vue diff --git a/package.json b/package.json index 03112518..1dd79e90 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix" }, "dependencies": { + "@alicloud/pop-core": "^1.7.10", "@fortawesome/fontawesome-svg-core": "~1.2.36", "@fortawesome/free-regular-svg-icons": "~5.15.4", "@fortawesome/free-solid-svg-icons": "~5.15.4", diff --git a/server/notification-providers/aliyun-sms.js b/server/notification-providers/aliyun-sms.js new file mode 100644 index 00000000..9111c429 --- /dev/null +++ b/server/notification-providers/aliyun-sms.js @@ -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; diff --git a/server/notification.js b/server/notification.js index 41a0063c..21bd2b1e 100644 --- a/server/notification.js +++ b/server/notification.js @@ -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(), diff --git a/src/components/notifications/AliyunSms.vue b/src/components/notifications/AliyunSms.vue new file mode 100644 index 00000000..07dca8bb --- /dev/null +++ b/src/components/notifications/AliyunSms.vue @@ -0,0 +1,24 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 140c1180..74fe0df3 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -18,6 +18,7 @@ import Pushbullet from "./Pushbullet.vue"; import Line from "./Line.vue"; import Mattermost from "./Mattermost.vue"; import Matrix from "./Matrix.vue"; +import AliyunSMS from "./AliyunSms.vue"; /** * Manage all notification form. @@ -40,6 +41,7 @@ const NotificationFormList = { "promosms": PromoSMS, "lunasea": LunaSea, "Feishu": Feishu, + "AliyunSMS":AliyunSMS, "apprise": Apprise, "pushbullet": Pushbullet, "line": Line,