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

Gather User Input via Keypad (DTMF Tones) in Ruby


In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF(link takes you to an external page) tones) in your Ruby application. By applying this technique, you can create interactive voice response (IVR(link takes you to an external page)) systems and other phone based interfaces for your users. The code snippets in this guide are written using the Ruby language version 2.0.0 or higher, and make use of the following libraries:

Let's get started!


Set up your Sinatra application to receive incoming phone calls

set-up-your-sinatra-application-to-receive-incoming-phone-calls page anchor

This guide assumes you have already set up your web application to receive incoming phone calls. If you still need to complete this step, check out this guide. It should walk you through the process of buying a Twilio number and configuring your app to receive incoming calls.


Collect user input with the <Gather> TwiML verb

collect-user-input-with-the-gather-twiml-verb page anchor

The <Gather> TwiML verb allows us to collect input from the user during a phone call. Gathering user input through the keypad is a core mechanism of Interactive Voice Response (IVR) systems where users can press "1" to connect to one menu of options and press "2" to reach another. These prompts can be accompanied by voice prompts to the caller, using the TwiML <Say> and <Play> verbs. In this example, we will prompt the user to enter a number to connect to a certain department within our little IVR system.

Use <Gather> to collect user input via the keypad (DTMF tones)

use-gather-to-collect-user-input-via-the-keypad-dtmf-tones page anchor

This example returns TwiML containing a <Say> tag nested within a <Gather> tag. The user will be prompted to choose sales or support.

Ruby

_12
# Get twilio-ruby from twilio.com/docs/ruby/install
_12
require 'sinatra'
_12
require 'twilio-ruby'
_12
_12
post '/voice' do
_12
Twilio::TwiML::VoiceResponse.new do |r|
_12
r.gather numDigits: 1 do |g|
_12
g.say(message: 'For sales, press 1. For support, press 2.')
_12
end
_12
r.redirect('/voice')
_12
end.to_s
_12
end

If the user doesn't enter any input after a configurable timeout, Twilio will continue processing the TwiML in the document to determine what should happen next in the call. When the end of the document is reached, Twilio will hang up the call. In the above example, we use the <Redirect> verb to have Twilio request the same URL again, repeating the prompt for the user

If a user were to enter input with the example above, the user would hear the same prompt over and over again regardless of what button you pressed. By default, if the user does enter input in the <Gather>, Twilio will send another HTTP request to the current webhook URL with a POST parameter containing the Digits entered by the user. In the sample above, we weren't handling this input at all. Let's update that logic to also process user input if it is present.

Branch your call logic based on the digits sent by the user

branch-your-call-logic-based-on-the-digits-sent-by-the-user page anchor

By default, <Gather> will 'loop' on the current TwiML URL, requesting the same URL every time input is entered.

Ruby

_34
# Get twilio-ruby from twilio.com/docs/ruby/install
_34
require 'sinatra'
_34
require 'twilio-ruby'
_34
_34
post '/voice' do
_34
if params['Digits']
_34
case params['Digits']
_34
when '1'
_34
Twilio::TwiML::VoiceResponse.new do |r|
_34
r.say(message: 'You selected sales. Good for you!')
_34
end.to_s
_34
when '2'
_34
Twilio::TwiML::VoiceResponse.new do |r|
_34
r.say(message: 'You need support. We will help!')
_34
end.to_s
_34
else
_34
Twilio::TwiML::VoiceResponse.new do |r|
_34
r.say(message: 'Sorry, I don\'t understand that choice.')
_34
r.pause
_34
r.gather(numDigits: 1) do |g|
_34
g.say(message: 'For sales, press 1. For support, press 2.')
_34
end
_34
r.redirect('/voice')
_34
end.to_s
_34
end
_34
else
_34
Twilio::TwiML::VoiceResponse.new do |r|
_34
r.gather(numDigits: 1) do |g|
_34
g.say(message: 'For sales, press 1. For support, press 2.')
_34
end
_34
r.redirect('/voice')
_34
end.to_s
_34
end
_34
end


Specify an action to take after user input is collected

specify-an-action-to-take-after-user-input-is-collected page anchor

You may want to have an entirely different endpoint in your application handle the processing of user input. This is possible using the "action" attribute of the <Gather> verb. Let's update our example to add a second endpoint that will be responsible for handling user input.

Add another route to handle the input from the user

add-another-route-to-handle-the-input-from-the-user page anchor

This technique creates a separate route to handle user input.

Ruby

_39
# Get twilio-ruby from twilio.com/docs/ruby/install
_39
require 'sinatra'
_39
require 'twilio-ruby'
_39
_39
post '/voice' do
_39
Twilio::TwiML::VoiceResponse.new do |r|
_39
r.gather(numDigits: 1, action: '/gather') do |g|
_39
g.say(message: 'For sales, press 1. For support, press 2.')
_39
end
_39
_39
# If the user doesn't enter input, loop
_39
r.redirect('/voice')
_39
end.to_s
_39
end
_39
_39
post '/gather' do
_39
if params['Digits']
_39
case params['Digits']
_39
when '1'
_39
Twilio::TwiML::VoiceResponse.new do |r|
_39
r.say(message: 'You selected sales. Good for you!')
_39
end.to_s
_39
when '2'
_39
Twilio::TwiML::VoiceResponse.new do |r|
_39
r.say(message: 'You need support. We will help!')
_39
end.to_s
_39
else
_39
Twilio::TwiML::VoiceResponse.new do |r|
_39
r.say(message: 'Sorry, I don\'t understand that choice.')
_39
r.pause
_39
r.redirect('/voice')
_39
end.to_s
_39
end
_39
else
_39
Twilio::TwiML::VoiceResponse.new do |r|
_39
r.redirect('/voice')
_39
end.to_s
_39
end
_39
end

The action attribute takes a relative URL which would point to another route your server is capable of handling. Now, instead of conditional logic in a single route, we use actions and redirects to handle our call logic with separate code paths.


If you're building call center type applications in Ruby, you might enjoy stepping through full sample applications written in Ruby.


Rate this page: