File size: 2,777 Bytes
5220c27
d3a9684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06cb7f3
d3a9684
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


/**
 * 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();