Trouble Sending Messages to Numbers Outside Twilio Sandbox via WhatsApp API

106 Views Asked by At

I'm integrating the Twilio WhatsApp API into a Flask application to send automated messages. While I can send messages to my own WhatsApp number that's subscribed to the sandbox with the join <given code> command, I'm unable to send messages to other numbers. The peculiar part is that there are no errors shown in the console or Twilio's debugger to indicate what might be going wrong.

Here's a snippet from the /bot endpoint in my Flask app which is supposed to handle incoming messages and send replies:

@app.route('/bot', methods=['POST'])
def bot():
    # ... [code to handle incoming messages] ...

    # Prepare the Twilio response
    twilio_response = MessagingResponse()
    twilio_response.message(latest_response)

    # ... [more code] ...

    # Attempt to send a message
    try:
        message = twilio_client.messages.create(
            body="Your appointment is coming up on July 21 at 3PM",
            from_="whatsapp:14155238886",  # This is the Twilio phone number
            to='whatsapp:+44074XXX'   # Trying to send to this number outside sandbox
        )
        print(f"Message sent to whatsapp:+44074XXX. Message SID: {message.sid}")
    except Exception as e:
        print(f"Error occurred: {e}")

    return str(twilio_response)

This /bot endpoint is triggered after receiving a webhook from WhatsApp. It's supposed to send a reply based on the message processed by the assistant. This is working perfectly for my personal number, which is registered with the sandbox:

twilio_response = MessagingResponse()
twilio_response.message(latest_response)

However, when using twilio_client.messages.create to send a message outside of the sandbox environment, I don't receive any errors, and yet the message does not get delivered.

The console prints the following, suggesting the message is being created:

Message sent to whatsapp:+440746XXXX. Message SID: SM17c2671921f3d77d68640a904c687223

Is there an additional step I'm missing, or is there something else I need to configure to send messages to numbers outside of the Twilio sandbox environment?

1

There are 1 best solutions below

0
IObert On

Your confusion is understandable! However, it's important to note that the Twilio Sandbox for WhatsApp is designed for development and testing. It is intended to allow you to test your integration before moving to production.

During the phase in which you use the sandbox, you indeed are able to send messages only to numbers that have joined the sandbox. This is a protective measure to ensure you don't accidentally start sending out lots of messages to numbers that haven't consented to receive them.

When you've finished testing your application and are ready to move it to production, you'll need to request access to the WhatsApp Business API. Once you've done that, you'll be able to send messages to any WhatsApp number from your application.