add handler mapping
This commit is contained in:
@@ -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