Why I receive messages from live users through the webhook registered with app that still in development?
1

I have an app currently in development and want to receive messages only from test users. Nevertheless, webhook sends messages from random people to my app. It seems those people are related to the other Whatsapp app and phone number attached to the business account. How can I limit the excess to the webhook with only test users?

Andrey
Asked about 5 months ago
Selected Answer
1

To ensure that your app receives messages only from test users, you can implement a mechanism to filter messages based on the phone_number_id provided in the webhook notifications. Here’s a step-by-step guide on how you can achieve this:

1- Extract the phone_number_id: When you receive a webhook notification, extract the phone_number_id to determine which phone number the message is associated with. You can do this using the following path in the JSON payload:

const phoneNumberId = req.body.entry?.[0]?.changes[0]?.value?.metadata?.phone_number_id;

2- Set Environment Variables: Maintain a configuration that specifies which phone numbers are for production and which are for testing. This can be done using environment variables or a configuration file. For example:

{
    "development": {
        "phone_numbers": ["TEST_PHONE_NUMBER_1", "TEST_PHONE_NUMBER_2"]
    },
    "production": {
        "phone_numbers": ["PROD_PHONE_NUMBER_1", "PROD_PHONE_NUMBER_2"]
    }
}

3- Filter Messages Based on Environment: In your webhook handler, check the phone_number_id against your configuration to determine if the message should be processed. Here’s an example in JavaScript:

const environment = process.env.NODE_ENV; // 'development' or 'production'
const config = require('./config.json'); // Your configuration file

const phoneNumberId = req.body.entry?.[0]?.changes[0]?.value?.metadata?.phone_number_id;

if (config[environment].phone_numbers.includes(phoneNumberId)) {
    // Process the message
} else {
    // Ignore the message
}

4- Test and Validate: Ensure your configuration is correct and test the filtering mechanism to make sure only messages from the designated test phone numbers are processed in the development environment.

By implementing this approach, you can effectively limit the messages processed by your app to only those from test users, ensuring that messages from random people do not interfere with your development process.

I hope this helps! If you have any further questions or need additional clarification, feel free to ask.

May 27 at 5:34 AM
Luis Henrique