Skip to content

Instantly share code, notes, and snippets.

@egradman
Created December 21, 2011 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egradman/1505388 to your computer and use it in GitHub Desktop.
Save egradman/1505388 to your computer and use it in GitHub Desktop.
tropo python webapi coroutines
import tropo
coroutines = {}
def prompt_text(self, prompt, number):
"""
initiate a conversation with <phone>, asking a prompt
(merely pings the outbound session endpoint)
"""
params = urllib.urlencode({
'action': 'create',
'token': tropo_token,
'number': number,
'prompt': prompt,
})
f = urllib2.urlopen(tropo_url+"?"+params)
data = f.read()
f.close()
def make_prompt_text_coroutine(session_id, parameters):
"""factory for coroutines that handle the prompt_text conversation"""
def coroutine():
"""
this coroutine embodies all the business logic of our interaction
with the tropo server and our interlocutor.
We'll ask the interlocutor for a recording, and make sure they're
happy with their response.
"""
# when execution reaches this next line, the coroutine is suspended
body = yield
# when it resumes, body contains the first POST from the Tropo server
# initiate a call
t = tropo.Tropo()
t.call(body['session']['parameters']['number'])
while True:
# this coroutine has a simple loop from which it cannot exit
# until the interlocutor is happy with their recording
t.say(parameters['prompt'])
t.record(
say='speak your mind, followed by the pound key',
choices="#",
url="<your recording POST URL>"
format="audio/wav",
maxTime=10,
beep=True,
)
t.on(event="continue", next="<your voice endpoint>")
json = t.RenderJson()
# we yield some JSON, which our handler will return to Tropo
# execution is suspended
body = yield json
# when we reach this line, Tropo has POSTed
# as a result of the on(event="continue") above
# say text
t = tropo.Tropo()
t.say("you said")
t.say("<your recording GET URL>")
choices = tropo.Choices({"value": "yes(1, one), no(2, two)"})
t.ask(
say="if you're happy with your response, press or say one. To re-record, press or say two",
choices="yes(yes,y,1), no(no,n,2)",
name="happy",
bargein=False,
timeout=5
)
t.on(event="continue", next="<your voice endpoint>")
json = t.RenderJson()
body = yield json
if body['result']['actions']['value'] == 'yes':
break
t = tropo.Tropo()
t.say("thank you, your input was recorded")
json = t.RenderJson()
yield json
# we should never reach this point if Tropo is behaving correctly
assert False
# create and register the coroutine
gen = coroutine() # create the coroutine
gen.next() # start it running (like a generator!)
# register the coroutine in the AudioUpload class
coroutines[session_id] = gen
class TropoHandler(RequestHandler):
"""Tropo Voice endpoint"""
# this is extracted from a server written using Tornado
def post(self):
t = tropo.Tropo()
body = json.loads(self.request.body)
try:
# if the body has a session:id then it a session initiation
# resulting from POSTing to the outbound session endpoint
session_id = body['session']['id']
except KeyError:
session_id = None
if session_id:
# if this is a session initiation, create and register the coroutine
session = body['session']
make_prompt_text_coroutine(session_id, session['parameters'])
else:
session_id = body['result']['sessionId']
# fetch the coroutine for this session id
gen = coroutines[session_id]
# resume it with the current request body, capturing the tropo result
result = gen.send(body)
# send the result
self.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment