This Ruby on Rails sample application is modeled after a typical call center experience with an IVR 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 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 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
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
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
renderxml:response.to_s
87
end
88
89
deftwiml_dial(phone_number)
90
response=Twilio::TwiML::VoiceResponse.new do|r|
91
r.dial(number:phone_number)
92
end
93
94
renderxml: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
The gather'saction 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.
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
renderxml:response.to_s
87
end
88
89
deftwiml_dial(phone_number)
90
response=Twilio::TwiML::VoiceResponse.new do|r|
91
r.dial(number:phone_number)
92
end
93
94
renderxml: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
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.
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
renderxml:response.to_s
87
end
88
89
deftwiml_dial(phone_number)
90
response=Twilio::TwiML::VoiceResponse.new do|r|
91
r.dial(number:phone_number)
92
end
93
94
renderxml:response.to_s
95
end
96
end
That was only the main menu and the first option. If the caller chooses '2', we will take them to the Planet Directory in order to collect more input.
The Planet Directory: Collect more input from the caller
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