简体   繁体   English

设置状态回调 URL 使用 Python - Flask

[英]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.我正在尝试使用 Python - Flask 来测试 Twilio 中的 StatusCallback,但是,我没有得到任何结果,不确定我遗漏了什么。 I am using ngrok as well.我也在使用 ngrok。

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. Twilio 开发者布道师在这里。

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.当您使用 ngrok 创建隧道时,会设置一个 URL 看起来像https://RANDOMSUBDOMAIN.ngrok.io ,确保您使用的是整个 URL,包括子域。

When ngrok is running there is also a dashboard you can check to ensure that requests are being made to your ngrok URLs.当 ngrok 运行时,还有一个仪表板,您可以检查以确保正在向您的 ngrok URL 发出请求。 You can reach this dashboard at http://localhost:4040 .您可以通过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.最后,您可能会遇到request.values.get和传递键数组的问题。 The get method of request.values only takes a single key, not an array. request.valuesget方法只接受一个键,而不是一个数组。

As you pointed out in the comments, you can use request.form.to_dict(flat=False) to get a dictionary of the parameters instead.正如您在评论中指出的那样,您可以使用request.form.to_dict(flat=False)来获取参数字典。 If you want to destructure that further into separate variables in a single line, you can use itemgetter from the operator module , like this:如果你想在一行中将其进一步解构为单独的变量,你可以使用operator 模块中的itemgetter ,如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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