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

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

@@ -0,0 +1,149 @@
import express from 'express';
import axios from 'axios';
import { WhatsAppMessage } from './types';
import { getHandler, Handler } from './chatHandlers';
const app = express();
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', 10),
RETRY_MS: parseInt(process.env.RETRY_MS || '2000', 10)
};
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() {
for (let i = 1; i <= config.MAX_ATTEMPTS; i++) {
try {
await axios.get(`${config.API_URL}/api-docs`);
log('info', '🟢 nucleo-whatsapp ready');
return;
} catch {
log('warn', `Gateway not responding (attempt ${i}/${config.MAX_ATTEMPTS})…`);
await new Promise(r => setTimeout(r, config.RETRY_MS));
}
}
throw new Error('nucleo-whatsapp did not respond in time');
}
async function clearWebhooks() {
try {
const { data } = await axios.post(`${config.API_URL}/listWebhooks`);
const hooks = data?.response || [];
if (!hooks.length) {
log('info', 'No existing webhooks to remove');
return;
}
log('info', `Removing ${hooks.length} webhooks…`);
const results = await Promise.allSettled(
hooks.map((h: any) => axios.post(`${config.API_URL}/removeWebhook`, { args: { webhookId: h.id } }))
);
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}`);
} else {
log('warn', `⚠️ Failed to remove webhook ${id}`);
}
});
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: any) {
log('error', 'Failed cleaning webhooks:', e.response?.data || e.message);
}
}
async function registerWebhook() {
const url = `http://whatsapp-router:${port}/webhook`;
const eventConfig = {
onAck: false,
onAddedToGroup: true,
onAnyMessage: true,
onBattery: true,
onBroadcast: true,
onButton: true,
onCallState: false,
onChatDeleted: true,
onChatOpened: true,
onChatState: true,
onContactAdded: true,
onGlobalParticipantsChanged: true,
onGroupApprovalRequest: true,
onGroupChange: true,
onIncomingCall: false,
onLabel: true,
onLogout: true,
onMessage: false,
onMessageDeleted: true,
onNewProduct: true,
onOrder: true,
onPlugged: false,
onPollVote: true,
onReaction: true,
onRemovedFromGroup: false,
onStateChanged: false,
onStory: false,
};
const events = Object.entries(eventConfig)
.filter(([_, enabled]) => enabled)
.map(([event]) => event);
const { data } = await axios.post(`${config.API_URL}/registerWebhook`, {
args: { url, events }
});
log('info', '✔️ Webhook registered:', data);
}
app.use(express.json());
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);
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);
});
app.listen(port, async () => {
console.log(`WhatsApp router listening on ${port}`);
try {
await waitForGateway();
await clearWebhooks();
await registerWebhook();
} 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;
}