[英]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.