Programmable Voice quickstart for Ruby
This quickstart shows you how to build a Ruby application that can make and receive phone calls. It uses the Twilio REST API to make the outbound call and TwiML to respond to the inbound call by reading a message using text-to-speech.
Log in to your Twilio account or sign up for a new one.
If you need a Twilio phone number, buy one in the Twilio Console:
- Go to Phone Numbers > Manage > Buy a Number.
- Under Capabilities, select Voice.
- Click Search. The page displays a list of available voice-capable phone numbers.
- Click Buy next to a phone number to add it to your account.
Check if you already have Ruby installed on your computer by opening a terminal and running the following command:
ruby --version
If your computer has Ruby, then you see something like this:
ruby 2.7.2
You'll need Ruby 2.6 or later to run this quickstart. If you don't have a matching version of Ruby on your computer, then follow the Ruby installation instructions for your operating system.
Create a new directory for this project and navigate to it using the command line. Run the following commands to create a Gemfile and add the dependencies that this quickstart relies on.
1bundle init2bundle add puma twilio-ruby sinatra
Danger
This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.
-
Create a new file named
make_call.rb
with the following code:1require 'twilio-ruby'23# To set up environment variables, see https://twil.io/secure4account_sid = ENV['TWILIO_ACCOUNT_SID']5auth_token = ENV['TWILIO_AUTH_TOKEN']67# Set up a client to talk to the Twilio REST API8@client = Twilio::REST::Client.new(account_sid, auth_token)910call = @client.calls.create(11to: "+15558675310",12from: "+15017122661",13url: "http://demo.twilio.com/docs/voice.xml")14puts call.toThis code starts a phone call between the two phone numbers that you pass as arguments. The
from
number is your Twilio number, and theto
number is the call recipient.The
url
argument points to a TwiML file that tells Twilio what to do when the call recipient answers their phone. This TwiML instructs Twilio to read a message using text-to-speech and then play an MP3. -
Log in to the Twilio Console.
-
Copy the Account SID and replace
ENV['TWILIO_ACCOUNT_SID']
with it in themake_call.rb
file. -
Copy the Auth Token and replace
ENV['TWILIO_AUTH_TOKEN']
with it. -
Copy your Twilio phone number and replace the
from
value inmake_call.rb
with it. -
Replace the
to
value with your own phone number (the number you want to call). -
Save the file.
-
Run the script using this command:
ruby make_call.rbYour phone should ring with a call from your Twilio number. Answer it, and you'll hear a short message.
Twilio trial account limitations
Twilio trial accounts limit outgoing phone calls to phone numbers you have verified with Twilio. To verify your phone numbers, use your Twilio Console's Verified Caller IDs. To learn about other trial account limitations, see how to work with your free Twilio trial account.
- Create a file in your project directory named
answer_call.rb
. - Add the following code to create a server that responds to incoming calls:
1require 'sinatra'2require 'twilio-ruby'34configure :development do5set :host_authorization, { permitted_hosts: [] }6end78def self.get_or_post(url,&block)9get(url,&block)10post(url,&block)11end1213get_or_post '/' do14content_type 'text/xml'1516Twilio::TwiML::VoiceResponse.new do | response |17response.say(message: "Ahoy world!")18end.to_s19end
- Save the file.
- Start your Sinatra server with the following command:
ruby answer_call.rb
-
If you don't already use ngrok, then download and install it.
-
Ensure your Sinatra server is still running, then open a new command line window and start ngrok with this command:
ngrok http 4567You should see output that resembles this:
1Session Status online2Version 3.23.23Web Interface http://127.0.0.1:40404Forwarding https://ff4660c91380.ngrok.app -> http://localhost:456756Connections ttl opn rt1 rt5 p50 p9070 0 0.00 0.00 0.00 0.00 -
Copy the URL ending in
ngrok.app
and paste it into your browser. You should see a TwiML document that contains the string 'Ahoy world!' -
In the Twilio Console, go to Phone Numbers > Manage > Active numbers.
-
Click your Twilio phone number.
-
On the Configure tab, under the Voice Configuration section, set A call comes in to Webhook.
-
Enter your ngrok public URL in the URL field (for example,
https://ff4660c91380.ngrok.app
). -
Click Save configuration.
-
Head back to your terminal. Make sure that ngrok is still running in one tab and your Sinatra server is running in another tab.
Make a phone call to your Twilio phone number. You'll hear a short message once the call connects.
Your new application uses the <Say>
TwiML verb to read a message to the caller using text-to-speech. You can create more powerful constructs and call flows using other TwiML verbs. Try a few, such as <Record>
, <Gather>
, and <Conference>
.
Check out these pages to learn more:
- Gather user input via keypad (DTMF tones) in Ruby
- Learn how to record incoming and outgoing Twilio Voice phone calls using Ruby
- Create conference calls in Ruby
- Dive into the API Reference documentation for Twilio Programmable Voice
- Learn how to modify calls in progress with Ruby
- Looking to make a call from your browser or mobile application? Use a Twilio Voice SDK to integrate high-quality VoIP calling.
- Retrieve information about in-progress and completed calls from your Twilio account using Ruby.
- Check out our full sample application tutorial on how to transfer calls from one support agent to another with Ruby and Sinatra
Let's build something amazing.