Check if platform type is accepted before formatting the message.

This commit is contained in:
Skorpios committed 2025-02-17 09:55:19 -08:00
1 parent afc9d46bd7
commit 03a938de8a
1 file changed
+18 -7
+18 -7
View File
@@ -72,10 +72,9 @@ class NotificationService {
const { monitor, status } = networkResponse; const { monitor, status } = networkResponse;
const { platform } = notification; const { platform } = notification;
const { webhookUrl, botToken, chatId } = notification.config; const { webhookUrl, botToken, chatId } = notification.config;
let url = webhookUrl;
const message = this.formatNotificationMessage(monitor, status, platform, chatId); // Early return if platform is not supported
if (message === null) { if (!PLATFORM_TYPES.includes(platform)) {
this.logger.warn({ this.logger.warn({
message: this.stringService.getWebhookUnsupportedPlatform(platform), message: this.stringService.getWebhookUnsupportedPlatform(platform),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
@@ -85,13 +84,25 @@ class NotificationService {
return false; 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 (platform === 'telegram') {
if (!botToken || !chatId) {
return false;
}
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; 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 { try {
const response = await this.networkService.requestWebhook(platform, url, message); const response = await this.networkService.requestWebhook(platform, url, message);
return response.status; return response.status;
@@ -108,7 +119,7 @@ class NotificationService {
}); });
return false; return false;
} }
} }
/** /**
* Sends an email notification for hardware infrastructure alerts * Sends an email notification for hardware infrastructure alerts