add handler mapping
This commit is contained in:
@@ -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
|
||||
|
||||
9
whatsapp-router/src/chatHandlers.ts
Normal file
9
whatsapp-router/src/chatHandlers.ts
Normal 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;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user