/** * WhatsApp Webhook Simulator * * Usage: npx tsx apps/api/src/scripts/simulate-webhook.ts [phoneNumberId] [fromPhone] [text] */ const targetUrl = 'http://localhost:8080/v1/whatsapp/webhook'; const phoneNumberId = process.argv[2] || '1029384756'; const fromPhone = process.argv[3] || '33612345678'; const text = process.argv[4] || 'Hello from simulation!'; async function runSimulation() { console.log(`šŸš€ Simulating message to ${targetUrl}...`); console.log(`šŸ“± Phone ID: ${phoneNumberId}`); console.log(`šŸ‘¤ From: ${fromPhone}`); console.log(`šŸ’¬ Text: "${text}"`); const payload = { object: 'whatsapp_business_account', entry: [ { id: 'WHATSAPP_BUSINESS_ACCOUNT_ID', changes: [ { value: { messaging_product: 'whatsapp', metadata: { display_phone_number: '123456789', phone_number_id: phoneNumberId }, contacts: [{ profile: { name: 'Test User' }, wa_id: fromPhone }], messages: [ { from: fromPhone, id: `wamid.HBgLMzM2MzA0Nzg0MDYVAgARGBI1NjI5RkZCMEY3OUM2ODRDOTQA`, timestamp: Math.floor(Date.now() / 1000).toString(), type: 'text', text: { body: text } } ] }, field: 'messages' } ] } ] }; try { const response = await fetch(targetUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', // We simulate the Meta HMAC signature if needed, or disable it in dev 'x-hub-signature-256': 'sha256=fake-signature-for-local-test' }, body: JSON.stringify(payload) }); const data = await response.json() as any; console.log(`\nāœ… Response (${response.status}):`, JSON.stringify(data, null, 2)); if (data.status === 'forwarded') { console.log("\nšŸ“” The message was successfully FORWARDED to the internal worker."); } else if (data.status === 'received') { console.log("\nšŸ“„ The message was successfully QUEUED for local processing."); } } catch (err: any) { console.error("\nāŒ Simulation failed:", err.message); } } runSimulation();