From b64c835ceed723960b90c81b33141164bd1405bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Gen=C3=A7?= Date: Sat, 25 Mar 2023 19:56:01 +0300 Subject: [PATCH] Add Twilio Sms Notification Provider --- server/notification-providers/twilio.js | 41 +++++++++++++++++++++++++ server/notification.js | 2 ++ src/components/NotificationDialog.vue | 1 + src/components/notifications/Twilio.vue | 27 ++++++++++++++++ src/components/notifications/index.js | 2 ++ src/lang/en.json | 6 +++- 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 server/notification-providers/twilio.js create mode 100644 src/components/notifications/Twilio.vue diff --git a/server/notification-providers/twilio.js b/server/notification-providers/twilio.js new file mode 100644 index 000000000..8f4db0404 --- /dev/null +++ b/server/notification-providers/twilio.js @@ -0,0 +1,41 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); + +class Twilio extends NotificationProvider { + + name = "twilio"; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + + let okMsg = "Sent Successfully."; + + let accountSID = notification.twilioAccountSID; + let authToken = notification.twilioAuthToken; + + try { + + let config = { + headers: { + "Content-Type": "application/x-www-form-urlencoded;charset=utf-8", + "Authorization": "Basic " + Buffer.from(accountSID + ":" + authToken).toString("base64"), + } + }; + + let data = new URLSearchParams(); + data.append("To", notification.twilioToNumber); + data.append("From", notification.twilioFromNumber); + data.append("Body", msg); + + let url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSID + "/Messages.json"; + + await axios.post(url, data, config); + + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + +} + +module.exports = Twilio; diff --git a/server/notification.js b/server/notification.js index 1897f5cc0..68f675e94 100644 --- a/server/notification.js +++ b/server/notification.js @@ -41,6 +41,7 @@ const Stackfield = require("./notification-providers/stackfield"); const Teams = require("./notification-providers/teams"); const TechulusPush = require("./notification-providers/techulus-push"); const Telegram = require("./notification-providers/telegram"); +const Twilio = require("./notification-providers/twilio"); const Splunk = require("./notification-providers/splunk"); const Webhook = require("./notification-providers/webhook"); const WeCom = require("./notification-providers/wecom"); @@ -103,6 +104,7 @@ class Notification { new Teams(), new TechulusPush(), new Telegram(), + new Twilio(), new Splunk(), new Webhook(), new WeCom(), diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index c3851b568..557e75fc4 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -143,6 +143,7 @@ export default { "stackfield": "Stackfield", "teams": "Microsoft Teams", "telegram": "Telegram", + "twilio": "Twilio", "Splunk": "Splunk", "webhook": "Webhook", "GoAlert": "GoAlert", diff --git a/src/components/notifications/Twilio.vue b/src/components/notifications/Twilio.vue new file mode 100644 index 000000000..3edf1e3df --- /dev/null +++ b/src/components/notifications/Twilio.vue @@ -0,0 +1,27 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index ed9dde0f1..8017b622e 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -41,6 +41,7 @@ import STMP from "./SMTP.vue"; import Teams from "./Teams.vue"; import TechulusPush from "./TechulusPush.vue"; import Telegram from "./Telegram.vue"; +import Twilio from "./Twilio.vue"; import Webhook from "./Webhook.vue"; import WeCom from "./WeCom.vue"; import GoAlert from "./GoAlert.vue"; @@ -95,6 +96,7 @@ const NotificationFormList = { "stackfield": Stackfield, "teams": Teams, "telegram": Telegram, + "twilio": Twilio, "Splunk": Splunk, "webhook": Webhook, "WeCom": WeCom, diff --git a/src/lang/en.json b/src/lang/en.json index 0c9fc3840..e7656c474 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -707,5 +707,9 @@ "wayToGetPagerTreeIntegrationURL": "After creating the Uptime Kuma integration in PagerTree, copy the Endpoint. See full details {0}", "lunaseaTarget": "Target", "lunaseaDeviceID": "Device ID", - "lunaseaUserID": "User ID" + "lunaseaUserID": "User ID", + "twilioAccountSID": "Account SID", + "twilioAuthToken": "Auth Token", + "twilioFromNumber": "From Number", + "twilioToNumber": "To Number" }