From 77fadb03be3317ec7c1832cf1b0e6fe5445ef54a Mon Sep 17 00:00:00 2001 From: AI Bot Date: Mon, 24 Aug 2026 10:33:27 -0500 Subject: [PATCH] fix(webhook): allow self-messaging from admin phone --- src/app/api/webhooks/whatsapp/route.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/app/api/webhooks/whatsapp/route.ts b/src/app/api/webhooks/whatsapp/route.ts index a5e3d67..ecf06c4 100644 --- a/src/app/api/webhooks/whatsapp/route.ts +++ b/src/app/api/webhooks/whatsapp/route.ts @@ -39,9 +39,13 @@ export async function POST(req: Request) { const remoteJid = data?.key?.remoteJid; const fromMe = data?.key?.fromMe; - // Ignorar mensajes enviados por el bot mismo - if (fromMe) { - return NextResponse.json({ success: true, message: 'Ignored bot message' }, { status: 200 }); + const messageData = data?.message || {}; + const rawText = messageData.conversation || messageData.extendedTextMessage?.text || messageData.imageMessage?.caption || messageData.videoMessage?.caption || ''; + + // Ignorar mensajes generados por el bot para evitar bucle infinito, + // pero permitir tests que el administrador envíe desde el mismo celular del bot. + if (fromMe && (rawText.includes('📰 *Publicación Procesada') || rawText.includes('✅ La publicación ha sido aprobada') || rawText.includes('❌ Error al publicar'))) { + return NextResponse.json({ success: true, message: 'Ignored bot self-message loop' }, { status: 200 }); } // Recomendado: filtrar por el grupo específico @@ -49,10 +53,8 @@ export async function POST(req: Request) { // return NextResponse.json({ success: true, message: 'Ignored other chat' }, { status: 200 }); // } - const messageData = data?.message || {}; - - // Extraer texto del mensaje - const text = messageData.conversation || messageData.extendedTextMessage?.text || messageData.imageMessage?.caption || messageData.videoMessage?.caption || ''; + // Extraer texto del mensaje (ya guardado en rawText, lo mantenemos como text para retrocompatibilidad interna) + const text = rawText; const lowerText = text.trim().toLowerCase(); const contextInfo = messageData.extendedTextMessage?.contextInfo;