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

@@ -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 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) {