Add HelloWorld debug agent and per-chat handler

This commit is contained in:
josedario87
2025-06-04 21:51:05 -06:00
parent dec4922d42
commit 8d2e8f4854
14 changed files with 582 additions and 35 deletions

View File

@@ -3,5 +3,6 @@ WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm install --production
COPY . .
RUN npm run build
EXPOSE 3001
CMD ["node","index.js"]
CMD ["node","dist/index.js"]

View File

@@ -11,6 +11,119 @@
"dependencies": {
"axios": "^1.5.0",
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.11.19",
"typescript": "^5.4.5"
}
},
"node_modules/@types/body-parser": {
"version": "1.19.5",
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz",
"integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/connect": "*",
"@types/node": "*"
}
},
"node_modules/@types/connect": {
"version": "3.4.38",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz",
"integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/express": {
"version": "4.17.22",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.22.tgz",
"integrity": "sha512-eZUmSnhRX9YRSkplpz0N+k6NljUUn5l3EWZIKZvYzhvMphEuNiyyy1viH/ejgt66JWgALwC/gtSUAeQKtSwW/w==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/body-parser": "*",
"@types/express-serve-static-core": "^4.17.33",
"@types/qs": "*",
"@types/serve-static": "*"
}
},
"node_modules/@types/express-serve-static-core": {
"version": "4.19.6",
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.19.6.tgz",
"integrity": "sha512-N4LZ2xG7DatVqhCZzOGb1Yi5lMbXSZcmdLDe9EzSndPV2HpWYWzRbaerl2n27irrm94EPpprqa8KpskPT085+A==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*",
"@types/qs": "*",
"@types/range-parser": "*",
"@types/send": "*"
}
},
"node_modules/@types/http-errors": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz",
"integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/mime": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz",
"integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/node": {
"version": "20.17.57",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.57.tgz",
"integrity": "sha512-f3T4y6VU4fVQDKVqJV4Uppy8c1p/sVvS3peyqxyWnzkqXFJLRU7Y1Bl7rMS1Qe9z0v4M6McY0Fp9yBsgHJUsWQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.2"
}
},
"node_modules/@types/qs": {
"version": "6.14.0",
"resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.14.0.tgz",
"integrity": "sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/range-parser": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz",
"integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/send": {
"version": "0.17.4",
"resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz",
"integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/mime": "^1",
"@types/node": "*"
}
},
"node_modules/@types/serve-static": {
"version": "1.15.7",
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.7.tgz",
"integrity": "sha512-W8Ym+h8nhuRwaKPaDw34QUkwsGi6Rc4yYqvKFo5rm2FUEhCFbzVWrxXUxuKK8TASjWsysJY0nsmNCGhCOIsrOw==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/http-errors": "*",
"@types/node": "*",
"@types/send": "*"
}
},
"node_modules/accepts": {
@@ -912,6 +1025,27 @@
"node": ">= 0.6"
}
},
"node_modules/typescript": {
"version": "5.8.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.3.tgz",
"integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==",
"dev": true,
"license": "MIT"
},
"node_modules/unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",

View File

@@ -1,10 +1,19 @@
{
"name": "whatsapp-router",
"version": "1.0.0",
"main": "index.js",
"main": "dist/index.js",
"license": "MIT",
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
},
"dependencies": {
"express": "^4.18.2",
"axios": "^1.5.0"
},
"devDependencies": {
"typescript": "^5.4.5",
"@types/node": "^20.11.19",
"@types/express": "^4.17.21"
}
}

View File

@@ -0,0 +1,15 @@
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): Handler | undefined {
if (!chatId) return defaultUrl;
return chatHandlers[chatId] || defaultUrl;
}

View File

@@ -0,0 +1,3 @@
export async function helloWorldAgent(): Promise<string> {
return 'hello world';
}

View File

