diff --git a/Server/db/models/Notification.js b/Server/db/models/Notification.js index 8d4a7e515..58433fbc0 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", "discord"], + enum: ["email", "sms", "discord", "slack"], }, address: { type: String, diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index eade52f55..f47a2a74c 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -37,6 +37,26 @@ class NotificationService { } } + async sendSlackNotification(networkResponse, webhookUrl) { + const { monitor, status } = networkResponse; + const message = { + text: `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: "sendSlackNotification", + stack: error.stack, + }); + return false; + } + } + /** * Sends an email notification for hardware infrastructure alerts * @@ -77,22 +97,25 @@ class NotificationService { } async handleStatusNotifications(networkResponse) { - try { - //If status hasn't changed, we're done - if (networkResponse.statusChanged === false) return false; + try { + // If status hasn't changed, we're done + if (networkResponse.statusChanged === false) return false; - // if prevStatus is undefined, monitor is resuming, we're done - if (networkResponse.prevStatus === undefined) return false; - const notifications = await this.db.getNotificationsByMonitorId( - networkResponse.monitorId - ); + // if prevStatus is undefined, monitor is resuming, we're done + if (networkResponse.prevStatus === undefined) return false; - for (const notification of notifications) { - if (notification.type === "email") { - this.sendEmail(networkResponse, notification.address); - } - // Handle other types of notifications here - } + const notifications = await this.db.getNotificationsByMonitorId(networkResponse.monitorId); + + for (const notification of notifications) { + if (notification.type === "email") { + this.sendEmail(networkResponse, notification.address); + } else if (notification.type === "discord") { + this.sendDiscordNotification(networkResponse, notification.address); + } else if (notification.type === "slack") { + this.sendSlackNotification(networkResponse, notification.address); + } + // Handle other types of notifications here + } return true; } catch (error) { this.logger.warn({