From 03a938de8af77bdc5be6ac3dcc4b5f5abd43041b Mon Sep 17 00:00:00 2001 From: Skorpios Date: Mon, 17 Feb 2025 09:55:19 -0800 Subject: [PATCH] Check if platform type is accepted before formatting the message. --- Server/service/notificationService.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index 3f4efb403..2546df3f9 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -72,10 +72,9 @@ class NotificationService { const { monitor, status } = networkResponse; const { platform } = notification; const { webhookUrl, botToken, chatId } = notification.config; - let url = webhookUrl; - const message = this.formatNotificationMessage(monitor, status, platform, chatId); - if (message === null) { + // Early return if platform is not supported + if (!PLATFORM_TYPES.includes(platform)) { this.logger.warn({ message: this.stringService.getWebhookUnsupportedPlatform(platform), service: this.SERVICE_NAME, @@ -85,13 +84,25 @@ class NotificationService { return false; } + // Early return for telegram if required fields are missing + if (platform === 'telegram' && (!botToken || !chatId)) { + this.logger.warn({ + message: 'Missing required fields for Telegram notification', + service: this.SERVICE_NAME, + method: 'sendWebhookNotification', + platform + }); + return false; + } + + let url = webhookUrl; if (platform === 'telegram') { - if (!botToken || !chatId) { - return false; - } url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; } + // Now that we know the platform is valid, format the message + const message = this.formatNotificationMessage(monitor, status, platform, chatId); + try { const response = await this.networkService.requestWebhook(platform, url, message); return response.status; @@ -108,7 +119,7 @@ class NotificationService { }); return false; } - } + } /** * Sends an email notification for hardware infrastructure alerts