2022-12-08 06:32:10 -05:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
2022-12-08 06:53:02 -05:00
|
|
|
const { DOWN, UP } = require("../../src/util");
|
2022-12-08 06:32:10 -05:00
|
|
|
|
|
|
|
class ZohoCliq extends NotificationProvider {
|
|
|
|
name = "ZohoCliq";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the message to send
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @param {string} monitorName Name of monitor
|
2023-08-11 03:46:41 -04:00
|
|
|
* @returns {string} Status message
|
2022-12-08 06:32:10 -05:00
|
|
|
*/
|
|
|
|
_statusMessageFactory = (status, monitorName) => {
|
|
|
|
if (status === DOWN) {
|
2024-06-12 02:46:48 -04:00
|
|
|
return `🔴 [${monitorName}] went down\n`;
|
2022-12-08 06:32:10 -05:00
|
|
|
} else if (status === UP) {
|
2024-06-12 02:46:48 -04:00
|
|
|
return `✅ [${monitorName}] is back online\n`;
|
2022-12-08 06:32:10 -05:00
|
|
|
}
|
|
|
|
return "Notification\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the notification
|
|
|
|
* @param {string} webhookUrl URL to send the request to
|
|
|
|
* @param {Array} payload Payload generated by _notificationPayloadFactory
|
2023-08-11 03:46:41 -04:00
|
|
|
* @returns {Promise<void>}
|
2022-12-08 06:32:10 -05:00
|
|
|
*/
|
2022-12-08 06:56:02 -05:00
|
|
|
_sendNotification = async (webhookUrl, payload) => {
|
|
|
|
await axios.post(webhookUrl, { text: payload.join("\n") });
|
2022-12-08 06:32:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate payload for notification
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {object} args Method arguments
|
|
|
|
* @param {const} args.status The status of the monitor
|
|
|
|
* @param {string} args.monitorMessage Message to send
|
|
|
|
* @param {string} args.monitorName Name of monitor affected
|
|
|
|
* @param {string} args.monitorUrl URL of monitor affected
|
|
|
|
* @returns {Array} Notification payload
|
2022-12-08 06:32:10 -05:00
|
|
|
*/
|
|
|
|
_notificationPayloadFactory = ({
|
2022-12-08 06:56:02 -05:00
|
|
|
status,
|
|
|
|
monitorMessage,
|
|
|
|
monitorName,
|
|
|
|
monitorUrl,
|
2022-12-08 06:41:05 -05:00
|
|
|
}) => {
|
2022-12-08 06:53:02 -05:00
|
|
|
const payload = [];
|
2022-12-08 06:32:10 -05:00
|
|
|
payload.push(this._statusMessageFactory(status, monitorName));
|
|
|
|
payload.push(`*Description:* ${monitorMessage}`);
|
|
|
|
|
|
|
|
if (monitorUrl && monitorUrl !== "https://") {
|
2024-06-12 02:46:48 -04:00
|
|
|
payload.push(`*URL:* ${monitorUrl}`);
|
2022-12-08 06:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a general notification
|
|
|
|
* @param {string} webhookUrl URL to send request to
|
|
|
|
* @param {string} msg Message to send
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
_handleGeneralNotification = (webhookUrl, msg) => {
|
|
|
|
const payload = this._notificationPayloadFactory({
|
|
|
|
monitorMessage: msg
|
|
|
|
});
|
|
|
|
|
|
|
|
return this._sendNotification(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
2023-08-11 03:46:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-12-08 06:32:10 -05:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 09:21:15 -04:00
|
|
|
const okMsg = "Sent Successfully.";
|
2022-12-08 06:32:10 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
await this._handleGeneralNotification(notification.webhookUrl, msg);
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:46:48 -04:00
|
|
|
let address;
|
|
|
|
|
2022-12-08 06:53:02 -05:00
|
|
|
switch (monitorJSON["type"]) {
|
2024-06-12 02:46:48 -04:00
|
|
|
case "ping":
|
|
|
|
address = monitorJSON["hostname"];
|
2022-12-08 06:41:05 -05:00
|
|
|
break;
|
2024-06-12 02:46:48 -04:00
|
|
|
case "port":
|
|
|
|
case "dns":
|
|
|
|
case "gamedig":
|
|
|
|
case "steam":
|
|
|
|
address = monitorJSON["hostname"];
|
|
|
|
if (monitorJSON["port"]) {
|
|
|
|
address += ":" + monitorJSON["port"];
|
|
|
|
}
|
2022-12-08 06:41:05 -05:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-12 02:46:48 -04:00
|
|
|
address = monitorJSON["url"];
|
2022-12-08 06:41:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-08 06:32:10 -05:00
|
|
|
const payload = this._notificationPayloadFactory({
|
|
|
|
monitorMessage: heartbeatJSON.msg,
|
|
|
|
monitorName: monitorJSON.name,
|
2024-06-12 02:46:48 -04:00
|
|
|
monitorUrl: address,
|
2022-12-08 06:53:02 -05:00
|
|
|
status: heartbeatJSON.status
|
2022-12-08 06:32:10 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
await this._sendNotification(notification.webhookUrl, payload);
|
|
|
|
return okMsg;
|
|
|
|
|
2022-12-08 06:56:02 -05:00
|
|
|
} catch (error) {
|
2022-12-08 06:32:10 -05:00
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ZohoCliq;
|