Add webhook forwarder for bi-directional whatsapp

This commit is contained in:
Deep Koluguri 2026-06-19 15:33:31 -04:00
parent db1dfdaa95
commit a75fcfd67d
1 changed files with 27 additions and 2 deletions

View File

@ -74,9 +74,11 @@ client.on('disconnected', (reason) => {
io.emit('disconnected', reason);
});
const AGENTS_API_URL = process.env.AGENTS_API_URL || 'http://agents-api.ai-agents.svc.cluster.local:8000/webhook';
// Incoming messages handler
client.on('message', async msg => {
if (ENABLE_INCOMING_MESSAGES) {
client.on('message_create', async msg => {
if (!msg.fromMe && ENABLE_INCOMING_MESSAGES) {
console.log(`[WhatsApp Bot] Received message from ${msg.from}: ${msg.body}`);
io.emit('incoming_message', { from: msg.from, body: msg.body });
}
@ -86,6 +88,29 @@ client.on('message', async msg => {
msg.reply('pong');
}
}
// Forward @bot messages to agents-runtime orchestrator
const text = msg.body.trim();
if (text.toLowerCase().startsWith('@bot')) {
console.log(`[WhatsApp Bot] Bot command detected: ${text}`);
// Target Jid is where the bot should reply.
const targetJid = msg.fromMe ? msg.to : msg.from;
try {
await fetch(AGENTS_API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: text,
sender: msg.author || msg.from,
targetJid: targetJid
})
});
} catch (err) {
console.error('[WhatsApp Bot] Failed to forward message to agents-runtime:', err.message);
}
}
});
// Real-time connections