fix: log detalhado de envio whatsapp e suporte a desativacao explicita de telegram

This commit is contained in:
Andre RDS
2026-11-28 11:28:49 -03:00
parent cc6a77612b
commit bc8ac7b74f
4 changed files with 32 additions and 9 deletions
+2 -1
View File
@@ -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;
@@ -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}`);
}
}
}
}
+2 -1
View File
@@ -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;
@@ -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));