问题
I have been trying to create a flow for sending back modified output to user on basis of the key press. For this, I am using the twilio's Api.
Before I go ahead with my problem I will share the two pieces of code from Route.py and CallResponse.py
app = Flask(__name__)
logger = lh.log_initializer()
@app.route("/Aresponse", methods=['GET', 'POST'])
def ReceivedA():
"""Respond to incoming requests."""
logger.debug("Starting ReceivedA flow| name:" + __name__)
resp = tcr.RoutedReceivedA()
return str(resp)
if __name__ == "__main__":
# logger.debug("In routing main")
app.run(debug=True, host='0.0.0.0', port=80, threaded=True)
Below is the code of CallResponse.py
app = Flask(__name__)
logger = lh.log_initializer()
def RoutedReceivedA():
"""Respond to incoming requests."""
resp = VoiceResponse()
resp.say("Hello Monkey")
# Say a command, and listen for the caller to press a key. When they press
# a key, redirect them to /user-input-key.
logger.debug('Starting call receiving process|name:' + __name__)
A_num = Gather(numDigits=2, action="/user-A-input", method="POST")
A_num_entered = str(A_num) #there is surely something not right with this line, but I am hardly bothered about this right now
A_num.say("Please press a key to register your response")
logger.debug("After taking A number from user| A num:" + A_num_entered)
resp.append(A_num)
return str(resp)
@app.route("/user-A-input", methods=['GET', 'POST'])
def UserAInput():
"""Handle key press from a user."""
# Get the digit pressed by the user
#The below logs are not printed, so somehow this method isn't called
logger.debug('before considering user input in route')
route_num = request.values.get('Digits', None)
logger.debug('In side UserRouteInput| route_num:' + route_num)
return route_num
So when I start the app, I hit the url http://xyz.abc.io/AResponse, the request is first received in the Route.py to the ReceivedA() method, which internally calls the RoutedReceivedA(), which prints the log
Starting call receiving process|name:...
after which I am expecting it to redirect to the method UserAInput, as I am passing the method URL through the action property in Gather method of twilio library, but it isn't calling that method. To confirm that I have also written a debug log, which never prints itself. The later pain would be to get the user inputted value and pass it to another method (as it would make an api call on basis of that input) which I am not bothered about right now.
I have tried understanding the flask working to verify if there is something I am doing wrong with that. I am missing something here but unknowingly I am not able to understand what. Although I am trying to find a solution myself, I can surely use some help.
P.S: I tried replicating the issue using the sample gather code on twilio website, by creating a maincall.py which internally calls hello_monkey() method of the twilios sample code, which then calls handle-key. Even in this case it wasn't routing to the handle_key() method. However it was working when I was directly calling the hello_monkey() through the API call. I believe I am surely misunderstanding some concept of flask routing. Really frustrated.
回答1:
I found the solution to the problem. I need to
@app.route("/user-A-input", methods=['GET', 'POST'])
method in Route.py instead of CallResponse.py It's probably because it is using the instance of flask object which was created in the Route.py. I still need to look into detail, like how can I make it work by keeping the UserAInput method in CallResponse.py
来源:https://stackoverflow.com/questions/47122561/unable-to-redirect-to-method-url-through-action-prop-in-gather-method-twilio