This Python Flask sample application is modeled after a typical call center experience, but with more Reese's Pieces.
Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet directly. In this tutorial, we'll show you the critical bits of code that make this work.
To initiate the phone tree, we need to configure one of our Twilio numbers to send our web application an HTTP request when we get an incoming call.
Click on one of your numbers and configure the Voice URL to point to our app. In our code, the route will be /ivr/welcome.
If you don't already have a server configured to use as your webhook, ngrok is an excellent tool for testing webhooks locally.
With our Twilio number configured, we are prepared to respond to the Twilio request.
Respond to the Twilio request with TwiML
Our Twilio number is now configured to send HTTP requests to this controller method on any incoming voice calls. Our app responds with TwiML to tell Twilio what to do in response to the message.
In this case, we tell Twilio to Gather the input from the caller and then Play a welcome message.
You may have noted we're using an unknown method, ``` TwiML` ``. This method comes from a custom view helper that takes a TwiML Response and transforms it into a valid HTTP Response. Check out the implementation:
1
importflask
2
3
deftwiml(resp):
4
resp=flask.Response(str(resp))
5
resp.headers['Content-Type']='text/xml'
6
returnresp
Respond with TwiML to gather an option from the caller
After playing the audio and retrieving the caller's input, Twilio will send this input to our application.
Where to send the caller's input
The gather'saction parameter takes an absolute or relative URL as a value - in our case, this is the menu endpoint.
When the caller finishes entering digits, Twilio will make a GET or POST request to this URL and include a Digits parameter with the number our caller chose.
After making its request, Twilio will continue the current call using the TwiML received in your response. Note that any TwiML verbs occurring after a <Gather> are unreachable unless the caller does not enter any digits.
g.say("To call the planet Broh doe As O G, press 2. To call the "+
84
"planet DuhGo bah, press 3. To call an oober asteroid "+
85
"to your location, press 4. To go back to the main menu "+
86
" press the star key.",
87
voice="Polly.Amy",language="en-GB",loop=3)
88
89
returnresponse
90
91
92
def_redirect_welcome():
93
response=VoiceResponse()
94
response.say("Returning to the main menu",voice="Polly.Amy",language="en-GB")
95
response.redirect(url_for('welcome'))
96
97
returntwiml(response)
Now that we have told Twilio where to send the caller's input, we can look at how to process that input.
The Main Menu: Processing the caller's selection
This route handles processing the caller's input.
If our caller chooses '1' for directions, we use the _give_instructions method to respond with TwiML that will Say directions to our caller's extraction point.
If the caller chooses '2' to call their home planet, then we need to gather more input from them. We wrote another method to handle this, _list_planets, which we'll cover in the next step.
If the caller enters anything else, we respond with a TwiML Redirect to the main menu.
g.say("To call the planet Broh doe As O G, press 2. To call the "+
84
"planet DuhGo bah, press 3. To call an oober asteroid "+
85
"to your location, press 4. To go back to the main menu "+
86
" press the star key.",
87
voice="Polly.Amy",language="en-GB",loop=3)
88
89
returnresponse
90
91
92
def_redirect_welcome():
93
response=VoiceResponse()
94
response.say("Returning to the main menu",voice="Polly.Amy",language="en-GB")
95
response.redirect(url_for('welcome'))
96
97
returntwiml(response)
If the caller chooses '2', we will take them to the Planet Directory to collect more input.
The Planet Directory: Collecting more input from the caller
If our callers choose to call their home planet, we will read them the planet directory. Our planet directory is similar to a typical "company directory" feature of most IVRs.
In our TwiML response, we again use a Gather verb to get our caller's input. This time, the action verb points to the planets route, which will map our response to what the caller chooses.
Let's look at that route next. The TwiML response we return for that route uses a Dial verb with the appropriate phone number to connect our caller to their home planet.
Collect more input from the caller via the Planet Directory
g.say("To call the planet Broh doe As O G, press 2. To call the "+
84
"planet DuhGo bah, press 3. To call an oober asteroid "+
85
"to your location, press 4. To go back to the main menu "+
86
" press the star key.",
87
voice="Polly.Amy",language="en-GB",loop=3)
88
89
returnresponse
90
91
92
def_redirect_welcome():
93
response=VoiceResponse()
94
response.say("Returning to the main menu",voice="Polly.Amy",language="en-GB")
95
response.redirect(url_for('welcome'))
96
97
returntwiml(response)
Again, we show some options to the caller and instruct Twilio to collect the caller's choice.
The Planet Directory: Connect the caller to another number
In this route, we grab the caller's digit selection from the HTTP request and store it in a variable called selected_option. We then use a Dial verb with the appropriate phone number to connect our caller to their home planet.
The current numbers are hardcoded, but you could update this code to read phone numbers from a database or a file.