Skip to content

Instantly share code, notes, and snippets.

@rdegges
Created April 26, 2010 15:35
Show Gist options
  • Save rdegges/379479 to your computer and use it in GitHub Desktop.
Save rdegges/379479 to your computer and use it in GitHub Desktop.
##
# This gist is an update of the tropo python scripting tutorial page. It contains some
# suggested changes that would make the tutorial more familiar to native python
# developers.
#
# The tropo python scripting tutorial can be found at:
# https://www.tropo.com/docs/scripting/t_tutorial-python.htm
#
# NOTE: All changes I've proposed here are very simple (just removing parenthesis
# from if statements, mostly). All examples work well. The python style guide
# pep-8 (see: http://www.python.org/dev/peps/pep-0008/) dictates that you
# should not use parenthesis unless they are necessary for the expression, or
# useful visually to group objects together. The other thing I propose is
# that there should be no trailing semi-colons (;) on the end of any
# lines (considered bad style).
#
# @author Randall Degegs
# @email [email protected]
##
##########
# --------------------------------------------
# Asking for input
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, press 1. For support, press 2.", {'choices':"1,2"})
if result.name == 'choice':
if result.value == "1": say("Sales is not available right now")
if result.value == "2": say("Support is currently on the other line.")
hangup()
##########
##########
# --------------------------------------------
# Repeating the question
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, press 1. For support, press 2.",
{'choices':"1,2", 'repeat':3})
if result.name == 'choice':
if result.value == "1": say("Sales is not available right now")
if result.value == "2": say("Support is currently on the other line.")
hangup()
##########
##########
# --------------------------------------------
# Changing the default timeout
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hello there. For sales, press 1. For support, press 2.",
{'choices':"1,2", 'repeat':3, 'timeout':10})
if result.name == 'choice':
if result.value == "1":
say("Sales is not available right now")
if result.value == "2":
say("Support is currently on the other line.")
hangup()
##########
##########
# --------------------------------------------
# Using speech input instead of touch-tone
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, say sales. For support, say support",
{'choices':"sales, support", 'repeat':3})
if result.name == 'choice':
if result.value == "sales":
say("Sales is not available right now")
if result.value == "support":
say("Support is currently on the other line.")
hangup()
##########
##########
# --------------------------------------------
# Using both speech and touch-tone input
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, say sales or press 1. For support, say support or press 2", {'choices':"sales(1,sales), support(2,support)", 'repeat':3})
if result.name == 'choice':
if result.value == "sales":
say("Sales is not available right now")
if result.value == "support":
say("Support is currently on the other line.")
hangup()
##########
##########
# --------------------------------------------
# connecting the call to another number
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, say sales or press 1. For support, say support or press 2", {'choices':"sales(1,sales), support(2,support)", 'repeat':3})
if result.name == 'choice':
if result.value == "sales":
say("Ok, let me transfer you to sales")
transfer("tel:+14129272358")
if result.value == "support":
say("Ok, let me transfer you to support")
transfer("tel:+14129272341")
hangup()
##########
##########
# --------------------------------------------
# handling the wrong choice
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, say sales or press 1. For support, say support or press 2", {'choices':"sales(1,sales), support(2,support)"})
if result.name == 'choice':
if result.value == "sales":
say("Ok, let me transfer you to sales")
transfer("tel:+14129272358")
if result.value == "support":
say("Ok, let me transfer you to support")
transfer("tel:+14129272341")
if result.name == 'badChoice':
say("I'm not sure what you wanted. Goodbye.")
hangup()
##########
##########
# --------------------------------------------
# handling the wrong choice the the right way - introducing event handlers
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
result = ask("Hi. For sales, say sales or press 1. For support, say support or press 2",
{'choices':"sales(1,sales), support(2,support)", 'repeat':3,
'onBadChoice': lambda : say("I'm sorry, I didn't understand what you said.")})
if result.name == 'choice':
if result.value == "sales":
say("Ok, let me transfer you to sales")
transfer("tel:+14129272358")
if result.value == "support":
say("Ok, let me transfer you to support")
transfer("tel:+14129272341")
##########
##########
# --------------------------------------------
# handling bad choices and no response (timeout) with event handlers
# See http://www.tropo.com for more info
# --------------------------------------------
# =============================
# THIS APP NEEDS TO BE VERIFIED
# Ported from JavaScript, but not verified to work
# =============================
answer()
result = ask("Hi. For sales, say sales or press 1. For support, say support or press 2",
{'choices':"sales(1,sales), support(2,support)", 'repeat':3,
'onBadChoice': lambda : say("I'm sorry, I didn't understand what you said."),
'onTimeout': lambda : say("I'm sorry. I didn't hear anything.")})
if result.name == 'choice':
if result.value == "sales":
say("Ok, let me transfer you to sales")
transfer("tel:+14129272358")
if result.value == "support":
say("Ok, let me transfer you to support")
transfer("tel:+14129272341")
##########
##########
# --------------------------------------------
# reject a call based on caller ID
# See http://www.tropo.com for more info
# --------------------------------------------
if currentCall.callerID == '4075551111':
answer()
say("Hello, world!")
hangup()
else:
reject()
##########
##########
# --------------------------------------------
# redirect a call based on caller ID
# See http://www.tropo.com for more info
# --------------------------------------------
if currentCall.callerID == '4075551111':
answer()
say("Hello, world!")
hangup()
else:
redirect("tel:+14075552222")
##########
##########
# --------------------------------------------
# changing behavior based on number called
# See http://www.tropo.com for more info
# --------------------------------------------
answer()
if currentCall.calledID == '4075551111':
say("Hello Andrew")
if currentCall.calledID == '4075552222':
say("Hello Brian")
hangup()
##########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment