From 28f5d355a7450528a3c46e561533a61c00378719 Mon Sep 17 00:00:00 2001 From: Skorpios Date: Wed, 22 Jan 2025 15:41:47 -0800 Subject: [PATCH] Add Discord webhook notification support --- Server/db/models/Notification.js | 2 +- Server/service/notificationService.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Server/db/models/Notification.js b/Server/db/models/Notification.js index 8767880c1..8d4a7e515 100644 --- a/Server/db/models/Notification.js +++ b/Server/db/models/Notification.js @@ -8,7 +8,7 @@ const NotificationSchema = mongoose.Schema( }, type: { type: String, - enum: ["email", "sms"], + enum: ["email", "sms", "discord"], }, address: { type: String, diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index 807640a61..eade52f55 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -1,4 +1,5 @@ const SERVICE_NAME = "NotificationService"; +import axios from 'axios'; class NotificationService { static SERVICE_NAME = SERVICE_NAME; @@ -16,6 +17,26 @@ class NotificationService { this.logger = logger; } + async sendDiscordNotification(networkResponse, webhookUrl) { + const { monitor, status } = networkResponse; + const message = { + content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` + }; + + try { + await axios.post(webhookUrl, message); + return true; + } catch (error) { + this.logger.error({ + message: error.message, + service: this.SERVICE_NAME, + method: "sendDiscordNotification", + stack: error.stack, + }); + return false; + } + } + /** * Sends an email notification for hardware infrastructure alerts *