From 73bfdb9ef9d3d970338086c8f339c84389e09a99 Mon Sep 17 00:00:00 2001 From: panos Date: Thu, 8 Dec 2022 13:32:10 +0200 Subject: [PATCH 1/4] 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 000000000..d944089d4 --- /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 9069601b4..275f07b93 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 000000000..9a9cd7360 --- /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 0c220b717..86dad13e7 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 c520a6079..9b7c4cfb3 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 e7de9648b..106edf0e8 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 c99f1eb70..a491c8728 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", From 68bc7ac421baf0234ccf51059108dfb3d88cd943 Mon Sep 17 00:00:00 2001 From: panos Date: Thu, 8 Dec 2022 13:41:05 +0200 Subject: [PATCH 2/4] zoho cliq code style --- server/notification-providers/zoho-cliq.js | 36 ++++++++++------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index d944089d4..61fc68bec 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -43,8 +43,7 @@ class ZohoCliq extends NotificationProvider { monitorMessage, monitorName, monitorUrl, - }) => { - + }) => { const payload = ["### Uptime Kuma\n"]; payload.push(this._statusMessageFactory(status, monitorName)); payload.push(`*Description:* ${monitorMessage}`); @@ -74,23 +73,6 @@ class ZohoCliq extends NotificationProvider { 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."; @@ -100,10 +82,24 @@ class ZohoCliq extends NotificationProvider { return okMsg; } + 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; + } + const payload = this._notificationPayloadFactory({ monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, - monitorUrl: this._monitorUrlFactory(monitorJSON), + monitorUrl: url, status: heartbeatJSON.status, }); From 851a04b08215020c1c2d21418189f17c80b5ce44 Mon Sep 17 00:00:00 2001 From: panos Date: Thu, 8 Dec 2022 13:53:02 +0200 Subject: [PATCH 3/4] zoho cliq code style --- server/notification-providers/zoho-cliq.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index 61fc68bec..b7885be42 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -1,6 +1,6 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); -const {DOWN, UP} = require("../../src/util"); +const { DOWN, UP } = require("../../src/util"); class ZohoCliq extends NotificationProvider { @@ -44,7 +44,8 @@ class ZohoCliq extends NotificationProvider { monitorName, monitorUrl, }) => { - const payload = ["### Uptime Kuma\n"]; + const payload = []; + payload.push("### Uptime Kuma\n"); payload.push(this._statusMessageFactory(status, monitorName)); payload.push(`*Description:* ${monitorMessage}`); @@ -83,7 +84,7 @@ class ZohoCliq extends NotificationProvider { } let url; - switch(monitorJSON["type"]) { + switch (monitorJSON["type"]) { case "http": case "keywork": url = monitorJSON["url"]; @@ -100,7 +101,7 @@ class ZohoCliq extends NotificationProvider { monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, monitorUrl: url, - status: heartbeatJSON.status, + status: heartbeatJSON.status }); await this._sendNotification(notification.webhookUrl, payload); From 9da28fbbc75b16b0d90dd013057666b20be641c9 Mon Sep 17 00:00:00 2001 From: panos Date: Thu, 8 Dec 2022 13:56:02 +0200 Subject: [PATCH 4/4] zoho cliq code style --- server/notification-providers/zoho-cliq.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index b7885be42..749647d06 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -26,8 +26,8 @@ class ZohoCliq extends NotificationProvider { * @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")}); + _sendNotification = async (webhookUrl, payload) => { + await axios.post(webhookUrl, { text: payload.join("\n") }); }; /** @@ -39,10 +39,10 @@ class ZohoCliq extends NotificationProvider { * @returns {Array} */ _notificationPayloadFactory = ({ - status, - monitorMessage, - monitorName, - monitorUrl, + status, + monitorMessage, + monitorName, + monitorUrl, }) => { const payload = []; payload.push("### Uptime Kuma\n"); @@ -107,7 +107,7 @@ class ZohoCliq extends NotificationProvider { await this._sendNotification(notification.webhookUrl, payload); return okMsg; - } catch(error) { + } catch (error) { this.throwGeneralAxiosError(error); } }