edtech / apps /api /src /scripts /simulate-webhook.ts
CognxSafeTrack
fix: explicit type casting for simulation response
06cb7f3
/**
* 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();