Search code examples
flasktwilio

Body is not being send to status callback URL


Using Flask and Twilio, I´m trying to receive user's message sent to a Twilio WhtsApp number. I´m using WhatsApp Sandbox. The problem is, the message sent by the user to the Twilio phone number is not being send to the callback URL. In order to illustrate this problem, this is app.py:

@app.route("/chatbot/receive", methods=["GET", "POST"])
def reply_whatsapp():
    print(request.values)
    print(request.form.get('body'))

This is the output:

>>> CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('SmsSid', 'SMaaaaa...'), ('SmsStatus', 'read'), ('MessageStatus', 'read'), ('ChannelToAddress', '+592XXXXXXXXX'), ('To', 'whatsapp:+592XXXXXXXXX'), ('ChannelPrefix', 'whatsapp'), ('MessageSid', 'SMaaaaa...'), ('AccountSid', 'ACssssssssssss...'), ('From', 'whatsapp:+14155238886'), ('ApiVersion', '2010-04-01'), ('ChannelInstallSid', 'WhatsAppChannelInstalled')])])  
>>> None

Notes:

I´m using a free account

I'm using ngrok to handle the callback URL

Note Sandbox configuration says "status callback URL", so my idea is this web hook is not sending body in purpose

enter image description here


Solution

  • I think you're mixing two different callbacks here. There is a Status Callback and A Message comes in callback.

    The purpose of the status callback to track the delivery status of an outbound Twilio message.

    You currently use the chatbot/receive URL (renaming this endpoint might be a good idea as it suggests a different purpose)

    The other callback is for responding to incoming messages. This is what you are looking for and these requests contain a body parameter.

    You currently use the demo-reply endpoint for this.