问题
I am trying to make a simple voice IVR for my project using nexmo API. Please refer to the image for exact clearance. basic idea by flow diagram of what I am trying to do as ivr.
Now the problem occurs that I can't able to make a loop to return to the mainMenu if digit pressed was wrong. The problem till I get understood is in GET and POST method of my function.
from flask import Flask, request, jsonify, Response, render_template
import nexmo
from pprint import pprint
from random import choice
app = Flask(__name__)
HOST = "localhost"
PORT = 5000
id_no = choice(range(1001, 10000))
name = ''
phone_no = ''
email_id = ''
UUID = ''
client = ''
flag_url = ''
counter = -1
call_flow = 0
main_ncco = [ [], [{ "action":"talk", "voiceName": "Amy", "text":"You have not pressed any key yet. Please press a correct key." }],
[ { "action": "talk", "voiceName":"Amy", "text":"You have reached the maximum limits of trial. Please try up again. Thanks for calling."} ],
[ { "action":"talk", "voiceName": "Amy", "text":"You have pressed a wrong key. Please press a correct key." }] ]
@app.route("/")
def home_page():
"""Returns a webpage on ngrok url address to collect user entries."""
return render_template("call_inputs.html")
@app.route("/", methods=["POST"])
def home_page_answer():
"""Fetches query for the form entries,
used this function because form action is set to ngrok url with a post
method. Collects all form entries and saves to global variables.
In response, returns the next webpage named returned_output.html"""
global name, phone_no, email_id
name = request.form['name']
phone_no = request.form['phone_no']
email_id = request.form['email_id']
print(name,email_id,phone_no)
make_call()
return render_template("returned_output.html")
@app.route("/webhooks/events", methods=["POST"])
def event():
return "OK"
@app.route("/webhooks/answer")
def answer_call():
global name, main_ncco, call_flow, flag_url, counter, id_no
flag_url = request.url_root + "webhooks/answer"
counter += 1
print('\n\n************************call answering response***********************')
for param_key, param_value in request.args.items():
print("{}: {}".format(param_key, param_value))
print('******************************************************************\n\n')
ncco = main_ncco[call_flow] + [ { "action": "talk", "voiceName":"Amy", "text": f"Hello {name}, Your calling identity is {id_no} and this identity number will be sent to you as a S M S and a mail after hanging up this call. Now please press 0 to continue." },
{ "action": "input", "maxDigits": 1, "eventUrl": [request.url_root + "mainMenu"] } ]
print(ncco)
return jsonify(ncco)
@app.route("/mainMenu", methods = ['POST'])
def mainMenu():
global response, client, UUID, call_flow, flag_url, counter, main_ncco
data = request.get_json()
pprint(data)
if data['timed_out']:
call_flow = 1
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[flag_url]})
elif counter > 3:
call_flow = 2
ncco = main_ncco[call_flow] + [ { "action":"hangup" } ]
elif data['dtmf'] == '0':
call_flow = 0
counter = -1
flag_url = request.url_root + "mainMenu"
ncco = main_ncco[call_flow] + [ { "action":"talk", "voiceName": "Amy", "text":"Welcome to Main Menu. Press 1 for project information. press 2 for asking your and developer information. press 3 for getting help link. press 4 for again listening the main menu.." },
{ "action":"input", "maxDigits":1, "eventUrl": [request.url_root + "index"] }]
else:
call_flow = 3
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[flag_url]})
return jsonify(ncco)
@app.route("/index", methods = ["POST"])
def index():
global response, client, UUID, call_flow, flag_url, counter, main_ncco, name, email_id, phone_no
data = request.get_json()
pprint(data)
ncco = [{"action":"talk", "text":"This is index ncco. This got returned and I don't know why"}]
if data['timed_out']:
call_flow = 1
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[flag_url]})
elif counter > 3:
call_flow = 2
ncco = main_ncco[call_flow] + [ { "action":"hangup" } ]
elif data['dtmf'] in ['1', '2', '3', '4']:
call_flow = 0
counter = -1
flag_url = request.url_root + "index"
if data['dtmf'] == '1':
call_flow = 0
counter = -1
flag_url = request.url_root + "index"
ncco = [ { "action":"talk", "voiceName": "Amy", "text":"hey ! This project has been named as Home Labs created for final year project for getting the degree of bachelor of technology. this project has been started on november 2019 and is still in process under the team leader of Tushit Agarwal. Thankyou. Press 1 for main menu." },
{ "action":"input", "maxDigits":1, "eventUrl": [request.url_root + "finalMenu"] }]
elif data['dtmf'] == '2':
ncco =[ { "action":"talk", "voiceName": "Amy", "text":f"Greetings from my side sir. Your information with us are quite visible with this option. Your name is {name} and your email id is {email_id} and your phone number to which I have called now is {phone_no}. The developer is TUSHIT AGARWAL creator of voice calling module with the help of nexmo. Thankyou. Press 1 for main menu"},
{ "action":"input", "maxDigits":1, "eventUrl": [request.url_root + "finalMenu"] }]
elif data['dtmf'] == '3':
ncco = [ { "action":"talk", "voiceName": "Amy", "text":"Sorry for having you a trouble. I will send you a S M S along with a mail listing all solutions available for your query. Thank you for the co-operation. Press 1. for main menu."},
{ "action":"input", "maxDigits":1, "eventUrl": [request.url_root + "finalMenu"] }]
elif data['dtmf'] == '4':
flag_url = request.url_root + "mainMenu"
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[request.url_root + "mainMenu"]})
else:
call_flow = 3
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[flag_url]})
return jsonify(ncco)
@app.route("/finalMenu", methods = ["POST"])
def finalMenu():
global response, client, UUID, call_flow, flag_url, counter, main_ncco
data = request.get_json()
pprint(data)
ncco = [{"action":"talk", "text":"This is final menu ncco. This got returned and I don't know why"}]
if data['timed_out']:
call_flow = 1
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[flag_url]})
elif counter > 3:
call_flow = 2
ncco = main_ncco[call_flow] + [ { "action":"hangup" } ]
elif data['dtmf'] == '1':
call_flow = 0
counter = -1
flag_url = request.url_root + "mainMenu"
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[request.url_root + "mainMenu"]})
else:
call_flow = 3
response = client.update_call(UUID, action = "transfer", destination = {"type":"ncco", "url":[request.url_root + "finalMenu"]})
return jsonify(ncco)
def make_call():
global ngrok_url_link, response, UUID, phone_no, client
client = nexmo.Client(application_id=app_id, private_key=private)
response = client.create_call({
'to': [{'type': 'phone', 'number': int(phone_no)}],
'from': {'type': 'phone', 'number': 91*********},
'answer_url': [ngrok_url_link + "/webhooks/answer"]
})
UUID = response['uuid']
print('\n\n************************call making response***********************')
pprint(response)
print('******************************************************************\n\n')
if __name__ == '__main__':
app.run(host = HOST, port = PORT, debug = True) # debug on for testing python
# code by url
I run flask app by creating virtualenv on ngrok. When call is made I pressed 0 where function mainMenu is called and then press 4 where goes to function index but it gives me a 404 error then and thus disconnects the call immediately.
I want to press 4 repeatedly 'n' number of times. The 404 error probably giving because mainMenu is of method POST and after pressing 4 flask is asking the GET method. I did tried :
@app.route("/mainMenu", methods=['GET','POST'])
def mainMenu():
-----same code as above follows-------
but it didn't work.
Can anyone tell me how to fix it up? Thanks!!
来源:https://stackoverflow.com/questions/58895927/nexmo-how-to-transfer-call-route-to-the-same-function-again-and-again-to-form-a