I am making an IVR (Interactive Voice Response) system.I am using Plivo to make IVR. I have followed this Sample app which is written in Python Flask. Here is the link to make the sample 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
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. I am just implementing everything in Python Django. Here is the link to the repository and the above ivr() method renamed to ivr_sample() implemented in Python Django.
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. 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. The Flask based application is working fine with no issues.
So I thought that Django needs CSRF protection in forms. So I used csrf decorator as specified on django documentation. here is the link: https://docs.djangoproject.com/en/1.8/ref/csrf/
But Still the IVR is not working.
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. Please let me know where I am wrong.
well this was just a small fix I figured out after trying all the csrf decorators. In the view named ivr_sample. At place of @csrf_protect
I just need to use @csrf_exempt
. Now everything is working perfectly.