diff --git a/src/api/app.ts b/src/api/app.ts index 40ea926..75a4da2 100644 --- a/src/api/app.ts +++ b/src/api/app.ts @@ -37,7 +37,8 @@ export function createApp(deps: AppDependencies) { function getNotificationService() { const botToken = dynamicConfig.get("TELEGRAM_BOT_TOKEN"); const chatId = dynamicConfig.get("TELEGRAM_CHAT_ID"); - const telegram = botToken && chatId + const isTelegramDisabled = !chatId || ["disabled", "none", "off", "0"].includes(chatId.toLowerCase().trim()); + const telegram = botToken && chatId && !isTelegramDisabled ? new TelegramNotifier({ botToken, chatId }) : null; diff --git a/src/application/services/notification-service.ts b/src/application/services/notification-service.ts index edb35ce..33d9d09 100644 --- a/src/application/services/notification-service.ts +++ b/src/application/services/notification-service.ts @@ -28,7 +28,10 @@ export class NotificationService { const warnings = alerts.filter(a => a.severity === "warning"); // 1. Telegram - um resumo + alertas individuais críticos - if (this.telegram && this.telegramChatId) { + const isTelegramDisabled = !this.telegramChatId || + ["disabled", "none", "off", "0"].includes(this.telegramChatId.toLowerCase().trim()); + + if (this.telegram && this.telegramChatId && !isTelegramDisabled) { if (critical.length > 0) { for (const alert of critical) { const msg = this.formatter.formatExpiryAlert({ @@ -43,16 +46,24 @@ export class NotificationService { channels: [], }); const ok = await this.telegram.sendMessage(msg, this.telegramChatId); - if (ok) telegramSent++; - else errors.push(`Falha Telegram: ${alert.table.title}`); + if (ok) { + telegramSent++; + console.log(`[Telegram] Alerta enviado: ${alert.table.title}`); + } else { + errors.push(`Falha Telegram: ${alert.table.title}`); + } } } if (warnings.length > 0) { const summaryMsg = this.formatWarningSummary(warnings); const ok = await this.telegram.sendMessage(summaryMsg, this.telegramChatId); - if (ok) telegramSent++; - else errors.push("Falha Telegram: resumo de avisos"); + if (ok) { + telegramSent++; + console.log(`[Telegram] Resumo de avisos enviado`); + } else { + errors.push("Falha Telegram: resumo de avisos"); + } } } @@ -62,8 +73,13 @@ export class NotificationService { const msg = this.formatWhatsAppCritical(alert); for (const target of this.whatsappTargets) { const ok = await this.astracalls.sendText(target, msg); - if (ok.success) whatsappSent++; - else errors.push(`Falha WhatsApp ${target}: ${alert.table.title}`); + if (ok.success) { + whatsappSent++; + console.log(`[WhatsApp] Alerta enviado para ${target}: ${alert.table.title}`); + } else { + errors.push(`Falha WhatsApp ${target}: ${alert.table.title} (${ok.error})`); + console.error(`[WhatsApp] Falha ao enviar para ${target} (${alert.table.title}): ${ok.error}`); + } } } } diff --git a/src/index.ts b/src/index.ts index aed4c01..f52ab55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,8 @@ function startScheduler() { if (result.alerts_generated.length > 0) { const botToken = dynamicConfig.get("TELEGRAM_BOT_TOKEN", config.TELEGRAM_BOT_TOKEN); const chatId = dynamicConfig.get("TELEGRAM_CHAT_ID", config.TELEGRAM_CHAT_ID); - const telegram = botToken && chatId + const isTelegramDisabled = !chatId || ["disabled", "none", "off", "0"].includes(chatId.toLowerCase().trim()); + const telegram = botToken && chatId && !isTelegramDisabled ? new TelegramNotifier({ botToken, chatId }) : null; diff --git a/src/infrastructure/telegram/telegram-notifier.ts b/src/infrastructure/telegram/telegram-notifier.ts index 97141ce..28946e3 100644 --- a/src/infrastructure/telegram/telegram-notifier.ts +++ b/src/infrastructure/telegram/telegram-notifier.ts @@ -76,6 +76,11 @@ export class TelegramNotifier { const errText = await response.text().catch(() => ""); console.error(`[TelegramNotifier] Erro ${response.status}: ${errText}`); + + // Erros de cliente não recuperáveis (ex: 400 chat not found, 403 bot blocked, 404 bot not found) + if (response.status >= 400 && response.status < 500) { + break; + } } catch (err: any) { console.error(`[TelegramNotifier] Falha tentativa ${attempt}:`, err?.message); await new Promise((resolve) => setTimeout(resolve, 300 * attempt));