From 84fe58b3617c8a467f4661fa621236f9be07d022 Mon Sep 17 00:00:00 2001 From: Skorpios Date: Sun, 26 Jan 2025 14:11:36 -0800 Subject: [PATCH] Refactor webhook integrations functions. --- Server/service/notificationService.js | 90 +++++++++------------------ 1 file changed, 29 insertions(+), 61 deletions(-) diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index b352dd729..a54afd506 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -17,75 +17,43 @@ class NotificationService { this.logger = logger; } - async sendDiscordNotification(networkResponse, webhookUrl) { + async sendWebhookNotification(networkResponse, address, platform) { const { monitor, status } = networkResponse; - const message = { - content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` - }; + 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) { + console.error('Invalid Telegram address format'); + return false; + } + message = { + chat_id: chatId, + text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` + }; + url = `https://api.telegram.org/bot${botToken}/sendMessage`; + } try { - await axios.post(webhookUrl, message); + await axios.post(url, message, { + headers: { + 'Content-Type': 'application/json' + } + }); return true; } catch (error) { - this.logger.error({ - message: error.message, - service: this.SERVICE_NAME, - method: "sendDiscordNotification", - stack: error.stack, - }); + + console.error(`Error sending ${platform} notification:`, error.toJSON()); + console.error(`URL: ${url}`); + console.error(`Message:`, message); return false; } } - - 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; - } - } - - 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; - } - } - - /**