IVR Phone Tree

IVRs (interactive voice response) are automated phone systems that can facilitate communication between callers and businesses. If you've ever dialed your credit card company to check on a balance after responding to a series of automated prompts, you've used an IVR. Learn how to build an IVR in minutes using Twilio's powerful TwiMl API.

Start Tutorial

About this application

Extra Terrestrial Phone Home

This Python's 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 key bits of code to make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub.

Let's get started! Click the right arrow above to move to the next step of the tutorial.

See Also:

Answering a Phone Call

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.

Configuring a Twilio Number

See Also:

Responding to Twilio's request

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 we Play a welcome message.

You may have noted we're using an unknown method TwiML. This is a custom view helper that takes a TwiML Response and transforms it into a valid HTTP Response. It's super easy, check the implementation:

import flask

def twiml(resp):
    resp = flask.Response(str(resp))
    resp.headers['Content-Type'] = 'text/xml'
    return resp

See Also:

Where to send the caller's input?

The gather's action parameter takes an absolute or relative URL as a value - in our case, the menu endpoint.

When the caller has finished entering digits Twilio will make a GET or POST request to this URL including a Digits parameter with the number our caller chose.

After making this request, Twilio will continue the current call using the TwiML received in your response. Any TwiML verbs occurring after a <Gather> are unreachable, unless the caller enters no digits.

See Also:

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 method defined below, _give_instructions, to respond with TwiML that will Say directions to our caller's extraction point.

If the caller chooses '2' to call her home planet, then we need to gather more input from her. 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.

See Also:

The Planet's Directory: Collecting more input from the caller

If our caller chooses to call her home planet we will tell her the planet's directory. This is similar to a typical "company directory" feature of most IVRs.

In our TwiML response we again use a Gather verb to receive our caller's input. The action verb points this time to the planets' route, which will switch our response based on what the caller chooses.

Let's look at that route next. The TwiML response we return for that route just uses a Dial verb with the appropriate phone number to connect our caller to her home planet.

The Planet's Directory: Connecting the caller to another number

In this route, we grab the caller's selection of the 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 her home planet.

The current numbers are hardcoded, but they could also be read from a database or from a file.

Where to next?

That's it! We've just implemented an IVR phone tree that will delight and serve your customers.

If you're a Python/Flask developer working with Twilio, you might want to check out these other tutorials:

Appointment Reminders

Use Twilio to automate the process of reaching out to your customers in advance of an upcoming appointment.

Two-Factor Authentication with Authy

Use Twilio and Twilio-powered Authy OneTouch to implement two-factor authentication (2FA) in your web app

Did this help?

Thanks for checking this tutorial out! If you have any feedback to share with us, we'd love to hear it. Contact the Twilio Developer Education Team to let us know what you think.