add handler mapping

This commit is contained in:
josedario87
2025-06-04 21:37:55 -06:00
parent 0836c086b2
commit 85d61c64b4
3 changed files with 18 additions and 4 deletions

View File

@@ -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: 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. - **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. - **chat-ui** simple web chat interface that also communicates with the LLM agent.
All services can be launched together with `docker-compose`. All services can be launched together with `docker-compose`.
@@ -11,7 +11,8 @@ All services can be launched together with `docker-compose`.
## Usage ## Usage
1. Configure the URL of your LLM agent in `docker-compose.yml` (`LLM_AGENT_URL`). 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 ```bash
docker-compose up --build docker-compose up --build

View File

@@ -0,0 +1,9 @@
export const chatHandlers: Record<string, string> = {
// 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;
}

View File

@@ -1,6 +1,7 @@
import express from 'express'; import express from 'express';
import axios from 'axios'; import axios from 'axios';
import { WhatsAppMessage } from './types'; import { WhatsAppMessage } from './types';
import { getHandler } from './chatHandlers';
const app = express(); const app = express();
@@ -114,8 +115,11 @@ app.post('/webhook', async (req: express.Request, res: express.Response) => {
const incoming = message || text; const incoming = message || text;
try { try {
if (!incoming) return res.sendStatus(200); if (!incoming) return res.sendStatus(200);
if (!agentUrl || !openWaUrl) throw new Error('Service URLs not configured'); if (!openWaUrl) throw new Error('Service URLs not configured');
const agentRes = await axios.post(agentUrl, { message: incoming }); 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; const reply = agentRes.data.reply || agentRes.data;
await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply }); await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply });
} catch (err: any) { } catch (err: any) {