diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js new file mode 100644 index 000000000..749647d06 --- /dev/null +++ b/server/notification-providers/zoho-cliq.js @@ -0,0 +1,116 @@ +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 = []; + payload.push("### 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); + }; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully."; + + try { + if (heartbeatJSON == null) { + await this._handleGeneralNotification(notification.webhookUrl, msg); + 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: url, + 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 e760f92ea..653f205ae 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",