我正在尝试使用nexmo API为我的项目制作一个简单的语音IVR。请参考图像以获取确切的间隙。 basic idea by flow diagram of what I am trying to do as ivr.
现在出现的问题是,如果数字输入错误,我将无法循环返回到mainMenu。直到我理解为止的问题是函数的GET和POST方法。
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
我通过在ngrok上创建virtualenv来运行flask应用程序。进行呼叫时,我在调用mainMenu函数的位置按了0,然后在函数索引处按了4,但是这给了我404错误,因此立即断开了呼叫。
我要重复按4次'n'次。可能会出现404错误,因为mainMenu属于POST方法,并且在按下4瓶后正在询问GET方法。我确实尝试过:
@app.route("/mainMenu", methods=['GET','POST'])
def mainMenu():
-----same code as above follows-------
但是没有用。
谁能告诉我如何解决它? 谢谢!