@@ -1,19 +1,24 @@
const express = require('express');
const axios = require('axios');
import express from 'express';
import axios from 'axios';
import { WhatsAppMessage } from './types';
import { getHandler, Handler } from './chatHandlers';
const app = express();
const port = process.env.PORT || 3001;
const agentUrl = process.env.LLM_AGENT_URL;
const openWaUrl = process.env.OPEN_WA_URL;
const port = Number(process.env.PORT) || 3001;
const agentUrl = process.env.LLM_AGENT_URL as string | undefined;
const openWaUrl = process.env.OPEN_WA_URL as string | undefined;
const config = {
API_URL: openWaUrl,
MAX_ATTEMPTS: parseInt(process.env.MAX_ATTEMPTS || '100', 100),
API_URL: openWaUrl || '',
MAX_ATTEMPTS: parseInt(process.env.MAX_ATTEMPTS || '100', 10),
RETRY_MS: parseInt(process.env.RETRY_MS || '2000', 10)
};
function log(level, ...args) {
console[level] ? console[level](...args) : console.log(...args);
function log(level: keyof Console | 'info' | 'warn' | 'error' | 'debug', ...args: unknown[]) {
const logger = (console as any)[level] as ((...args: unknown[]) => void) | undefined;
if (logger) logger(...args);
else console.log(...args);
}
async function waitForGateway() {
@@ -41,10 +46,10 @@ async function clearWebhooks() {
log('info', `Removing ${hooks.length} webhooks…`);
const results = await Promise.allSettled(
hooks.map(h => axios.post(`${config.API_URL}/removeWebhook`, { args: { webhookId: h.id } }))
hooks.map((h: any) => axios.post(`${config.API_URL}/removeWebhook`, { args: { webhookId: h.id } }))
);
results.forEach((r, i) => {
results.forEach((r: PromiseSettledResult<any>, i: number) => {
const id = hooks[i].id;
if (r.status === 'fulfilled' && r.value?.data?.response === true) {
log('debug', `✔️ Removed webhook ${id}`);
@@ -53,9 +58,9 @@ async function clearWebhooks() {
}
});
const ok = results.filter(r => r.status === 'fulfilled' && r.value?.data?.response === true).length;
const ok = results.filter((r: PromiseSettledResult<any>) => r.status === 'fulfilled' && (r as PromiseFulfilledResult<any>).value?.data?.response === true).length;
log('info', `Cleanup OK (${ok}/${hooks.length} removed)`);
} catch (e) {
} catch (e: any) {
log('error', 'Failed cleaning webhooks:', e.response?.data || e.message);
}
}
@@ -105,15 +110,28 @@ async function registerWebhook() {
app.use(express.json());
app.post('/webhook', async (req, res) => {
const { message, text, from } = req.body;
app.post('/webhook', async (req: express.Request, res: express.Response) => {
const { message, text, from } = req.body as {
message?: WhatsAppMessage;
text?: string;
from?: string;
};
const incoming = message || text;
try {
if (!incoming) return res.sendStatus(200);
const agentRes = await axios.post(agentUrl, { message: incoming });
const reply = agentRes.data.reply || agentRes.data;
await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply });
} catch (err) {
if (!openWaUrl) throw new Error('Service URLs not configured');
const chatId = (message && message.chatId) || from;
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);
}
res.sendStatus(200);
@@ -125,7 +143,7 @@ app.listen(port, async () => {
await waitForGateway();
await clearWebhooks();
await registerWebhook();
} catch (err) {
} catch (err: any) {
log('error', 'Webhook setup failed:', err.message);
}
});

View File

@@ -0,0 +1,194 @@
export interface ProfilePicThumb {
eurl?: string;
id?: string;
img?: string;
imgFull?: string;
tag?: string;
}
export interface Sender {
id: string;
name: string;
shortName: string;
pushname: string;
type: string;
isBusiness: boolean;
isEnterprise: boolean;
isSmb: boolean;
isContactSyncCompleted: number;
disappearingModeDuration: number;
disappearingModeSettingTimestamp: number;
textStatusLastUpdateTime: number;
syncToAddressbook: boolean;
formattedName: string;
isMe: boolean;
isMyContact: boolean;
isPSA: boolean;
isUser: boolean;
status?: string;
isVerified: boolean;
isWAContact: boolean;
profilePicThumbObj?: ProfilePicThumb;
msgs: any;
}
export interface Contact {
id: string;
name: string;
shortName: string;
pushname: string;
type: string;
isBusiness: boolean;
isEnterprise: boolean;
isSmb: boolean;
isContactSyncCompleted: number;
disappearingModeDuration: number;
disappearingModeSettingTimestamp: number;
textStatusLastUpdateTime: number;
syncToAddressbook: boolean;
formattedName: string;
isMe: boolean;
isMyContact: boolean;
isPSA: boolean;
isUser: boolean;
isVerified: boolean;
isWAContact: boolean;
profilePicThumbObj?: Partial<ProfilePicThumb>;
msgs: any;
}
export interface Chat {
id: string;
pendingMsgs: boolean;
lastReceivedKey?: {
fromMe: boolean;
remote: string;
id: string;
_serialized: string;
};
t: number;
unreadCount: number;
unreadDividerOffset: number;
archive: boolean;
isReadOnly: boolean;
isLocked: boolean;
muteExpiration: number;
isAutoMuted: boolean;
name: string;
notSpam: boolean;
pin: number;
ephemeralDuration: number;
ephemeralSettingTimestamp: number;
disappearingModeInitiator: string;
disappearingModeTrigger: string;
createdLocally: boolean;
unreadMentionsOfMe: any[];
unreadMentionCount: number;
hasUnreadMention: boolean;
archiveAtMentionViewedInDrawer: boolean;
hasChatBeenOpened: boolean;
tcToken: Record<string, unknown>;
tcTokenTimestamp: number;
tcTokenSenderTimestamp: number;
endOfHistoryTransferType: number;
pendingInitialLoading: boolean;
chatlistPreview?: any;
unreadEditTimestampMs: number;
celebrationAnimationLastPlayed: number;
hasRequestedWelcomeMsg: boolean;
msgs: any;
canSend: boolean;
isGroup: boolean;
pic?: string;
formattedTitle: string;
contact: Contact;
groupMetadata: any;
presence?: {
id: string;
chatstates: any[];
};
isOnline: boolean;
participantsCount: number;
}
export interface WhatsAppMessage {
id: string;
viewed: boolean;
body: string;
type: string;
t: number;
notifyName: string;
from: string;
to: string;
author: string | null;
invis: boolean;
isNewMsg: boolean;
star: boolean;
kicNotified: boolean;
recvFresh: boolean;
isFromTemplate: boolean;
thumbnail?: string;
pollInvalidated: boolean;
isSentCagPollCreation: boolean;
latestEditMsgKey: any;
latestEditSenderTimestampMs: any;
mentionedJidList: any[];
groupMentions: any[];
isEventCanceled: boolean;
eventInvalidated: boolean;
isVcardOverMmsDocument: boolean;
labels: any[];
hasReaction: boolean;
ephemeralDuration: number;
ephemeralSettingTimestamp: number;
disappearingModeInitiator: string;
disappearingModeTrigger: string;
viewMode: string;
productHeaderImageRejected: boolean;
lastPlaybackProgress: number;
isDynamicReplyButtonsMsg: boolean;
isCarouselCard: boolean;
parentMsgId: any;
callSilenceReason: any;
isVideoCall: boolean;
callDuration: any;
callParticipants: any;
isMdHistoryMsg: boolean;
stickerSentTs: number;
isAvatar: boolean;
lastUpdateFromServerTs: number;
invokedBotWid: any;
bizBotType: any;
botResponseTargetId: any;
botPluginType: any;
botPluginReferenceIndex: any;
botPluginSearchProvider: any;
botPluginSearchUrl: any;
botPluginSearchQuery: any;
botPluginMaybeParent: boolean;
botReelPluginThumbnailCdnUrl: any;
botMessageDisclaimerText: any;
botMsgBodyType: any;
reportingTokenInfo: any;
requiresDirectConnection: boolean;
bizContentPlaceholderType: any;
hostedBizEncStateMismatch: boolean;
senderOrRecipientAccountTypeHosted: boolean;
placeholderCreatedWhenAccountIsHosted: boolean;
device: number;
local: boolean;
fromMe: boolean;
mId: string;
sender: Sender;
senderId: any;
timestamp: number;
content: string;
isGroupMsg: boolean;
isQuotedMsgAvailable: boolean;
isMedia: boolean;
chat: Chat;
isOnline: boolean;
chatId: string;
mediaData: Record<string, unknown>;
text: string;
}

View File

@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"lib": ["es2020"],
"moduleResolution": "node"
},
"include": ["src"]
}