From 37c4384dec119f0c49711a8b0201ac1e7bd8c697 Mon Sep 17 00:00:00 2001 From: josedario87 <71241187+josedario87@users.noreply.github.com> Date: Wed, 4 Jun 2025 23:33:02 -0600 Subject: [PATCH 1/2] fix webhook data parsing --- whatsapp-router/src/index.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/whatsapp-router/src/index.ts b/whatsapp-router/src/index.ts index 36daaa6..35a937d 100644 --- a/whatsapp-router/src/index.ts +++ b/whatsapp-router/src/index.ts @@ -127,11 +127,23 @@ app.use(express.json()); app.post('/webhook', async (req: express.Request, res: express.Response) => { log('debug', 'Received webhook request:', req.body); - const { message, text, from } = req.body as { - message?: WhatsAppMessage; - text?: string; - from?: string; - }; + + let message: WhatsAppMessage | undefined; + let text: string | undefined; + let from: string | undefined; + + if (req.body && req.body.data) { + // New webhook format from nucleo-whatsapp + message = req.body.data as WhatsAppMessage; + text = (req.body.data.body as string) || req.body.data.text; + from = req.body.data.from; + } else { + ({ message, text, from } = req.body as { + message?: WhatsAppMessage; + text?: string; + from?: string; + }); + } const incoming = message || text; From b154e74538cfeac769c22c3d737fc4fabce755c6 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Wed, 4 Jun 2025 23:45:48 -0600 Subject: [PATCH 2/2] listo procesamiento y enrutamiento de mensajes de whatsapp --- docker-compose.yml | 2 ++ whatsapp-router/src/index.ts | 47 ++++++++++++++---------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5fd7bfa..cf1bb82 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,8 @@ services: environment: - OPEN_WA_URL=http://openwa:8080 - LLM_AGENT_URL=http://llm-agent:8000 + - WEBHOOK_URL=http://whatsapp-router:3001/webhook + - NODE_ENV=production ports: - "3001:3001" networks: diff --git a/whatsapp-router/src/index.ts b/whatsapp-router/src/index.ts index 35a937d..f2defe9 100644 --- a/whatsapp-router/src/index.ts +++ b/whatsapp-router/src/index.ts @@ -85,7 +85,7 @@ async function registerWebhook() { const eventConfig = { onAck: false, onAddedToGroup: true, - onAnyMessage: true, + onAnyMessage: false, onBattery: true, onBroadcast: true, onButton: true, @@ -100,7 +100,7 @@ async function registerWebhook() { onIncomingCall: false, onLabel: true, onLogout: true, - onMessage: false, + onMessage: true, onMessageDeleted: true, onNewProduct: true, onOrder: true, @@ -126,51 +126,40 @@ async function registerWebhook() { app.use(express.json()); app.post('/webhook', async (req: express.Request, res: express.Response) => { - log('debug', 'Received webhook request:', req.body); - let message: WhatsAppMessage | undefined; let text: string | undefined; let from: string | undefined; - if (req.body && req.body.data) { - // New webhook format from nucleo-whatsapp - message = req.body.data as WhatsAppMessage; - text = (req.body.data.body as string) || req.body.data.text; - from = req.body.data.from; - } else { - ({ message, text, from } = req.body as { - message?: WhatsAppMessage; - text?: string; - from?: string; - }); - } + try { + if (req.body && req.body.data) { + // New webhook format from nucleo-whatsapp + message = req.body.data as WhatsAppMessage; + text = (req.body.data.body as string) || req.body.data.text; + from = req.body.data.from; + } else { + throw new Error('Invalid webhook format'); + } + }catch{} - const incoming = message || text; - log('debug', 'Webhook received:', { - message: incoming, - from, - chatId: message?.chatId, - type: typeof incoming - }); - if (incoming) { - const tipo = typeof incoming === 'string' ? 'texto' : 'objeto'; + if (message) { + const tipo = typeof message === 'string' ? 'texto' : 'objeto'; const origen = from || (message?.chatId ?? 'desconocido'); - log('info', `📩 Mensaje recibido (${tipo}) de ${origen}`); + log('info', `📩 Mensaje recibido (${message?.text}) de ${origen}`); } try { - if (!incoming) return res.sendStatus(200); + if (!message) return res.sendStatus(200); if (!openWaUrl) throw new Error('Service URLs not configured'); const chatId = (message && message.chatId) || from; const handler = getHandler(chatId, agentUrl); if (!handler) throw new Error('No handler configured'); let reply: string; if (typeof handler === 'string') { - const agentRes = await axios.post(handler, { message: incoming }); + const agentRes = await axios.post(handler, {message}); reply = agentRes.data.reply || agentRes.data; } else { - reply = await handler(incoming); + reply = await handler(message); } await axios.post(`${openWaUrl}/sendText`, { args: { to: from, content: reply } }); } catch (err: any) {