From d3810db6cb3b5a93db01e985b48ac720b33cdcb5 Mon Sep 17 00:00:00 2001 From: Skorpios Date: Sat, 25 Jan 2025 16:13:16 -0800 Subject: [PATCH] Added telegram notifications --- Server/db/models/Notification.js | 2 +- Server/service/notificationService.js | 69 +++++++++++++++++++-------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/Server/db/models/Notification.js b/Server/db/models/Notification.js index 58433fbc0..4b24c8f49 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", "slack"], + enum: ["email", "sms", "discord", "slack", "telegram"], }, address: { type: String, diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index f47a2a74c..b352dd729 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -57,6 +57,37 @@ class NotificationService { } } + async sendTelegramNotification(networkResponse, address) { + const { monitor, status } = networkResponse; + + const [botToken, chatId] = address.split('|').map(part => part?.trim()); + + if (!botToken || !chatId) { + return false; + } + + const message = { + chat_id: chatId, + text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`, + }; + + const url = `https://api.telegram.org/bot${botToken}/sendMessage`; + + try { + await axios.post(url, message, { + headers: { + 'Content-Type': 'application/json', + }, + }); + return true; + } catch (error) { + return false; + } + } + + + + /** * Sends an email notification for hardware infrastructure alerts * @@ -97,25 +128,25 @@ class NotificationService { } async handleStatusNotifications(networkResponse) { - 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); - - 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 - } + try { + if (networkResponse.statusChanged === false) return false; + if (networkResponse.prevStatus === undefined) return false; + + 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); + } else if (notification.type === "telegram") { + this.sendTelegramNotification(networkResponse, notification.address); + } + + // Handle other types of notifications here + } return true; } catch (error) { this.logger.warn({