Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

IVR: Phone Tree with Ruby and Rails


ET Phone Home.

This Ruby on Rails(link takes you to an external page) sample application is modeled after a typical call center experience with an IVR but with more Reese's Pieces(link takes you to an external page).

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely or call their home planet(link takes you to an external page) 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(link takes you to an external page).

Read how Livestream(link takes you to an external page) and other companies(link takes you to an external page) built IVR phone trees with Twilio. Find examples and sample code for many web languages on our IVR application page.


Answering a Phone Call

answering-a-phone-call page anchor

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(link takes you to an external page) and configure the Voice URL to point to our app. In our code the route will be /ivr/welcome.

IVR Webhook Configuration.

If you don't already have a server configured to use as your webhook, ngrok(link takes you to an external page) is a great 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

respond-to-the-twilio-request-with-twiml page anchor

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

Respond with TwiML to gather an option from the caller

respond-with-twiml-to-gather-an-option-from-the-caller page anchor

app/controllers/twilio_controller.rb


_96
require 'twilio-ruby'
_96
_96
_96
class TwilioController < ApplicationController
_96
_96
def index
_96
render plain: "Dial Me."
_96
end
_96
_96
# POST ivr/welcome
_96
def ivr_welcome
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: menu_path) do |gather|
_96
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
_96
directions. Press 2 for a list of planets to call.", loop: 3)
_96
end
_96
render xml: response.to_s
_96
end
_96
_96
# GET ivr/selection
_96
def menu_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "1"
_96
@output = "To get to your extraction point, get on your bike and go down
_96
the street. Then Left down an alley. Avoid the police cars. Turn left
_96
into an unfinished housing development. Fly over the roadblock. Go
_96
passed the moon. Soon after you will see your mother ship."
_96
twiml_say(@output, true)
_96
when "2"
_96
list_planets
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
_96
end
_96
_96
# POST/GET ivr/planets
_96
# planets_path
_96
def planet_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "2"
_96
twiml_dial("+19295566487")
_96
when "3"
_96
twiml_dial("+17262043675")
_96
when "4"
_96
twiml_dial("+16513582243")
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
end
_96
_96
private
_96
_96
def list_planets
_96
message = "To call the planet Broh doe As O G, press 2. To call the planet
_96
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
_96
go back to the main menu, press the star key."
_96
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: planets_path) do |gather|
_96
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_say(phrase, exit = false)
_96
# Respond with some TwiML and say something.
_96
# Should we hangup or go back to the main menu?
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
_96
if exit
_96
response.say(message: "Thank you for calling the ET Phone Home Service - the
_96
adventurous alien's first choice in intergalactic travel.")
_96
response.hangup
_96
else
_96
response.redirect(welcome_path)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_dial(phone_number)
_96
response = Twilio::TwiML::VoiceResponse.new do |r|
_96
r.dial(number: phone_number)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
end

After reading the text to the caller and retrieving their input, Twilio will send this input to our application.


Where to send the caller's input

where-to-send-the-callers-input page anchor

The gather's action parameter takes an absolute or relative URL as a value. In our case, this is the menu route.

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 doesn't enter any digits.

Send caller input to the intended route

send-caller-input-to-the-intended-route page anchor

app/controllers/twilio_controller.rb


_96
require 'twilio-ruby'
_96
_96
_96
class TwilioController < ApplicationController
_96
_96
def index
_96
render plain: "Dial Me."
_96
end
_96
_96
# POST ivr/welcome
_96
def ivr_welcome
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: menu_path) do |gather|
_96
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
_96
directions. Press 2 for a list of planets to call.", loop: 3)
_96
end
_96
render xml: response.to_s
_96
end
_96
_96
# GET ivr/selection
_96
def menu_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "1"
_96
@output = "To get to your extraction point, get on your bike and go down
_96
the street. Then Left down an alley. Avoid the police cars. Turn left
_96
into an unfinished housing development. Fly over the roadblock. Go
_96
passed the moon. Soon after you will see your mother ship."
_96
twiml_say(@output, true)
_96
when "2"
_96
list_planets
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
_96
end
_96
_96
# POST/GET ivr/planets
_96
# planets_path
_96
def planet_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "2"
_96
twiml_dial("+19295566487")
_96
when "3"
_96
twiml_dial("+17262043675")
_96
when "4"
_96
twiml_dial("+16513582243")
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
end
_96
_96
private
_96
_96
def list_planets
_96
message = "To call the planet Broh doe As O G, press 2. To call the planet
_96
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
_96
go back to the main menu, press the star key."
_96
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: planets_path) do |gather|
_96
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_say(phrase, exit = false)
_96
# Respond with some TwiML and say something.
_96
# Should we hangup or go back to the main menu?
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
_96
if exit
_96
response.say(message: "Thank you for calling the ET Phone Home Service - the
_96
adventurous alien's first choice in intergalactic travel.")
_96
response.hangup
_96
else
_96
response.redirect(welcome_path)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_dial(phone_number)
_96
response = Twilio::TwiML::VoiceResponse.new do |r|
_96
r.dial(number: phone_number)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
end

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: Process the caller's selection

the-main-menu-process-the-callers-selection page anchor

If our caller chooses '1' for directions, we use the twiml_say helper 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, we need to gather more input from them. We'll cover this in the next step.

If the caller enters anything else, we respond with a TwiML Redirect to the main menu.


The Planet Directory: Collect more input from the caller

the-planet-directory-collect-more-input-from-the-caller page anchor

If our callers choose to call their home planet, we will read them the planet directory. This is similar to a typical "company directory" feature of most IVRs.

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

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.The current numbers are hardcoded, but we can also set those numbers using environment variables.

Route users to a new phone number via the Planet Directory

route-users-to-a-new-phone-number-via-the-planet-directory page anchor

app/controllers/twilio_controller.rb


_96
require 'twilio-ruby'
_96
_96
_96
class TwilioController < ApplicationController
_96
_96
def index
_96
render plain: "Dial Me."
_96
end
_96
_96
# POST ivr/welcome
_96
def ivr_welcome
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: menu_path) do |gather|
_96
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
_96
directions. Press 2 for a list of planets to call.", loop: 3)
_96
end
_96
render xml: response.to_s
_96
end
_96
_96
# GET ivr/selection
_96
def menu_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "1"
_96
@output = "To get to your extraction point, get on your bike and go down
_96
the street. Then Left down an alley. Avoid the police cars. Turn left
_96
into an unfinished housing development. Fly over the roadblock. Go
_96
passed the moon. Soon after you will see your mother ship."
_96
twiml_say(@output, true)
_96
when "2"
_96
list_planets
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
_96
end
_96
_96
# POST/GET ivr/planets
_96
# planets_path
_96
def planet_selection
_96
user_selection = params[:Digits]
_96
_96
case user_selection
_96
when "2"
_96
twiml_dial("+19295566487")
_96
when "3"
_96
twiml_dial("+17262043675")
_96
when "4"
_96
twiml_dial("+16513582243")
_96
else
_96
@output = "Returning to the main menu."
_96
twiml_say(@output)
_96
end
_96
end
_96
_96
private
_96
_96
def list_planets
_96
message = "To call the planet Broh doe As O G, press 2. To call the planet
_96
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
_96
go back to the main menu, press the star key."
_96
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.gather(num_digits: '1', action: planets_path) do |gather|
_96
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_say(phrase, exit = false)
_96
# Respond with some TwiML and say something.
_96
# Should we hangup or go back to the main menu?
_96
response = Twilio::TwiML::VoiceResponse.new
_96
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
_96
if exit
_96
response.say(message: "Thank you for calling the ET Phone Home Service - the
_96
adventurous alien's first choice in intergalactic travel.")
_96
response.hangup
_96
else
_96
response.redirect(welcome_path)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
_96
def twiml_dial(phone_number)
_96
response = Twilio::TwiML::VoiceResponse.new do |r|
_96
r.dial(number: phone_number)
_96
end
_96
_96
render xml: response.to_s
_96
end
_96
end

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


If you're a Ruby developer working with Twilio, you might enjoy these other tutorials:

Automated Survey(link takes you to an external page)

Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.

Click-to-call

Click-to-call enables your company to convert web traffic into phone calls with the click of a button.

Did this help?

did-this-help page anchor

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Connect with us on Twitter(link takes you to an external page) and let us know what you build!


Rate this page: