diff --git a/README.md b/README.md index acb4064..c579a24 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ 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. Optionally edit `whatsapp-router/src/chatHandlers.ts` to map specific chat IDs to different handler URLs. +2. Optionally edit `whatsapp-router/src/chatHandlers.ts` to map specific chat IDs to different handler URLs or local handlers. By default the chat ID `50498554225@c.us` is mapped to a built‑in *HelloWorld* agent that replies "hello world" for testing. 3. Run: ```bash diff --git a/whatsapp-router/src/chatHandlers.ts b/whatsapp-router/src/chatHandlers.ts index efc7be8..623aa07 100644 --- a/whatsapp-router/src/chatHandlers.ts +++ b/whatsapp-router/src/chatHandlers.ts @@ -1,9 +1,16 @@ -export const chatHandlers: Record = { - // Example mapping: +import { helloWorldAgent } from './helloAgent'; +import { WhatsAppMessage } from './types'; + +export type Handler = string | ((msg: WhatsAppMessage | string) => Promise); + +export const chatHandlers: Record = { + '50498554225@c.us': helloWorldAgent, + // Add other mappings like: // '50496210031@c.us': 'http://llm-agent:8000' }; -export function getHandler(chatId: string | undefined, defaultUrl?: string): string | undefined { +export function getHandler(chatId: string | undefined, defaultUrl?: string): Handler | undefined { + if (!chatId) return defaultUrl; return chatHandlers[chatId] || defaultUrl; } diff --git a/whatsapp-router/src/helloAgent.ts b/whatsapp-router/src/helloAgent.ts new file mode 100644 index 0000000..5d0f2a0 --- /dev/null +++ b/whatsapp-router/src/helloAgent.ts @@ -0,0 +1,3 @@ +export async function helloWorldAgent(): Promise { + return 'hello world'; +} diff --git a/whatsapp-router/src/index.ts b/whatsapp-router/src/index.ts index 3e15ebc..8d6b2b8 100644 --- a/whatsapp-router/src/index.ts +++ b/whatsapp-router/src/index.ts @@ -1,7 +1,8 @@ import express from 'express'; import axios from 'axios'; import { WhatsAppMessage } from './types'; -import { getHandler } from './chatHandlers'; +import { getHandler, Handler } from './chatHandlers'; + const app = express(); @@ -111,17 +112,27 @@ async function registerWebhook() { app.use(express.json()); app.post('/webhook', async (req: express.Request, res: express.Response) => { - const { message, text, from } = req.body as { message?: WhatsAppMessage; text?: string; from?: string }; + const { message, text, from } = req.body as { + message?: WhatsAppMessage; + text?: string; + from?: string; + }; + const incoming = message || text; try { if (!incoming) return res.sendStatus(200); 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 }); + 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 }); + reply = agentRes.data.reply || agentRes.data; + } else { + reply = await handler(incoming); + } + await axios.post(`${openWaUrl}/sendText`, { args: { to: from, content: reply } }); } catch (err: any) { console.error('Error processing message', err.message); }