From 9ade9af1e29979e8f81b461eda13f86560939d6a Mon Sep 17 00:00:00 2001 From: Niyas Date: Thu, 5 Aug 2021 21:41:11 +0530 Subject: [PATCH 1/9] Discord enhancements --- server/notification.js | 78 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/server/notification.js b/server/notification.js index f53b681a1..06926b315 100644 --- a/server/notification.js +++ b/server/notification.js @@ -83,41 +83,91 @@ class Notification { return await Notification.smtp(notification, msg) } else if (notification.type === "discord") { + let kumaURL = notification.discorduptimekumaUrl || "https://github.com/louislam/uptime-kuma"; + let dashboardURL = notification.discorduptimekumaUrl + '/dashboard/' + monitorJSON["id"]; try { // If heartbeatJSON is null, assume we're testing. if (heartbeatJSON == null) { - let data = { - username: "Uptime-Kuma", + let discordtestdata = { + username: "Uptime Kuma", content: msg, } - await axios.post(notification.discordWebhookUrl, data) + await axios.post(notification.discordWebhookUrl, discordtestdata) return okMsg; } // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] == 0) { - var alertColor = "16711680"; - } else if (heartbeatJSON["status"] == 1) { - var alertColor = "65280"; - } - let data = { - username: "Uptime-Kuma", + let discorddowndata = { + username: "Uptime Kuma", embeds: [{ - title: "Uptime-Kuma Alert", - color: alertColor, + title: "❌ One of your services went down. ❌", + color: 16711680, + timestamp: heartbeatJSON["time"], fields: [ + { + name: "Service Name", + value: monitorJSON["name"], + }, + { + name: "Service URL", + value: monitorJSON["url"], + }, { name: "Time (UTC)", value: heartbeatJSON["time"], }, { - name: "Message", - value: msg, + name: "Error", + value: heartbeatJSON["msg"], + }, + { + name: "Visit Service Dashboard", + value: "[Visit Dashboard]("+ dashboardURL + ")", + }, + { + name: "Visit Uptime Kuma", + value: "[Visit]("+ kumaURL +")", }, ], }], } - await axios.post(notification.discordWebhookUrl, data) + await axios.post(notification.discordWebhookUrl, discorddowndata) return okMsg; + + } else if (heartbeatJSON["status"] == 1) { + let discordupdata = { + username: "Uptime Kuma", + embeds: [{ + title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", + color: 65280, + timestamp: heartbeatJSON["time"], + fields: [ + { + name: "Service Name", + value: monitorJSON["name"], + }, + { + name: "Service URL", + value: "[Visit Service]("+ monitorJSON["url"] +")", + }, + { + name: "Time (UTC)", + value: heartbeatJSON["time"], + }, + { + name: "Ping", + value: heartbeatJSON["ping"] + "ms", + }, + { + name: "Visit Uptime Kuma", + value: "[Visit]("+ kumaURL +")", + }, + ], + }], + } + await axios.post(notification.discordWebhookUrl, discordupdata) + return okMsg; + } } catch (error) { throwGeneralAxiosError(error) } From 908176c910e59740efa8238961d73bd3d12ef28f Mon Sep 17 00:00:00 2001 From: Niyas Date: Thu, 5 Aug 2021 21:42:45 +0530 Subject: [PATCH 2/9] Discord enhancements --- src/components/NotificationDialog.vue | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 4eea85d66..3a80a9295 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -143,6 +143,8 @@
+ +
You can get this by going to Server Settings -> Integrations -> Create Webhook
From 3a7414125a6af1eb24167f8a7eddee1dd7c926f7 Mon Sep 17 00:00:00 2001 From: Niyas Date: Fri, 6 Aug 2021 17:37:22 +0530 Subject: [PATCH 3/9] Updated discord embeds --- server/notification.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/server/notification.js b/server/notification.js index 06926b315..bf4bac541 100644 --- a/server/notification.js +++ b/server/notification.js @@ -83,8 +83,6 @@ class Notification { return await Notification.smtp(notification, msg) } else if (notification.type === "discord") { - let kumaURL = notification.discorduptimekumaUrl || "https://github.com/louislam/uptime-kuma"; - let dashboardURL = notification.discorduptimekumaUrl + '/dashboard/' + monitorJSON["id"]; try { // If heartbeatJSON is null, assume we're testing. if (heartbeatJSON == null) { @@ -120,14 +118,6 @@ class Notification { name: "Error", value: heartbeatJSON["msg"], }, - { - name: "Visit Service Dashboard", - value: "[Visit Dashboard]("+ dashboardURL + ")", - }, - { - name: "Visit Uptime Kuma", - value: "[Visit]("+ kumaURL +")", - }, ], }], } @@ -158,10 +148,6 @@ class Notification { name: "Ping", value: heartbeatJSON["ping"] + "ms", }, - { - name: "Visit Uptime Kuma", - value: "[Visit]("+ kumaURL +")", - }, ], }], } From 91fce75a9354038c8cc60e1f18c818df89619c7b Mon Sep 17 00:00:00 2001 From: Niyas Date: Fri, 6 Aug 2021 17:38:16 +0530 Subject: [PATCH 4/9] Removed UptimeKuma url field --- src/components/NotificationDialog.vue | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 3a80a9295..4eea85d66 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -143,8 +143,6 @@
- -
You can get this by going to Server Settings -> Integrations -> Create Webhook
From ebf207c2f513d7d12872efdb9994a5bb333b7f2c Mon Sep 17 00:00:00 2001 From: Niyas Date: Sat, 7 Aug 2021 11:12:36 +0530 Subject: [PATCH 5/9] Custom embed username --- server/notification.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/notification.js b/server/notification.js index bf4bac541..65cccfbf4 100644 --- a/server/notification.js +++ b/server/notification.js @@ -87,7 +87,7 @@ class Notification { // If heartbeatJSON is null, assume we're testing. if (heartbeatJSON == null) { let discordtestdata = { - username: "Uptime Kuma", + username: notification.discordUsername || "Uptime Kuma", content: msg, } await axios.post(notification.discordWebhookUrl, discordtestdata) @@ -96,7 +96,7 @@ class Notification { // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] == 0) { let discorddowndata = { - username: "Uptime Kuma", + username: notification.discordUsername || "Uptime Kuma", embeds: [{ title: "❌ One of your services went down. ❌", color: 16711680, @@ -126,7 +126,7 @@ class Notification { } else if (heartbeatJSON["status"] == 1) { let discordupdata = { - username: "Uptime Kuma", + username: notification.discordUsername || "Uptime Kuma", embeds: [{ title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", color: 65280, From 4469b3a19b90e0d12b452363b1819a3fb0c65346 Mon Sep 17 00:00:00 2001 From: Niyas Date: Sat, 7 Aug 2021 11:13:25 +0530 Subject: [PATCH 6/9] Added discord username field --- src/components/NotificationDialog.vue | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 4eea85d66..1260e0241 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -143,6 +143,8 @@
+ +
You can get this by going to Server Settings -> Integrations -> Create Webhook
From 2677f5dd876f67a378137c6b7dad6afd4c3ee807 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sun, 8 Aug 2021 02:18:33 +0800 Subject: [PATCH 7/9] run eslint for discord enhancement --- server/notification.js | 112 ++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/server/notification.js b/server/notification.js index 65cccfbf4..c43983d7a 100644 --- a/server/notification.js +++ b/server/notification.js @@ -96,63 +96,63 @@ class Notification { // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] == 0) { let discorddowndata = { - username: notification.discordUsername || "Uptime Kuma", - embeds: [{ - title: "❌ One of your services went down. ❌", - color: 16711680, - timestamp: heartbeatJSON["time"], - fields: [ - { - name: "Service Name", - value: monitorJSON["name"], - }, - { - name: "Service URL", - value: monitorJSON["url"], - }, - { - name: "Time (UTC)", - value: heartbeatJSON["time"], - }, - { - name: "Error", - value: heartbeatJSON["msg"], - }, - ], - }], - } - await axios.post(notification.discordWebhookUrl, discorddowndata) - return okMsg; - - } else if (heartbeatJSON["status"] == 1) { + username: notification.discordUsername || "Uptime Kuma", + embeds: [{ + title: "❌ One of your services went down. ❌", + color: 16711680, + timestamp: heartbeatJSON["time"], + fields: [ + { + name: "Service Name", + value: monitorJSON["name"], + }, + { + name: "Service URL", + value: monitorJSON["url"], + }, + { + name: "Time (UTC)", + value: heartbeatJSON["time"], + }, + { + name: "Error", + value: heartbeatJSON["msg"], + }, + ], + }], + } + await axios.post(notification.discordWebhookUrl, discorddowndata) + return okMsg; + + } if (heartbeatJSON["status"] == 1) { let discordupdata = { - username: notification.discordUsername || "Uptime Kuma", - embeds: [{ - title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", - color: 65280, - timestamp: heartbeatJSON["time"], - fields: [ - { - name: "Service Name", - value: monitorJSON["name"], - }, - { - name: "Service URL", - value: "[Visit Service]("+ monitorJSON["url"] +")", - }, - { - name: "Time (UTC)", - value: heartbeatJSON["time"], - }, - { - name: "Ping", - value: heartbeatJSON["ping"] + "ms", - }, - ], - }], - } - await axios.post(notification.discordWebhookUrl, discordupdata) - return okMsg; + username: notification.discordUsername || "Uptime Kuma", + embeds: [{ + title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", + color: 65280, + timestamp: heartbeatJSON["time"], + fields: [ + { + name: "Service Name", + value: monitorJSON["name"], + }, + { + name: "Service URL", + value: "[Visit Service](" + monitorJSON["url"] + ")", + }, + { + name: "Time (UTC)", + value: heartbeatJSON["time"], + }, + { + name: "Ping", + value: heartbeatJSON["ping"] + "ms", + }, + ], + }], + } + await axios.post(notification.discordWebhookUrl, discordupdata) + return okMsg; } } catch (error) { throwGeneralAxiosError(error) From c990edc87ddd2060383442fa02ebae8e05e05fda Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sun, 8 Aug 2021 02:34:51 +0800 Subject: [PATCH 8/9] allowElseIf for else return, since its auto fix removes "else" but without newline --- .eslintrc.js | 3 --- server/notification.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 41ad54b81..dcd4215c5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -62,9 +62,6 @@ module.exports = { exceptAfterSingleLine: true, }], "no-unneeded-ternary": "error", - "no-else-return": ["error", { - "allowElseIf": false, - }], "array-bracket-newline": ["error", "consistent"], "eol-last": ["error", "always"], //'prefer-template': 'error', diff --git a/server/notification.js b/server/notification.js index c43983d7a..38cb80d90 100644 --- a/server/notification.js +++ b/server/notification.js @@ -124,7 +124,7 @@ class Notification { await axios.post(notification.discordWebhookUrl, discorddowndata) return okMsg; - } if (heartbeatJSON["status"] == 1) { + } else if (heartbeatJSON["status"] == 1) { let discordupdata = { username: notification.discordUsername || "Uptime Kuma", embeds: [{ From d5b40dfebf9ed4427e480478344e35ee60b293b3 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sun, 8 Aug 2021 11:03:22 +0800 Subject: [PATCH 9/9] better code reuse and "Username" to "Bot Display Name" --- server/notification.js | 8 +++++--- src/components/NotificationDialog.vue | 14 ++++++++------ src/main.js | 6 ++++++ src/util.js | 3 ++- src/util.ts | 1 + 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/server/notification.js b/server/notification.js index 38cb80d90..05dae8ad1 100644 --- a/server/notification.js +++ b/server/notification.js @@ -84,10 +84,12 @@ class Notification { } else if (notification.type === "discord") { try { + const discordDisplayName = notification.discordUsername || "Uptime Kuma"; + // If heartbeatJSON is null, assume we're testing. if (heartbeatJSON == null) { let discordtestdata = { - username: notification.discordUsername || "Uptime Kuma", + username: discordDisplayName, content: msg, } await axios.post(notification.discordWebhookUrl, discordtestdata) @@ -96,7 +98,7 @@ class Notification { // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] == 0) { let discorddowndata = { - username: notification.discordUsername || "Uptime Kuma", + username: discordDisplayName, embeds: [{ title: "❌ One of your services went down. ❌", color: 16711680, @@ -126,7 +128,7 @@ class Notification { } else if (heartbeatJSON["status"] == 1) { let discordupdata = { - username: notification.discordUsername || "Uptime Kuma", + username: discordDisplayName, embeds: [{ title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", color: 65280, diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 1260e0241..e4adb528f 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -143,8 +143,11 @@
- - +
+ +
+ +
You can get this by going to Server Settings -> Integrations -> Create Webhook
@@ -235,17 +238,17 @@ @@ -336,7 +339,6 @@
-