Merge branch 'main' of https://github.com/josedario87/conversation-layer
All checks were successful
Deploy conversation layer / deploy (push) Successful in 19s
All checks were successful
Deploy conversation layer / deploy (push) Successful in 19s
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
export const chatHandlers: Record<string, string> = {
|
||||
// Example mapping:
|
||||
import { helloWorldAgent } from './helloAgent';
|
||||
import { WhatsAppMessage } from './types';
|
||||
|
||||
export type Handler = string | ((msg: WhatsAppMessage | string) => Promise<string>);
|
||||
|
||||
export const chatHandlers: Record<string, Handler> = {
|
||||
'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;
|
||||
}
|
||||
|
||||
3
whatsapp-router/src/helloAgent.ts
Normal file
3
whatsapp-router/src/helloAgent.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export async function helloWorldAgent(): Promise<string> {
|
||||
return 'hello world';
|
||||
}
|
||||
@@ -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,9 +112,12 @@ async function registerWebhook() {
|
||||
app.use(express.json());
|
||||
|
||||
app.post('/webhook', async (req: express.Request, res: express.Response) => {
|
||||
log('info', 'webhook received', req.body);
|
||||
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;
|
||||
|
||||
if (incoming) {
|
||||
@@ -126,11 +130,16 @@ app.post('/webhook', async (req: express.Request, res: express.Response) => {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user