diff --git a/Server/db/models/Notification.js b/Server/db/models/Notification.js index 4b24c8f49..7a3d599ff 100644 --- a/Server/db/models/Notification.js +++ b/Server/db/models/Notification.js @@ -10,6 +10,15 @@ const NotificationSchema = mongoose.Schema( type: String, enum: ["email", "sms", "discord", "slack", "telegram"], }, + webhookUrl: { + type: String, + }, + botToken: { + type: String, + }, + chatId: { + type: String, + }, address: { type: String, }, @@ -76,4 +85,5 @@ NotificationSchema.pre("findOneAndUpdate", function (next) { } next(); }); + export default mongoose.model("Notification", NotificationSchema); diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index 66a7ea889..05adbb3ff 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -18,17 +18,16 @@ class NotificationService { this.networkService = networkService; } - async sendWebhookNotification(networkResponse, address, platform) { + async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) { const { monitor, status } = networkResponse; let message; let url = address; - + if (platform === 'slack') { message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` }; } else if (platform === 'discord') { message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` }; } else if (platform === 'telegram') { - const [botToken, chatId] = address.split('|').map(part => part?.trim()); if (!botToken || !chatId) { return false; } @@ -38,7 +37,7 @@ class NotificationService { }; url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; } - + try { const response = await this.networkService.requestWebhook(platform, url, message); return response.status; @@ -56,9 +55,8 @@ class NotificationService { return false; } } - - + /** * Sends an email notification for hardware infrastructure alerts *