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
+16 -5
+16 -5
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;
} }
if (platform === 'telegram') { // Early return for telegram if required fields are missing
if (!botToken || !chatId) { if (platform === 'telegram' && (!botToken || !chatId)) {
this.logger.warn({
message: 'Missing required fields for Telegram notification',
service: this.SERVICE_NAME,
method: 'sendWebhookNotification',
platform
});
return false; return false;
} }
let url = webhookUrl;
if (platform === 'telegram') {
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;