简体   繁体   中英

Setting status callback URL using Python - Flask

I am trying to use Python - Flask to test the StatusCallback in Twilio, however, I am not getting any results, not sure what I am missing. I am using ngrok as well.

This is the code:

from flask import Flask, request, abort
import logging

logging.basicConfig(level=logging.INFO)
app = Flask(__name__)

@app.route('/webhook', methods =['POST', 'GET'])
def webhook():
    status=request.values.get(['CallSid', 'From', 'To', 'Direction'])
    logging.info('Status: {}'.format(status))
    return ('', 204)

if __name__ == '__main__':
    app.run(debug=True)

When I make a call, from the image I attached, you will notice I am not getting any results. Could you please advise what I may be missing? Thanks.

依恋

Twilio developer evangelist here.

When you create a tunnel with ngrok, a URL is set up that looks like https://RANDOMSUBDOMAIN.ngrok.io , make sure you are using the entire URL, including the subdomain.

When ngrok is running there is also a dashboard you can check to ensure that requests are being made to your ngrok URLs. You can reach this dashboard at http://localhost:4040 . You can also use this to check the request parameters that are being sent.

Finally, you might have trouble with request.values.get and passing an array of keys. The get method of request.values only takes a single key, not an array.

As you pointed out in the comments, you can use request.form.to_dict(flat=False) to get a dictionary of the parameters instead. If you want to destructure that further into separate variables in a single line, you can use itemgetter from the operator module , like this:

from operator import itemgetter

@app.route('/webhook', methods =['POST', 'GET'])
def webhook():
    parameters=request.form.to_dict(flat=False)
    CallSid, From, To, Direction = itemgetter('CallSid', 'From', 'To', 'Direction')(parameters)
    logging.info('CallSid: {}'.format(CallSid))
    return ('', 204)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM