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

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