Ghost
Ghost

Reputation: 569

Twilo python Flask passing parameter

I have an app with python which works but I like to pass the result of speech recognition to the caller I dont know how, I have tried everything. I even tried to set lastRes to global variable by declaring it on top it is not working

lastRes = ""
app = Flask(__name__)

@app.route("/voice", methods=['GET', 'POST'])
def voice():
  resp = VoiceResponse()
  resp.say("What is your name?")
  print ("1---------------")
  resp.gather(input='speech', timeout="3", action='/gather', method='POST')
  #resp.append(gather)
  print ("2---------------")
  return str(resp)

@app.route("/mainFlow", methods=['GET', 'POST'])
def mainFlow():
  resp = VoiceResponse()
  resp.say("We are done?")
  print("5--------------->" + str(lastRes))
  return str(resp)

@app.route("/gather", methods=['GET', 'POST'])
def gather():
  resp = VoiceResponse()
  print("3---------------")
  lastRes = request.values.get("SpeechResult", "")
  print("4--------------->" + str(lastRes))
  resp.redirect('/mainFlow')
  return str(resp)

if __name__ == "__main__":
  lastRes = ""
  app.run(debug=True)

How can I pass lastRes to the routine /mainFlow?

Upvotes: 1

Views: 76

Answers (1)

philnash
philnash

Reputation: 73047

Twilio developer evangelist here.

First up, I would ask why you are trying to redirect before you use the result. You already have the speech result in your /gather endpoint, why not respond then? Like this:

@app.route("/gather", methods=['GET', 'POST'])
def gather():
  resp = VoiceResponse()
  lastRes = request.values.get("SpeechResult", "")
  resp.say('Thank you ' + lastRes + '.')
  resp.redirect('/mainFlow')
  return str(resp)

This response will say "Thank you" and the speech result before then redirecting to the main flow.

Alternatively, you are working with URLs here. You could set the response as a URL parameter, like this:

import urllib

@app.route("/gather", methods=['GET', 'POST'])
def gather():
  resp = VoiceResponse()
  SpeechResult = request.values.get("SpeechResult", "")
  params = urllib.urlencode({"SpeechResult": SpeechResult})
  resp.redirect('/mainFlow?' + params)
  return str(resp)

Then you can use the SpeechResult in mainFlow like:

@app.route("/mainFlow", methods=['GET', 'POST'])
def mainFlow():
  SpeechResult = request.args.get('SpeechResult')
  resp = VoiceResponse()
  resp.say("We are done? " + SpeechResult)
  return str(resp)

Finally, you could set the SpeechResult in the session as that is maintained over the course of a call too.

from flask import Flask, session, request

@app.route("/gather", methods=['GET', 'POST'])
def gather():
  resp = VoiceResponse()
  session['SpeechResult'] = request.values.get("SpeechResult", "")
  resp.redirect('/mainFlow')
  return str(resp)

@app.route("/mainFlow", methods=['GET', 'POST'])
def mainFlow():
  SpeechResult = session['SpeechResult']
  resp = VoiceResponse()
  resp.say("We are done? " + SpeechResult)
  return str(resp)

Let me know if that helps at all.

Upvotes: 3

Related Questions