Merge pull request #7 from josedario87/codex/change-repo-to-typescript-and-create-type-for-onmessage-even
Convert services to TypeScript
This commit is contained in:
@@ -11,7 +11,7 @@ All services can be launched together with `docker-compose`.
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
1. Configure the URL of your LLM agent in `docker-compose.yml` (`LLM_AGENT_URL`).
|
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:
|
3. Run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
export const chatHandlers: Record<string, string> = {
|
import { helloWorldAgent } from './helloAgent';
|
||||||
// Example mapping:
|
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'
|
// '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;
|
if (!chatId) return defaultUrl;
|
||||||
return chatHandlers[chatId] || 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 express from 'express';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { WhatsAppMessage } from './types';
|
import { WhatsAppMessage } from './types';
|
||||||
import { getHandler } from './chatHandlers';
|
import { getHandler, Handler } from './chatHandlers';
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@@ -111,17 +112,27 @@ async function registerWebhook() {
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.post('/webhook', async (req: express.Request, res: express.Response) => {
|
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;
|
const incoming = message || text;
|
||||||
try {
|
try {
|
||||||
if (!incoming) return res.sendStatus(200);
|
if (!incoming) return res.sendStatus(200);
|
||||||
if (!openWaUrl) throw new Error('Service URLs not configured');
|
if (!openWaUrl) throw new Error('Service URLs not configured');
|
||||||
const chatId = (message && message.chatId) || from;
|
const chatId = (message && message.chatId) || from;
|
||||||
const handlerUrl = getHandler(chatId, agentUrl);
|
const handler = getHandler(chatId, agentUrl);
|
||||||
if (!handlerUrl) throw new Error('No handler configured');
|
if (!handler) throw new Error('No handler configured');
|
||||||
const agentRes = await axios.post(handlerUrl, { message: incoming });
|
let reply: string;
|
||||||
const reply = agentRes.data.reply || agentRes.data;
|
if (typeof handler === 'string') {
|
||||||
await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply });
|
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) {
|
} catch (err: any) {
|
||||||
console.error('Error processing message', err.message);
|
console.error('Error processing message', err.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user