简体   繁体   English

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

[英]POST request not working in Django IVR made using Plivo

I am making an IVR (Interactive Voice Response) system.I am using Plivo to make IVR.我正在制作 IVR(交互式语音响应)系统。我正在使用 Plivo 制作 IVR。 I have followed this Sample app which is written in Python Flask.我遵循了这个用 Python Flask 编写的示例应用程序。 Here is the link to make the sample app.这是制作示例应用程序的链接。

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

and here is the repository and a view method named ivr() in python flask https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23这是 python 烧瓶中名为 ivr() 的存储库和视图方法https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23

you can also view the code你也可以查看代码

@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')

I just need the same behavior in my Django IVR.我只需要在我的 Django IVR 中使用相同的行为。 I am just implementing everything in Python Django.我只是在 Python Django 中实现所有内容。 Here is the link to the repository and the above ivr() method renamed to ivr_sample() implemented in Python Django.这是存储库的链接,上面的 ivr() 方法重命名为 ivr_sample() 在 Python Django 中实现。

https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23 https://github.com/Chitrank-Dixit/phone-ivr-python/blob/master/app.py#L23

here is the code这是代码

@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")

I am able to listen the GET requests on my phone But when I type 0 or 1 so that I can listen the desired message.我可以在我的手机上收听 GET 请求但是当我输入 0 或 1 以便我可以收听所需的消息时。 The phone Hangs and then the connection gets closed.电话挂断,然后连接关闭。 This means the ivr_sample() method is accepting the GET responses but it is not running the POST responses in my case.这意味着 ivr_sample() 方法正在接受 GET 响应,但在我的情况下它没有运行 POST 响应。 The Flask based application is working fine with no issues.基于 Flask 的应用程序运行良好,没有任何问题。

So I thought that Django needs CSRF protection in forms.所以我认为Django需要表单中的CSRF保护。 So I used csrf decorator as specified on django documentation.所以我使用了 django 文档中指定的 csrf 装饰器。 here is the link: https://docs.djangoproject.com/en/1.8/ref/csrf/这是链接: https : //docs.djangoproject.com/en/1.8/ref/csrf/

But Still the IVR is not working.但 IVR 仍然无法正常工作。

The worst thing is we can not test things locally.最糟糕的是我们不能在本地测试东西。 So I have to make correction and test it online.所以我必须进行更正并在线测试。 If anyone used before plivo to make IVRs in Python Django.如果有人在 plivo 之前使用过在 Python Django 中制作 IVR。 Please let me know where I am wrong.请让我知道我错在哪里。

well this was just a small fix I figured out after trying all the csrf decorators.好吧,这只是我在尝试了所有 csrf 装饰器后发现的一个小修复。 In the view named ivr_sample.在名为 ivr_sample 的视图中。 At place of @csrf_protect I just need to use @csrf_exempt .@csrf_protect地方,我只需要使用@csrf_exempt Now everything is working perfectly.现在一切正常。

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

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