Reputation: 383
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
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
My ngrok console shows as follows
My twilio console logs shows as follows
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.
Upvotes: 2
Views: 1125
Reputation: 73029
Twilio developer evangelist here.
As Alan points out, Twilio webhook requests send data in the form application/x-www-form-urlencoded
, the same format that a web form will post data. It is not JSON. Twilio also expects your application's response to be application/xml
.
So, you should read the data from request.form
and, since it doesn't look like you are expecting to tell Twilio to do anything further with the request, return an empty <Response>
TwiML element.
Something like this should work:
@app.route('/twilio/unsubscribes_incremental', methods=['POST', 'GET'])
def phone_unsubscribes_incremental():
print("start")
print("The arguments are ", request.args)
payload = request.form
print("The payload is ", payload)
if payload.get('Body') in twilio_unsubscribe_list:
stream_data_to_bq(payload)
resp = MessagingResponse()
return str(resp), { 'Content-Type': 'application/xml' }
Upvotes: 4
Reputation: 10771
The webhook is application/x-www-form-urlencoded
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.
Upvotes: 2