POST 请求在使用 Plivo 发出的 Django IVR 中不起作用

POST request not working in Django IVR made using Plivo

我正在制作 IVR(交互式语音响应)system.I我正在使用 Plivo 制作 IVR。我遵循了这个用 Python Flask 编写的示例应用程序。这是制作示例应用程序的 link。

https://www.plivo.com/docs/getting-started/phone-menu-app/

这是 python flask 中的存储库和名为 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 中实现所有内容。 这是存储库的 link,上面的 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")

我可以在 phone 上收听 GET 请求,但是当我键入 0 或 1 时,我可以收听所需的消息。 phone 挂起,然后连接关闭。这意味着 ivr_sample() 方法正在接受 GET 响应,但在我的例子中它不是 运行 POST 响应。基于 Flask 的应用程序运行良好,没有任何问题。

所以我认为Django需要表单中的CSRF保护。所以我使用了 django 文档中指定的 csrf 装饰器。 这是 link:https://docs.djangoproject.com/en/1.8/ref/csrf/

但 IVR 仍然无法正常工作。

最糟糕的是我们无法在本地进行测试。所以只好改正在线测试了。如果有人在 plivo 之前使用过在 Python Django 中制作 IVR。请让我知道我哪里错了。

好吧,这只是我在尝试所有 csrf 装饰器后想出的一个小修复。在名为 ivr_sample 的视图中。在 @csrf_protect 的位置,我只需要使用 @csrf_exempt。现在一切正常。