From 82fb7b281622dfffdc0c549141b9ed8b40839004 Mon Sep 17 00:00:00 2001 From: Francesco M Date: Wed, 12 Jun 2024 08:46:48 +0200 Subject: [PATCH 1/4] improved zoho-cliq message format: - show PORT (cloned switchcase from discord,squadcast,servenio) - show monitorName in the first row to get on point also from messagge preview notification - avoid duplication of monitorName and monitorName in the message content --- server/notification-providers/zoho-cliq.js | 33 +++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index 10381c81f..d67d7722a 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -13,9 +13,9 @@ class ZohoCliq extends NotificationProvider { */ _statusMessageFactory = (status, monitorName) => { if (status === DOWN) { - return `🔴 Application [${monitorName}] went down\n`; + return `🔴 [${monitorName}] went down\n`; } else if (status === UP) { - return `✅ Application [${monitorName}] is back online\n`; + return `✅ [${monitorName}] is back online\n`; } return "Notification\n"; }; @@ -46,16 +46,11 @@ class ZohoCliq extends NotificationProvider { 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})`); + payload.push(`*URL:* ${monitorUrl}`); } return payload; @@ -87,24 +82,30 @@ class ZohoCliq extends NotificationProvider { return okMsg; } - let url; + let address; + switch (monitorJSON["type"]) { - case "http": - case "keywork": - url = monitorJSON["url"]; + case "ping": + address = monitorJSON["hostname"]; break; - case "docker": - url = monitorJSON["docker_host"]; + case "port": + case "dns": + case "gamedig": + case "steam": + address = monitorJSON["hostname"]; + if (monitorJSON["port"]) { + address += ":" + monitorJSON["port"]; + } break; default: - url = monitorJSON["hostname"]; + address = monitorJSON["url"]; break; } const payload = this._notificationPayloadFactory({ monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, - monitorUrl: url, + monitorUrl: address, status: heartbeatJSON.status }); From bd95ccdc64d90735e4662e6f24ce157a754e4b13 Mon Sep 17 00:00:00 2001 From: Francesco M Date: Wed, 12 Jun 2024 18:14:05 +0200 Subject: [PATCH 2/4] Update server/notification-providers/zoho-cliq.js Co-authored-by: Frank Elsinga --- server/notification-providers/zoho-cliq.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index d67d7722a..e741603ce 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -15,7 +15,7 @@ class ZohoCliq extends NotificationProvider { if (status === DOWN) { return `🔴 [${monitorName}] went down\n`; } else if (status === UP) { - return `✅ [${monitorName}] is back online\n`; + return `### ✅ [${monitorName}] is back online\n`; } return "Notification\n"; }; From c9132adfc709d8fc0549e39dd98a53c8f77041d2 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 13 Jun 2024 17:00:11 +0200 Subject: [PATCH 3/4] made sure that the address extraction is extracted into its own method --- server/notification-providers/discord.js | 22 +------------ .../notification-provider.js | 31 +++++++++++++++++++ server/notification-providers/sevenio.js | 23 +------------- server/notification-providers/smtp.js | 7 +---- server/notification-providers/squadcast.js | 20 +----------- server/notification-providers/teams.js | 17 +--------- server/notification-providers/zoho-cliq.js | 18 ----------- 7 files changed, 36 insertions(+), 102 deletions(-) diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index 129e334b0..38b88fe94 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -33,26 +33,6 @@ class Discord extends NotificationProvider { return okMsg; } - let address; - - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } - // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] === DOWN) { let discorddowndata = { @@ -105,7 +85,7 @@ class Discord extends NotificationProvider { }, { name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL", - value: monitorJSON["type"] === "push" ? "Heartbeat" : address, + value: this.extractAdress(monitorJSON), }, { name: `Time (${heartbeatJSON["timezone"]})`, diff --git a/server/notification-providers/notification-provider.js b/server/notification-providers/notification-provider.js index 9b4f0bb05..1a3a36125 100644 --- a/server/notification-providers/notification-provider.js +++ b/server/notification-providers/notification-provider.js @@ -19,6 +19,37 @@ class NotificationProvider { throw new Error("Have to override Notification.send(...)"); } + /** + * Extracts the address from a monitor JSON object based on its type. + * + * @param {?object} monitorJSON Monitor details (For Up/Down only) + * @returns {string} The extracted address based on the monitor type. + */ + extractAdress(monitorJSON){ + if (!monitorJSON) { + return "" + } + switch (monitorJSON["type"]) { + case "push": + return "Heartbeat"; + case "ping": + return monitorJSON["hostname"]; + case "port": + case "dns": + case "gamedig": + case "steam": + if (monitorJSON["port"]) { + return monitorJSON["hostname"] + ":" + monitorJSON["port"]; + } + return monitorJSON["hostname"] + default: + if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) { + return monitorJSON["url"]; + } + return ""; + } + } + /** * Throws an error * @param {any} error The error to throw diff --git a/server/notification-providers/sevenio.js b/server/notification-providers/sevenio.js index d363e95d0..e422ff1fb 100644 --- a/server/notification-providers/sevenio.js +++ b/server/notification-providers/sevenio.js @@ -32,28 +32,7 @@ class SevenIO extends NotificationProvider { return okMsg; } - let address = ""; - - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) { - address = monitorJSON["url"]; - } - break; - } - + let address = extractAdress(monitorJSON); if (address !== "") { address = `(${address}) `; } diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index b403117b4..801e0e69d 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -93,12 +93,7 @@ class SMTP extends NotificationProvider { if (monitorJSON !== null) { monitorName = monitorJSON["name"]; - - if (monitorJSON["type"] === "http" || monitorJSON["type"] === "keyword" || monitorJSON["type"] === "json-query") { - monitorHostnameOrURL = monitorJSON["url"]; - } else { - monitorHostnameOrURL = monitorJSON["hostname"]; - } + monitorHostnameOrURL = this.extractAdress(monitorJSON); } let serviceStatus = "⚠️ Test"; diff --git a/server/notification-providers/squadcast.js b/server/notification-providers/squadcast.js index 1c653d141..9074e9b76 100644 --- a/server/notification-providers/squadcast.js +++ b/server/notification-providers/squadcast.js @@ -34,25 +34,7 @@ class Squadcast extends NotificationProvider { data.status = "resolve"; } - let address; - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } - - data.tags["AlertAddress"] = address; + data.tags["AlertAddress"] = this.extractAdress(monitorJSON); monitorJSON["tags"].forEach(tag => { data.tags[tag["name"]] = { diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index f34ba1ced..2fcea7077 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -216,21 +216,6 @@ class Teams extends NotificationProvider { return okMsg; } - let monitorUrl; - - switch (monitorJSON["type"]) { - case "http": - case "keywork": - monitorUrl = monitorJSON["url"]; - break; - case "docker": - monitorUrl = monitorJSON["docker_host"]; - break; - default: - monitorUrl = monitorJSON["hostname"]; - break; - } - const baseURL = await setting("primaryBaseURL"); let dashboardUrl; if (baseURL) { @@ -240,7 +225,7 @@ class Teams extends NotificationProvider { const payload = this._notificationPayloadFactory({ heartbeatJSON: heartbeatJSON, monitorName: monitorJSON.name, - monitorUrl: monitorUrl, + monitorUrl: this.extractAdress(monitorJSON), dashboardUrl: dashboardUrl, }); diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index e741603ce..76a059683 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -82,25 +82,7 @@ class ZohoCliq extends NotificationProvider { return okMsg; } - let address; - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } const payload = this._notificationPayloadFactory({ monitorMessage: heartbeatJSON.msg, From 9f563adc1a1f1ec5d714b65f96fab4f23187e255 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 13 Jun 2024 17:04:38 +0200 Subject: [PATCH 4/4] fixed formatting mistakes --- server/notification-providers/discord.js | 2 +- server/notification-providers/notification-provider.js | 7 +++---- server/notification-providers/sevenio.js | 2 +- server/notification-providers/zoho-cliq.js | 4 +--- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index 38b88fe94..ccb80bf40 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -48,7 +48,7 @@ class Discord extends NotificationProvider { }, { name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL", - value: monitorJSON["type"] === "push" ? "Heartbeat" : address, + value: this.extractAdress(monitorJSON), }, { name: `Time (${heartbeatJSON["timezone"]})`, diff --git a/server/notification-providers/notification-provider.js b/server/notification-providers/notification-provider.js index 1a3a36125..d26b791f6 100644 --- a/server/notification-providers/notification-provider.js +++ b/server/notification-providers/notification-provider.js @@ -21,13 +21,12 @@ class NotificationProvider { /** * Extracts the address from a monitor JSON object based on its type. - * * @param {?object} monitorJSON Monitor details (For Up/Down only) * @returns {string} The extracted address based on the monitor type. */ - extractAdress(monitorJSON){ + extractAdress(monitorJSON) { if (!monitorJSON) { - return "" + return ""; } switch (monitorJSON["type"]) { case "push": @@ -41,7 +40,7 @@ class NotificationProvider { if (monitorJSON["port"]) { return monitorJSON["hostname"] + ":" + monitorJSON["port"]; } - return monitorJSON["hostname"] + return monitorJSON["hostname"]; default: if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) { return monitorJSON["url"]; diff --git a/server/notification-providers/sevenio.js b/server/notification-providers/sevenio.js index e422ff1fb..9d805d7e6 100644 --- a/server/notification-providers/sevenio.js +++ b/server/notification-providers/sevenio.js @@ -32,7 +32,7 @@ class SevenIO extends NotificationProvider { return okMsg; } - let address = extractAdress(monitorJSON); + let address = this.extractAdress(monitorJSON); if (address !== "") { address = `(${address}) `; } diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index 76a059683..44681b7df 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -82,12 +82,10 @@ class ZohoCliq extends NotificationProvider { return okMsg; } - - const payload = this._notificationPayloadFactory({ monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, - monitorUrl: address, + monitorUrl: this.extractAdress(monitorJSON), status: heartbeatJSON.status });