Get Started

Twilio Ruby TwiML Quickstart

Say a Message

We'll start with greeting people who call your Twilio phone number.

Setting Up Your Server

This tutorial assumes you have a Ruby development environment with Sinatra and the twilio-ruby helper library. If you don't have these yet, please see our post on setting up your environment.

Create a new Sinatra application with code below. We're using the twilio-ruby helper library to generate TwiML, Twilio's Markup XML:

twiml-quickstart.rb
require 'rubygems'
require 'sinatra'
require 'twilio-ruby'

get '/hello-monkey' do
  Twilio::TwiML::Response.new do |r|
    r.Say 'Hello Monkey'
  end.text
end

Save the file and start your local server:

$ ruby twiml-quickstart.rb
== Sinatra has taken the stage ...
>> Listening on 0.0.0.0:4567

Go to your browser at http://localhost:4567/hello-monkey. You should see TwiML that looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Hello Monkey</Say>
</Response>

If you do, great. You're ready to move on to the next step.

Let Twilio Find Your Server

At this point in the tutorial, you will need to find a way to expose your server to the public Internet.

Here are some tutorials that may be able to help you:

For the rest of the tutorial, we will assume your server is available at example.com.

Try it out

Copy and paste the URL of your servlet into the "Voice" URL of a number listed on the Numbers page of your Twilio Account. On the page for that number, change the Method from "POST" to "GET".

Now call your Twilio number! You should hear a voice say "Hello Monkey" in response. When you call, Twilio will fetch your URL, and execute the XML instructions listed above. Then, Twilio will hang up, because there are no more instructions.


Next: Greet callers by name »