diff --git a/Server/controllers/notificationController.js b/Server/controllers/notificationController.js index a65206cbe..7681d7c2a 100644 --- a/Server/controllers/notificationController.js +++ b/Server/controllers/notificationController.js @@ -9,8 +9,9 @@ import { handleError, handleValidationError } from './controllerUtils.js'; const SERVICE_NAME = "NotificationController"; class NotificationController { - constructor(notificationService) { + constructor(notificationService, stringService) { this.notificationService = notificationService; + this.stringService = stringService; this.triggerNotification = this.triggerNotification.bind(this); } diff --git a/Server/index.js b/Server/index.js index 090498de2..0733e9b78 100644 --- a/Server/index.js +++ b/Server/index.js @@ -187,7 +187,7 @@ const startApp = async () => { logger ); const statusService = new StatusService(db, logger); - const notificationService = new NotificationService(emailService, db, logger, networkService); + const notificationService = new NotificationService(emailService, db, logger, networkService, stringService); const jobQueue = new JobQueue( @@ -274,7 +274,8 @@ const startApp = async () => { ); const notificationController = new NotificationController( - ServiceRegistry.get(NotificationService.SERVICE_NAME) + ServiceRegistry.get(NotificationService.SERVICE_NAME), + ServiceRegistry.get(StringService.SERVICE_NAME) ); const distributedUptimeController = new DistributedUptimeController( diff --git a/Server/service/notificationService.js b/Server/service/notificationService.js index d19e1fe32..be8432336 100644 --- a/Server/service/notificationService.js +++ b/Server/service/notificationService.js @@ -11,12 +11,13 @@ class NotificationService { * @param {Object} logger - The logger instance for logging activities. * @param {Object} networkService - The network service for sending webhook notifications. */ - constructor(emailService, db, logger, networkService) { + constructor(emailService, db, logger, networkService, stringService) { this.SERVICE_NAME = SERVICE_NAME; this.emailService = emailService; this.db = db; this.logger = logger; this.networkService = networkService; + this.stringService = stringService; } /** @@ -32,19 +33,23 @@ class NotificationService { */ formatNotificationMessage(monitor, status, platform, chatId) { - const messageText = `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`; - - if (platform === 'telegram') { - return { chat_id: chatId, text: messageText }; - } - if (platform === 'slack') { - return { text: messageText }; - } - if (platform === 'discord') { - return { content: messageText }; - } - return null; - } + const messageText = this.stringService.getMonitorStatus( + monitor.name, + status, + monitor.url + ); + + if (platform === 'telegram') { + return { chat_id: chatId, text: messageText }; + } + if (platform === 'slack') { + return { text: messageText }; + } + if (platform === 'discord') { + return { content: messageText }; + } + return null; + } /** * Sends a webhook notification to a specified platform. @@ -62,46 +67,46 @@ class NotificationService { */ async sendWebhookNotification(networkResponse, notification) { - 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) { - this.logger.warn({ - message: `Unsupported webhook platform: ${platform}`, - service: this.SERVICE_NAME, - method: 'sendWebhookNotification', - platform - }); - return false; - } - - if (platform === 'telegram') { - if (!botToken || !chatId) { - return false; - } - url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; - } - - try { - const response = await this.networkService.requestWebhook(platform, url, message); - return response.status; - } catch (error) { - this.logger.error({ - message: `Error sending ${platform} notification`, - service: this.SERVICE_NAME, - method: 'sendWebhookNotification', - error: error.message, - stack: error.stack, - url, - platform, - requestPayload: message - }); - return false; - } - } + 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) { + this.logger.warn({ + message: this.stringService.getWebhookUnsupportedPlatform(platform), + service: this.SERVICE_NAME, + method: 'sendWebhookNotification', + platform + }); + return false; + } + + if (platform === 'telegram') { + if (!botToken || !chatId) { + return false; + } + url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; + } + + try { + const response = await this.networkService.requestWebhook(platform, url, message); + return response.status; + } catch (error) { + this.logger.error({ + message: this.stringService.getWebhookSendError(platform), + service: this.SERVICE_NAME, + method: 'sendWebhookNotification', + error: error.message, + stack: error.stack, + url, + platform, + requestPayload: message + }); + return false; + } + } /** * Sends an email notification for hardware infrastructure alerts diff --git a/Server/service/stringService.js b/Server/service/stringService.js index 1ef208d6c..e3ab03ca5 100644 --- a/Server/service/stringService.js +++ b/Server/service/stringService.js @@ -165,6 +165,36 @@ class StringService { return this.translationService.getTranslation('maintenanceWindowEdit'); } + // Webhook Messages + get webhookUnsupportedPlatform() { + return this.translationService.getTranslation('webhookUnsupportedPlatform'); + } + + get webhookSendError() { + return this.translationService.getTranslation('webhookSendError'); + } + + get webhookSendSuccess() { + return this.translationService.getTranslation('webhookSendSuccess'); + } + + getWebhookUnsupportedPlatform(platform) { + return this.translationService.getTranslation('webhookUnsupportedPlatform') + .replace('{platform}', platform); + } + + getWebhookSendError(platform) { + return this.translationService.getTranslation('webhookSendError') + .replace('{platform}', platform); + } + + getMonitorStatus(name, status, url) { + return this.translationService.getTranslation('monitorStatus') + .replace('{name}', name) + .replace('{status}', status ? "up" : "down") + .replace('{url}', url); + } + // Error Messages get unknownError() { return this.translationService.getTranslation('unknownError');