From 73bfdb9ef9d3d970338086c8f339c84389e09a99 Mon Sep 17 00:00:00 2001 From: panos Date: Thu, 8 Dec 2022 13:32:10 +0200 Subject: [PATCH] zoho cliq notification provider --- server/notification-providers/zoho-cliq.js | 119 +++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/ZohoCliq.vue | 18 ++++ src/components/notifications/index.js | 2 + src/languages/el-GR.js | 2 + src/languages/en.js | 2 + src/languages/eu.js | 2 + 7 files changed, 147 insertions(+) create mode 100644 server/notification-providers/zoho-cliq.js create mode 100644 src/components/notifications/ZohoCliq.vue diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js new file mode 100644 index 00000000..d944089d --- /dev/null +++ b/server/notification-providers/zoho-cliq.js @@ -0,0 +1,119 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const {DOWN, UP} = require("../../src/util"); + +class ZohoCliq extends NotificationProvider { + + name = "ZohoCliq"; + + /** + * Generate the message to send + * @param {const} status The status constant + * @param {string} monitorName Name of monitor + * @returns {string} + */ + _statusMessageFactory = (status, monitorName) => { + if (status === DOWN) { + return `🔴 Application [${monitorName}] went down\n`; + } else if (status === UP) { + return `✅ Application [${monitorName}] is back online\n`; + } + return "Notification\n"; + }; + + /** + * Send the notification + * @param {string} webhookUrl URL to send the request to + * @param {Array} payload Payload generated by _notificationPayloadFactory + */ + _sendNotification = async(webhookUrl, payload) => { + await axios.post(webhookUrl, {text: payload.join("\n")}); + }; + + /** + * Generate payload for notification + * @param {const} status The status of the monitor + * @param {string} monitorMessage Message to send + * @param {string} monitorName Name of monitor affected + * @param {string} monitorUrl URL of monitor affected + * @returns {Array} + */ + _notificationPayloadFactory = ({ + status, + monitorMessage, + monitorName, + monitorUrl, + }) => { + + const payload = ["### Uptime Kuma\n"]; + payload.push(this._statusMessageFactory(status, monitorName)); + payload.push(`*Description:* ${monitorMessage}`); + + if (monitorName) { + payload.push(`*Monitor:* ${monitorName}`); + } + + if (monitorUrl && monitorUrl !== "https://") { + payload.push(`*URL:* [${monitorUrl}](${monitorUrl})`); + } + + return payload; + }; + + /** + * Send a general notification + * @param {string} webhookUrl URL to send request to + * @param {string} msg Message to send + * @returns {Promise} + */ + _handleGeneralNotification = (webhookUrl, msg) => { + const payload = this._notificationPayloadFactory({ + monitorMessage: msg + }); + + return this._sendNotification(webhookUrl, payload); + }; + + _monitorUrlFactory = (monitorJSON) => { + let url; + switch(monitorJSON["type"]) { + case "http": + case "keywork": + url = monitorJSON["url"]; + break; + case "docker": + url = monitorJSON["docker_host"]; + break; + default: + url = monitorJSON["hostname"]; + break; + } + return url; + }; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully."; + + try { + if (heartbeatJSON == null) { + await this._handleGeneralNotification(notification.webhookUrl, msg); + return okMsg; + } + + const payload = this._notificationPayloadFactory({ + monitorMessage: heartbeatJSON.msg, + monitorName: monitorJSON.name, + monitorUrl: this._monitorUrlFactory(monitorJSON), + status: heartbeatJSON.status, + }); + + await this._sendNotification(notification.webhookUrl, payload); + return okMsg; + + } catch(error) { + this.throwGeneralAxiosError(error); + } + } +} + +module.exports = ZohoCliq; diff --git a/server/notification.js b/server/notification.js index 9069601b..275f07b9 100644 --- a/server/notification.js +++ b/server/notification.js @@ -44,6 +44,7 @@ const WeCom = require("./notification-providers/wecom"); 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"); class Notification { @@ -100,6 +101,7 @@ class Notification { new Webhook(), new WeCom(), new GoAlert(), + new ZohoCliq() ]; for (let item of list) { diff --git a/src/components/notifications/ZohoCliq.vue b/src/components/notifications/ZohoCliq.vue new file mode 100644 index 00000000..9a9cd736 --- /dev/null +++ b/src/components/notifications/ZohoCliq.vue @@ -0,0 +1,18 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 0c220b71..86dad13e 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -42,6 +42,7 @@ import Telegram from "./Telegram.vue"; import Webhook from "./Webhook.vue"; import WeCom from "./WeCom.vue"; import GoAlert from "./GoAlert.vue"; +import ZohoCliq from "./ZohoCliq.vue"; /** * Manage all notification form. @@ -93,6 +94,7 @@ const NotificationFormList = { "WeCom": WeCom, "GoAlert": GoAlert, "ServerChan": ServerChan, + "ZohoCliq": ZohoCliq }; export default NotificationFormList; diff --git a/src/languages/el-GR.js b/src/languages/el-GR.js index c520a607..9b7c4cfb 100644 --- a/src/languages/el-GR.js +++ b/src/languages/el-GR.js @@ -194,6 +194,7 @@ export default { here: "εδώ", Required: "Απαιτείται", telegram: "Telegram", + "ZohoCliq": "ZohoCliq", "Bot Token": "Διακριτικό Bot", wayToGetTelegramToken: "Μπορείτε να πάρετε ένα διακριτικό από {0}.", "Chat ID": "Chat ID", @@ -224,6 +225,7 @@ export default { teams: "Microsoft Teams", "Webhook URL": "Webhook URL", wayToGetTeamsURL: "Μπορείτε να μάθετε πώς να δημιουργείτε μια διεύθυνση URL webhook {0}.", + wayToGetZohoCliqURL: "Μπορείτε να μάθετε πώς να δημιουργείτε μια διεύθυνση URL webhook {0}.", signal: "Signal", Number: "Αριθμός", Recipients: "Αποδέκτες", diff --git a/src/languages/en.js b/src/languages/en.js index e7de9648..106edf0e 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -209,6 +209,7 @@ export default { here: "here", Required: "Required", telegram: "Telegram", + "ZohoCliq": "ZohoCliq", "Bot Token": "Bot Token", wayToGetTelegramToken: "You can get a token from {0}.", "Chat ID": "Chat ID", @@ -241,6 +242,7 @@ export default { teams: "Microsoft Teams", "Webhook URL": "Webhook URL", wayToGetTeamsURL: "You can learn how to create a webhook URL {0}.", + wayToGetZohoCliqURL: "You can learn how to create a webhook URL {0}.", signal: "Signal", Number: "Number", Recipients: "Recipients", diff --git a/src/languages/eu.js b/src/languages/eu.js index c99f1eb7..a491c872 100644 --- a/src/languages/eu.js +++ b/src/languages/eu.js @@ -191,6 +191,7 @@ export default { here: "Hemen", Required: "Beharrezkoa", telegram: "Telegram", + "ZohoCliq": "ZohoCliq", "Bot Token": "Bot Tokena", wayToGetTelegramToken: "You can get a token from {0}.", "Chat ID": "Txat IDa", @@ -221,6 +222,7 @@ export default { teams: "Microsoft Teams", "Webhook URL": "Webhook URL", wayToGetTeamsURL: "You can learn how to create a webhook URL {0}.", + wayToGetZohoCliqURL: "You can learn how to create a webhook URL {0}.", signal: "Signal", Number: "Zenbakia", Recipients: "Recipients",