[英]POST request not working in Django IVR made using Plivo
我正在制作 IVR(交互式语音响应)系统。我正在使用 Plivo 制作 IVR。 我遵循了这个用 Python Flask 编写的示例应用程序。 这是制作示例应用程序的链接。
https://www.plivo.com/docs/getting-started/phone-menu-app/
这是 python 烧瓶中名为 ivr() 的存储库和视图方法https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23
你也可以查看代码
@app.route('/response/ivr/', methods=['GET', 'POST'])
def ivr():
response = plivoxml.Response()
if request.method == 'GET':
# GetDigit XML Docs - http://plivo.com/docs/xml/getdigits/
getdigits_action_url = url_for('ivr', _external=True)
getDigits = plivoxml.GetDigits(action=getdigits_action_url,
method='POST', timeout=7, numDigits=1,
retries=1)
getDigits.addSpeak(IVR_MESSAGE)
response.add(getDigits)
response.addSpeak(NO_INPUT_MESSAGE)
return Response(str(response), mimetype='text/xml')
elif request.method == 'POST':
digit = request.form.get('Digits')
if digit == "1":
# Fetch a random joke using the Reddit API.
joke = joke_from_reddit()
response.addSpeak(joke)
elif digit == "2":
# Listen to a song
response.addPlay(PLIVO_SONG)
else:
response.addSpeak(WRONG_INPUT_MESSAGE)
return Response(str(response), mimetype='text/xml')
我只需要在我的 Django IVR 中使用相同的行为。 我只是在 Python Django 中实现所有内容。 这是存储库的链接,上面的 ivr() 方法重命名为 ivr_sample() 在 Python Django 中实现。
https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23
这是代码
@csrf_protect
def ivr_sample(request):
context = {
"working": "yes"
}
response = plivoxml.Response()
print type(request.method) , request.POST.get('Digits')
if request.method == 'GET':
print request.get_host(), request.build_absolute_uri()
getdigits_action_url = request.build_absolute_uri()
getDigits = plivoxml.GetDigits(action=getdigits_action_url, method='POST', timeout=7, numDigits=1, retries=1)
getDigits.addSpeak("Welcome to Sample IVR, Press 0 for sales , Press 1 for support")
response.add(getDigits)
response.addSpeak("Sorry No Input has been received")
return HttpResponse(response, content_type="text/xml")
elif request.method == 'POST':
digit = request.POST.get('Digits')
if (digit == "0" or digit == 0):
response.addSpeak("Hello Welcome to Sample , I am a Sales Guy")
elif (digit == "1" or digit == 1):
response.addSpeak("Hello Welcome to Sample , I am a Support Guy")
else:
response.addSpeak("Wrong Input Received")
return HttpResponse(response, content_type="text/xml")
我可以在我的手机上收听 GET 请求但是当我输入 0 或 1 以便我可以收听所需的消息时。 电话挂断,然后连接关闭。 这意味着 ivr_sample() 方法正在接受 GET 响应,但在我的情况下它没有运行 POST 响应。 基于 Flask 的应用程序运行良好,没有任何问题。
所以我认为Django需要表单中的CSRF保护。 所以我使用了 django 文档中指定的 csrf 装饰器。 这是链接: https : //docs.djangoproject.com/en/1.8/ref/csrf/
但 IVR 仍然无法正常工作。
最糟糕的是我们不能在本地测试东西。 所以我必须进行更正并在线测试。 如果有人在 plivo 之前使用过在 Python Django 中制作 IVR。 请让我知道我错在哪里。
好吧,这只是我在尝试了所有 csrf 装饰器后发现的一个小修复。 在名为 ivr_sample 的视图中。 在@csrf_protect
地方,我只需要使用@csrf_exempt
。 现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.