From 85d61c64b4c925d61b531d81321c4aba613a478e Mon Sep 17 00:00:00 2001 From: josedario87 <71241187+josedario87@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:37:55 -0600 Subject: [PATCH] add handler mapping --- README.md | 5 +++-- whatsapp-router/src/chatHandlers.ts | 9 +++++++++ whatsapp-router/src/index.ts | 8 ++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 whatsapp-router/src/chatHandlers.ts diff --git a/README.md b/README.md index 317167a..acb4064 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This module contains the services that handle messaging for WhatsApp and the web chat interface. All source code is now written in **TypeScript**. It is composed of three containers: - **openwa** – provides access to WhatsApp through the [open-wa](https://github.com/open-wa/wa-automate-nodejs) project. -- **whatsapp-router** – receives webhook events from openwa and forwards messages to an external LLM agent. +- **whatsapp-router** – receives webhook events from openwa and forwards messages to a conversation handler. Handlers can be configured per chat ID in `whatsapp-router/src/chatHandlers.ts`. - **chat-ui** – simple web chat interface that also communicates with the LLM agent. All services can be launched together with `docker-compose`. @@ -11,7 +11,8 @@ All services can be launched together with `docker-compose`. ## Usage 1. Configure the URL of your LLM agent in `docker-compose.yml` (`LLM_AGENT_URL`). -2. Run: +2. Optionally edit `whatsapp-router/src/chatHandlers.ts` to map specific chat IDs to different handler URLs. +3. Run: ```bash docker-compose up --build diff --git a/whatsapp-router/src/chatHandlers.ts b/whatsapp-router/src/chatHandlers.ts new file mode 100644 index 0000000..efc7be8 --- /dev/null +++ b/whatsapp-router/src/chatHandlers.ts @@ -0,0 +1,9 @@ +export const chatHandlers: Record = { + // Example mapping: + // '50496210031@c.us': 'http://llm-agent:8000' +}; + +export function getHandler(chatId: string | undefined, defaultUrl?: string): string | undefined { + if (!chatId) return defaultUrl; + return chatHandlers[chatId] || defaultUrl; +} diff --git a/whatsapp-router/src/index.ts b/whatsapp-router/src/index.ts index 6f522b3..3e15ebc 100644 --- a/whatsapp-router/src/index.ts +++ b/whatsapp-router/src/index.ts @@ -1,6 +1,7 @@ import express from 'express'; import axios from 'axios'; import { WhatsAppMessage } from './types'; +import { getHandler } from './chatHandlers'; const app = express(); @@ -114,8 +115,11 @@ app.post('/webhook', async (req: express.Request, res: express.Response) => { const incoming = message || text; try { if (!incoming) return res.sendStatus(200); - if (!agentUrl || !openWaUrl) throw new Error('Service URLs not configured'); - const agentRes = await axios.post(agentUrl, { message: incoming }); + if (!openWaUrl) throw new Error('Service URLs not configured'); + const chatId = (message && message.chatId) || from; + const handlerUrl = getHandler(chatId, agentUrl); + if (!handlerUrl) throw new Error('No handler configured'); + const agentRes = await axios.post(handlerUrl, { message: incoming }); const reply = agentRes.data.reply || agentRes.data; await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply }); } catch (err: any) {