Skip to contentSkip to navigationSkip to topbar
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

1
require 'twilio-ruby'
2
3
4
class TwilioController < ApplicationController
5
6
def index
7
render plain: "Dial Me."
8
end
9
10
# POST ivr/welcome
11
def ivr_welcome
12
response = Twilio::TwiML::VoiceResponse.new
13
response.gather(num_digits: '1', action: menu_path) do |gather|
14
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
15
directions. Press 2 for a list of planets to call.", loop: 3)
16
end
17
render xml: response.to_s
18
end
19
20
# GET ivr/selection
21
def menu_selection
22
user_selection = params[:Digits]
23
24
case user_selection
25
when "1"
26
@output = "To get to your extraction point, get on your bike and go down
27
the street. Then Left down an alley. Avoid the police cars. Turn left
28
into an unfinished housing development. Fly over the roadblock. Go
29
passed the moon. Soon after you will see your mother ship."
30
twiml_say(@output, true)
31
when "2"
32
list_planets
33
else
34
@output = "Returning to the main menu."
35
twiml_say(@output)
36
end
37
38
end
39
40
# POST/GET ivr/planets
41
# planets_path
42
def planet_selection
43
user_selection = params[:Digits]
44
45
case user_selection
46
when "2"
47
twiml_dial("+19295566487")
48
when "3"
49
twiml_dial("+17262043675")
50
when "4"
51
twiml_dial("+16513582243")
52
else
53
@output = "Returning to the main menu."
54
twiml_say(@output)
55
end
56
end
57
58
private
59
60
def list_planets
61
message = "To call the planet Broh doe As O G, press 2. To call the planet
62
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
63
go back to the main menu, press the star key."
64
65
response = Twilio::TwiML::VoiceResponse.new
66
response.gather(num_digits: '1', action: planets_path) do |gather|
67
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
68
end
69
70
render xml: response.to_s
71
end
72
73
def twiml_say(phrase, exit = false)
74
# Respond with some TwiML and say something.
75
# Should we hangup or go back to the main menu?
76
response = Twilio::TwiML::VoiceResponse.new
77
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
78
if exit
79
response.say(message: "Thank you for calling the ET Phone Home Service - the
80
adventurous alien's first choice in intergalactic travel.")
81
response.hangup
82
else
83
response.redirect(welcome_path)
84
end
85
86
render xml: response.to_s
87
end
88
89
def twiml_dial(phone_number)
90
response = Twilio::TwiML::VoiceResponse.new do |r|
91
r.dial(number: phone_number)
92
end
93
94
render xml: response.to_s
95
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

1
require 'twilio-ruby'
2
3
4
class TwilioController < ApplicationController
5
6
def index
7
render plain: "Dial Me."
8
end
9
10
# POST ivr/welcome
11
def ivr_welcome
12
response = Twilio::TwiML::VoiceResponse.new
13
response.gather(num_digits: '1', action: menu_path) do |gather|
14
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
15
directions. Press 2 for a list of planets to call.", loop: 3)
16
end
17
render xml: response.to_s
18
end
19
20
# GET ivr/selection
21
def menu_selection
22
user_selection = params[:Digits]
23
24
case user_selection
25
when "1"
26
@output = "To get to your extraction point, get on your bike and go down
27
the street. Then Left down an alley. Avoid the police cars. Turn left
28
into an unfinished housing development. Fly over the roadblock. Go
29
passed the moon. Soon after you will see your mother ship."
30
twiml_say(@output, true)
31
when "2"
32
list_planets
33
else
34
@output = "Returning to the main menu."
35
twiml_say(@output)
36
end
37
38
end
39
40
# POST/GET ivr/planets
41
# planets_path
42
def planet_selection
43
user_selection = params[:Digits]
44
45
case user_selection
46
when "2"
47
twiml_dial("+19295566487")
48
when "3"
49
twiml_dial("+17262043675")
50
when "4"
51
twiml_dial("+16513582243")
52
else
53
@output = "Returning to the main menu."
54
twiml_say(@output)
55
end
56
end
57
58
private
59
60
def list_planets
61
message = "To call the planet Broh doe As O G, press 2. To call the planet
62
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
63
go back to the main menu, press the star key."
64
65
response = Twilio::TwiML::VoiceResponse.new
66
response.gather(num_digits: '1', action: planets_path) do |gather|
67
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
68
end
69
70
render xml: response.to_s
71
end
72
73
def twiml_say(phrase, exit = false)
74
# Respond with some TwiML and say something.
75
# Should we hangup or go back to the main menu?
76
response = Twilio::TwiML::VoiceResponse.new
77
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
78
if exit
79
response.say(message: "Thank you for calling the ET Phone Home Service - the
80
adventurous alien's first choice in intergalactic travel.")
81
response.hangup
82
else
83
response.redirect(welcome_path)
84
end
85
86
render xml: response.to_s
87
end
88
89
def twiml_dial(phone_number)
90
response = Twilio::TwiML::VoiceResponse.new do |r|
91
r.dial(number: phone_number)
92
end
93
94
render xml: response.to_s
95
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

1
require 'twilio-ruby'
2
3
4
class TwilioController < ApplicationController
5
6
def index
7
render plain: "Dial Me."
8
end
9
10
# POST ivr/welcome
11
def ivr_welcome
12
response = Twilio::TwiML::VoiceResponse.new
13
response.gather(num_digits: '1', action: menu_path) do |gather|
14
gather.say(message: "Thanks for calling the E T Phone Home Service. Please press 1 for
15
directions. Press 2 for a list of planets to call.", loop: 3)
16
end
17
render xml: response.to_s
18
end
19
20
# GET ivr/selection
21
def menu_selection
22
user_selection = params[:Digits]
23
24
case user_selection
25
when "1"
26
@output = "To get to your extraction point, get on your bike and go down
27
the street. Then Left down an alley. Avoid the police cars. Turn left
28
into an unfinished housing development. Fly over the roadblock. Go
29
passed the moon. Soon after you will see your mother ship."
30
twiml_say(@output, true)
31
when "2"
32
list_planets
33
else
34
@output = "Returning to the main menu."
35
twiml_say(@output)
36
end
37
38
end
39
40
# POST/GET ivr/planets
41
# planets_path
42
def planet_selection
43
user_selection = params[:Digits]
44
45
case user_selection
46
when "2"
47
twiml_dial("+19295566487")
48
when "3"
49
twiml_dial("+17262043675")
50
when "4"
51
twiml_dial("+16513582243")
52
else
53
@output = "Returning to the main menu."
54
twiml_say(@output)
55
end
56
end
57
58
private
59
60
def list_planets
61
message = "To call the planet Broh doe As O G, press 2. To call the planet
62
DuhGo bah, press 3. To call an oober asteroid to your location, press 4. To
63
go back to the main menu, press the star key."
64
65
response = Twilio::TwiML::VoiceResponse.new
66
response.gather(num_digits: '1', action: planets_path) do |gather|
67
gather.say(message: message, voice: 'Polly.Amy', language: 'en-GB', loop: 3)
68
end
69
70
render xml: response.to_s
71
end
72
73
def twiml_say(phrase, exit = false)
74
# Respond with some TwiML and say something.
75
# Should we hangup or go back to the main menu?
76
response = Twilio::TwiML::VoiceResponse.new
77
response.say(message: phrase, voice: 'Polly.Amy', language: 'en-GB')
78
if exit
79
response.say(message: "Thank you for calling the ET Phone Home Service - the
80
adventurous alien's first choice in intergalactic travel.")
81
response.hangup
82
else
83
response.redirect(welcome_path)
84
end
85
86
render xml: response.to_s
87
end
88
89
def twiml_dial(phone_number)
90
response = Twilio::TwiML::VoiceResponse.new do |r|
91
r.dial(number: phone_number)
92
end
93
94
render xml: response.to_s
95
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.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.