In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF tones) in your Ruby application. By applying this technique, you can create interactive voice response (IVR) 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!
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.
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.
1# Get twilio-ruby from twilio.com/docs/ruby/install2require 'sinatra'3require 'twilio-ruby'45post '/voice' do6Twilio::TwiML::VoiceResponse.new do |r|7r.gather numDigits: 1 do |g|8g.say(message: 'For sales, press 1. For support, press 2.')9end10r.redirect('/voice')11end.to_s12end
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.
1# Get twilio-ruby from twilio.com/docs/ruby/install2require 'sinatra'3require 'twilio-ruby'45post '/voice' do6if params['Digits']7case params['Digits']8when '1'9Twilio::TwiML::VoiceResponse.new do |r|10r.say(message: 'You selected sales. Good for you!')11end.to_s12when '2'13Twilio::TwiML::VoiceResponse.new do |r|14r.say(message: 'You need support. We will help!')15end.to_s16else17Twilio::TwiML::VoiceResponse.new do |r|18r.say(message: 'Sorry, I don\'t understand that choice.')19r.pause20r.gather(numDigits: 1) do |g|21g.say(message: 'For sales, press 1. For support, press 2.')22end23r.redirect('/voice')24end.to_s25end26else27Twilio::TwiML::VoiceResponse.new do |r|28r.gather(numDigits: 1) do |g|29g.say(message: 'For sales, press 1. For support, press 2.')30end31r.redirect('/voice')32end.to_s33end34end
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.
1# Get twilio-ruby from twilio.com/docs/ruby/install2require 'sinatra'3require 'twilio-ruby'45post '/voice' do6Twilio::TwiML::VoiceResponse.new do |r|7r.gather(numDigits: 1, action: '/gather') do |g|8g.say(message: 'For sales, press 1. For support, press 2.')9end1011# If the user doesn't enter input, loop12r.redirect('/voice')13end.to_s14end1516post '/gather' do17if params['Digits']18case params['Digits']19when '1'20Twilio::TwiML::VoiceResponse.new do |r|21r.say(message: 'You selected sales. Good for you!')22end.to_s23when '2'24Twilio::TwiML::VoiceResponse.new do |r|25r.say(message: 'You need support. We will help!')26end.to_s27else28Twilio::TwiML::VoiceResponse.new do |r|29r.say(message: 'Sorry, I don\'t understand that choice.')30r.pause31r.redirect('/voice')32end.to_s33end34else35Twilio::TwiML::VoiceResponse.new do |r|36r.redirect('/voice')37end.to_s38end39end
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.