From cd69146f334747e3e7f4d3f54930fb2645213176 Mon Sep 17 00:00:00 2001 From: Dj Isaac Date: Sun, 20 Oct 2024 19:28:40 -0500 Subject: [PATCH] add discord buttons to payload --- server/notification-providers/discord.js | 70 ++++++++++++++++-------- src/components/notifications/Discord.vue | 6 ++ src/lang/en.json | 2 + 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index bd54cc7c..e74c0c5b 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -1,6 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); -const { DOWN, UP } = require("../../src/util"); +const { Settings } = require("../settings"); +const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util"); class Discord extends NotificationProvider { name = "discord"; @@ -19,8 +20,13 @@ class Discord extends NotificationProvider { } // If heartbeatJSON is null, assume we're testing. + + const baseURL = await Settings.get("primaryBaseURL"); + const address = this.extractAddress(monitorJSON); + const hasAddress = address !== "" && address !== monitorJSON.hostname; + if (heartbeatJSON == null) { - let discordtestdata = { + const discordtestdata = { username: discordDisplayName, content: msg, }; @@ -33,20 +39,18 @@ class Discord extends NotificationProvider { return okMsg; } - const address = this.extractAddress(monitorJSON); - const embedFields = [ { name: "Service Name", - value: monitorJSON["name"], + value: monitorJSON.name, }, - ...((address !== "" && address !== monitorJSON["hostname"]) ? [{ + ...(hasAddress ? [{ name: "Service URL", value: address }] : []), { - name: `Time (${heartbeatJSON["timezone"]})`, - value: heartbeatJSON["localDateTime"], + name: `Time (${heartbeatJSON.timezone})`, + value: heartbeatJSON.localDateTime, }, { name: "Error", @@ -54,50 +58,70 @@ class Discord extends NotificationProvider { }, ]; + const components = [ + { + type: 1, // Action Row + components: [ + baseURL && { + type: 2, // Button + style: 5, // Link Button, + label: "Visit Uptime Kuma", + url: baseURL + getMonitorRelativeURL(monitorJSON.id) + }, + hasAddress && { + type: 2, // Button + style: 5, // Link Button, + label: "Visit Service URL", + url: address + } + ].filter(Boolean) // remove invalid data + } + ]; + // If heartbeatJSON is not null, we go into the normal alerting loop. - if (heartbeatJSON["status"] === DOWN) { - let discorddowndata = { + if (heartbeatJSON.status === DOWN) { + const discorddowndata = { username: discordDisplayName, + content: notification.discordPrefixMessage || "", embeds: [{ - title: "❌ Your service " + monitorJSON["name"] + " went down. ❌", + title: `❌ Your service ${monitorJSON.name} went down. ❌`, color: 16711680, - timestamp: heartbeatJSON["time"], + timestamp: heartbeatJSON.time, fields: embedFields, }], + components: components, }; + if (notification.discordChannelType === "createNewForumPost") { discorddowndata.thread_name = notification.postName; } - if (notification.discordPrefixMessage) { - discorddowndata.content = notification.discordPrefixMessage; - } await axios.post(webhookUrl.toString(), discorddowndata); return okMsg; + } - } else if (heartbeatJSON["status"] === UP) { - let discordupdata = { + if (heartbeatJSON.status === UP) { + const discordupdata = { username: discordDisplayName, + content: notification.discordPrefixMessage || "", embeds: [{ - title: "✅ Your service " + monitorJSON["name"] + " is up! ✅", + title: `✅ Your service ${monitorJSON.name} is up! ✅`, color: 65280, - timestamp: heartbeatJSON["time"], + timestamp: heartbeatJSON.time, fields: embedFields, }], + components: components, }; if (notification.discordChannelType === "createNewForumPost") { discordupdata.thread_name = notification.postName; } - if (notification.discordPrefixMessage) { - discordupdata.content = notification.discordPrefixMessage; - } - await axios.post(webhookUrl.toString(), discordupdata); return okMsg; } } catch (error) { + console.log(error); this.throwGeneralAxiosError(error); } } diff --git a/src/components/notifications/Discord.vue b/src/components/notifications/Discord.vue index 5d8334f5..966da93e 100644 --- a/src/components/notifications/Discord.vue +++ b/src/components/notifications/Discord.vue @@ -4,6 +4,12 @@
{{ $t("wayToGetDiscordURL") }} + + {{ $t("withADiscordBot") }} +
diff --git a/src/lang/en.json b/src/lang/en.json index 968a3d9f..60d83244 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -541,6 +541,8 @@ "smtpBCC": "BCC", "Discord Webhook URL": "Discord Webhook URL", "wayToGetDiscordURL": "You can get this by going to Server Settings -> Integrations -> View Webhooks -> New Webhook", + "wayToGetDiscordURLEnhanced": "Using the Discord API, you may also create the webhook {0} for button support.", + "withADiscordBot": "with a bot", "Bot Display Name": "Bot Display Name", "Prefix Custom Message": "Prefix Custom Message", "Hello @everyone is...": "Hello {'@'}everyone is…",