Search code examples
pythonflasktwiliotwilio-programmable-chattwilio-python

Always receiving 400:Bad request when Twilio webhook is configured


I want to maintain seperate twilio phone unsubscribers list and this can be done when webhook is configured via twilio console to receive incoming messages. I would parse only those messages when some one types "STOP". I have successfully configured webhook enter image description here

Now from my phone when i type "STOP" to my twilio number, I am receiving always bad request. My code looks as follows

@app.route('/twilio/unsubscribes_incremental', methods=['POST', 'GET'])
def phone_unsubscribes_incremental():
    print("start")
    print("The arguments are ", request.args)
    payload = request.get_json(force=True)
    print("The payload is ", payload)
    #resp = MessagingResponse()
    if payload.get('Body') in twilio_unsubscribe_list:
        stream_data_to_bq(payload)
    #resp.message("")
    #return str(resp)
    return jsonify({"status":"ok"})

My python console shows as follows enter image description here

My ngrok console shows as follows enter image description here

My twilio console logs shows as follows enter image description here

For some reason iam unable to parse request object sent to my webhook. This account is in free trial. Can any one point out to me right documentation to parse incoming messages.


Solution

  • The webhook is application/x-www-form-urlencoded

    Webhook FAQ

    For inbound text messages, Twilio will send an HTTP POST request to your server with a body that uses the application/x-www-form-urlencoded encoding. View the list of parameters sent in that request.