Merge pull request #9 from josedario87/codex/fix-router-processing-of-webhook-data
All checks were successful
Deploy conversation layer / deploy (push) Successful in 19s

Fix webhook parsing for new message format
This commit is contained in:
josedario87
2025-06-04 23:46:56 -06:00
committed by GitHub
2 changed files with 24 additions and 21 deletions

View File

@@ -19,6 +19,8 @@ services:
environment: environment:
- OPEN_WA_URL=http://openwa:8080 - OPEN_WA_URL=http://openwa:8080
- LLM_AGENT_URL=http://llm-agent:8000 - LLM_AGENT_URL=http://llm-agent:8000
- WEBHOOK_URL=http://whatsapp-router:3001/webhook
- NODE_ENV=production
ports: ports:
- "3001:3001" - "3001:3001"
networks: networks:

View File

@@ -85,7 +85,7 @@ async function registerWebhook() {
const eventConfig = { const eventConfig = {
onAck: false, onAck: false,
onAddedToGroup: true, onAddedToGroup: true,
onAnyMessage: true, onAnyMessage: false,
onBattery: true, onBattery: true,
onBroadcast: true, onBroadcast: true,
onButton: true, onButton: true,
@@ -100,7 +100,7 @@ async function registerWebhook() {
onIncomingCall: false, onIncomingCall: false,
onLabel: true, onLabel: true,
onLogout: true, onLogout: true,
onMessage: false, onMessage: true,
onMessageDeleted: true, onMessageDeleted: true,
onNewProduct: true, onNewProduct: true,
onOrder: true, onOrder: true,
@@ -126,39 +126,40 @@ async function registerWebhook() {
app.use(express.json()); app.use(express.json());
app.post('/webhook', async (req: express.Request, res: express.Response) => { app.post('/webhook', async (req: express.Request, res: express.Response) => {
log('debug', 'Received webhook request:', req.body); let message: WhatsAppMessage | undefined;
const { message, text, from } = req.body as { let text: string | undefined;
message?: WhatsAppMessage; let from: string | undefined;
text?: string;
from?: string;
};
const incoming = message || text; 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{}
log('debug', 'Webhook received:', {
message: incoming, if (message) {
from, const tipo = typeof message === 'string' ? 'texto' : 'objeto';
chatId: message?.chatId,
type: typeof incoming
});
if (incoming) {
const tipo = typeof incoming === 'string' ? 'texto' : 'objeto';
const origen = from || (message?.chatId ?? 'desconocido'); const origen = from || (message?.chatId ?? 'desconocido');
log('info', `📩 Mensaje recibido (${tipo}) de ${origen}`); log('info', `📩 Mensaje recibido (${message?.text}) de ${origen}`);
} }
try { try {
if (!incoming) return res.sendStatus(200); if (!message) return res.sendStatus(200);
if (!openWaUrl) throw new Error('Service URLs not configured'); if (!openWaUrl) throw new Error('Service URLs not configured');
const chatId = (message && message.chatId) || from; const chatId = (message && message.chatId) || from;
const handler = getHandler(chatId, agentUrl); const handler = getHandler(chatId, agentUrl);
if (!handler) throw new Error('No handler configured'); if (!handler) throw new Error('No handler configured');
let reply: string; let reply: string;
if (typeof handler === '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; reply = agentRes.data.reply || agentRes.data;
} else { } else {
reply = await handler(incoming); reply = await handler(message);
} }
await axios.post(`${openWaUrl}/sendText`, { args: { to: from, content: reply } }); await axios.post(`${openWaUrl}/sendText`, { args: { to: from, content: reply } });
} catch (err: any) { } catch (err: any) {