From e8e7c7afb0998c757b0dc6d1daccbe078d7cbf0d Mon Sep 17 00:00:00 2001 From: edo2313 <32812884+edo2313@users.noreply.github.com> Date: Fri, 5 Jan 2024 00:06:10 +0100 Subject: [PATCH 1/6] Add Whapi notification provider --- server/notification-providers/whapi.js | 46 ++++++++++++++++++++++++++ server/notification.js | 4 ++- src/components/NotificationDialog.vue | 3 +- src/components/notifications/Whapi.vue | 23 +++++++++++++ src/components/notifications/index.js | 4 ++- src/lang/en.json | 3 +- 6 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 server/notification-providers/whapi.js create mode 100644 src/components/notifications/Whapi.vue diff --git a/server/notification-providers/whapi.js b/server/notification-providers/whapi.js new file mode 100644 index 0000000000..8153753ac2 --- /dev/null +++ b/server/notification-providers/whapi.js @@ -0,0 +1,46 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); + +class Whapi extends NotificationProvider { + + name = "whapi"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + + let okMsg = "Sent Successfully."; + + let apiUrl = notification.whapiApiUrl; + let apiToken = notification.whapiAuthToken; + let toNumber = notification.whapiToNumber; + + try { + + let config = { + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": "Bearer " + apiToken, + } + }; + + let data = { + "to": toNumber + "@s.whatsapp.net", + "body": msg, + }; + + let url = apiUrl + "/messages/text"; + + await axios.post(url, data, config); + + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + +} + +module.exports = Whapi; diff --git a/server/notification.js b/server/notification.js index 5e76d6eb1b..1c2abec1f8 100644 --- a/server/notification.js +++ b/server/notification.js @@ -54,6 +54,7 @@ const GoAlert = require("./notification-providers/goalert"); const SMSManager = require("./notification-providers/smsmanager"); const ServerChan = require("./notification-providers/serverchan"); const ZohoCliq = require("./notification-providers/zoho-cliq"); +const Whapi = require("./notification-providers/whapi"); class Notification { @@ -124,7 +125,8 @@ class Notification { new Webhook(), new WeCom(), new GoAlert(), - new ZohoCliq() + new ZohoCliq(), + new Whapi(), ]; for (let item of list) { if (! item.name) { diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 57a2fdf2db..0377847ea3 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -151,7 +151,8 @@ export default { "Splunk": "Splunk", "webhook": "Webhook", "GoAlert": "GoAlert", - "ZohoCliq": "ZohoCliq" + "ZohoCliq": "ZohoCliq", + "whapi": "Whapi", }; // Put notifications here if it's not supported in most regions or its documentation is not in English diff --git a/src/components/notifications/Whapi.vue b/src/components/notifications/Whapi.vue new file mode 100644 index 0000000000..ff348791ee --- /dev/null +++ b/src/components/notifications/Whapi.vue @@ -0,0 +1,23 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 0606d41af6..b2fe455608 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -52,6 +52,7 @@ import WeCom from "./WeCom.vue"; import GoAlert from "./GoAlert.vue"; import ZohoCliq from "./ZohoCliq.vue"; import Splunk from "./Splunk.vue"; +import Whapi from "./Whapi.vue"; /** * Manage all notification form. @@ -111,7 +112,8 @@ const NotificationFormList = { "WeCom": WeCom, "GoAlert": GoAlert, "ServerChan": ServerChan, - "ZohoCliq": ZohoCliq + "ZohoCliq": ZohoCliq, + "whapi": Whapi, }; export default NotificationFormList; diff --git a/src/lang/en.json b/src/lang/en.json index c003de74f5..8f63323ea1 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -882,5 +882,6 @@ "useRemoteBrowser": "Use a Remote Browser", "deleteRemoteBrowserMessage": "Are you sure want to delete this Remote Browser for all monitors?", "GrafanaOncallUrl": "Grafana Oncall URL", - "Browser Screenshot": "Browser Screenshot" + "Browser Screenshot": "Browser Screenshot", + "wayToWriteWhapiPhoneNumber": "The phone number with the international prefix, but without the plus sign at the start" } From 5d6e6cd8e66956a346d894ef98a368d6e40fa260 Mon Sep 17 00:00:00 2001 From: edo2313 <32812884+edo2313@users.noreply.github.com> Date: Fri, 5 Jan 2024 00:07:30 +0100 Subject: [PATCH 2/6] Fix regex for Whapi phone number --- src/components/notifications/Whapi.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/notifications/Whapi.vue b/src/components/notifications/Whapi.vue index ff348791ee..b7ede0869a 100644 --- a/src/components/notifications/Whapi.vue +++ b/src/components/notifications/Whapi.vue @@ -11,7 +11,7 @@
- +
From 929f62cb686371112ab17ce7b847a708ccfc53fb Mon Sep 17 00:00:00 2001 From: edo2313 <32812884+edo2313@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:08:57 +0100 Subject: [PATCH 3/6] Added translation strings, made the API work with group IDs and fixed the HTML --- server/notification-providers/whapi.js | 12 ++++------- src/components/notifications/Whapi.vue | 28 +++++++++++++++++--------- src/lang/en.json | 5 ++++- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/server/notification-providers/whapi.js b/server/notification-providers/whapi.js index 8153753ac2..a13cb02849 100644 --- a/server/notification-providers/whapi.js +++ b/server/notification-providers/whapi.js @@ -12,26 +12,22 @@ class Whapi extends NotificationProvider { let okMsg = "Sent Successfully."; - let apiUrl = notification.whapiApiUrl; - let apiToken = notification.whapiAuthToken; - let toNumber = notification.whapiToNumber; - try { - let config = { + const config = { headers: { "Accept": "application/json", "Content-Type": "application/json", - "Authorization": "Bearer " + apiToken, + "Authorization": "Bearer " + notification.whapiAuthToken, } }; let data = { - "to": toNumber + "@s.whatsapp.net", + "to": notification.whapiRecipient, "body": msg, }; - let url = apiUrl + "/messages/text"; + let url = notification.whapiApiUrl + "/messages/text"; await axios.post(url, data, config); diff --git a/src/components/notifications/Whapi.vue b/src/components/notifications/Whapi.vue index b7ede0869a..d308cfce66 100644 --- a/src/components/notifications/Whapi.vue +++ b/src/components/notifications/Whapi.vue @@ -6,18 +6,28 @@
- + + + https://panel.whapi.cloud/dashboard +
- - - + + +
{{ $t("wayToWriteWhapiRecipient", ["00117612345678", "00117612345678@s.whatsapp.net", "123456789012345678@g.us"]) }}
-
- - https://whapi.cloud/ - -
+ + https://whapi.cloud/ + + diff --git a/src/lang/en.json b/src/lang/en.json index 8f63323ea1..683bb51bd9 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -883,5 +883,8 @@ "deleteRemoteBrowserMessage": "Are you sure want to delete this Remote Browser for all monitors?", "GrafanaOncallUrl": "Grafana Oncall URL", "Browser Screenshot": "Browser Screenshot", - "wayToWriteWhapiPhoneNumber": "The phone number with the international prefix, but without the plus sign at the start" + "wayToWriteWhapiRecipient": "The phone number with the international prefix, but without the plus sign at the start ({0}), the Contact ID ({1}) or the Group ID ({2}).", + "wayToGetWhapiUrlAndToken": "You can get the API URL and the token by going into your desired channel from {0}", + "whapiRecipient": "Phone Number / Contact ID / Group ID", + "API URL": "API URL" } From e0743ce88bf3469182cdf80e4c30cbccecbecfc4 Mon Sep 17 00:00:00 2001 From: edo2313 <32812884+edo2313@users.noreply.github.com> Date: Tue, 9 Jan 2024 00:36:59 +0100 Subject: [PATCH 4/6] Added placeholder and use default url if not specified --- server/notification-providers/whapi.js | 2 +- src/components/notifications/Whapi.vue | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/notification-providers/whapi.js b/server/notification-providers/whapi.js index a13cb02849..2d1c289ded 100644 --- a/server/notification-providers/whapi.js +++ b/server/notification-providers/whapi.js @@ -27,7 +27,7 @@ class Whapi extends NotificationProvider { "body": msg, }; - let url = notification.whapiApiUrl + "/messages/text"; + let url = (notification.whapiApiUrl || "https://gate.whapi.cloud/").replace(/\/+$/, "") + "/messages/text"; await axios.post(url, data, config); diff --git a/src/components/notifications/Whapi.vue b/src/components/notifications/Whapi.vue index d308cfce66..4c92ad2c3a 100644 --- a/src/components/notifications/Whapi.vue +++ b/src/components/notifications/Whapi.vue @@ -1,7 +1,7 @@