From 9ade9af1e29979e8f81b461eda13f86560939d6a Mon Sep 17 00:00:00 2001 From: Niyas Date: Thu, 5 Aug 2021 21:41:11 +0530 Subject: [PATCH] Discord enhancements --- server/notification.js | 76 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/server/notification.js b/server/notification.js index f53b681..06926b3 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 discorddowndata = { + username: "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"], + }, + { + name: "Visit Service Dashboard", + value: "[Visit Dashboard]("+ dashboardURL + ")", + }, + { + name: "Visit Uptime Kuma", + value: "[Visit]("+ kumaURL +")", + }, + ], + }], } - let data = { - username: "Uptime-Kuma", + await axios.post(notification.discordWebhookUrl, discorddowndata) + return okMsg; + + } else if (heartbeatJSON["status"] == 1) { + let discordupdata = { + username: "Uptime Kuma", embeds: [{ - title: "Uptime-Kuma Alert", - color: alertColor, + 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: "Message", - value: msg, + name: "Ping", + value: heartbeatJSON["ping"] + "ms", + }, + { + name: "Visit Uptime Kuma", + value: "[Visit]("+ kumaURL +")", }, ], }], } - await axios.post(notification.discordWebhookUrl, data) + await axios.post(notification.discordWebhookUrl, discordupdata) return okMsg; + } } catch (error) { throwGeneralAxiosError(error) }