Get Started

Twilio SMS and MMS Ruby Quickstart

Respond to an Incoming SMS

We'll start with replying to SMS messages. When someone sends a text message to your Twilio number, you can send a reply back. Here's how:

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 the following:

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

get '/sms-quickstart' do
  twiml = Twilio::TwiML::Response.new do |r|
    r.Message "Hey Monkey. Thanks for the message!"
  end
  twiml.text
end

We're using the twilio-ruby helper library to generate TwiML, Twilio's Markup XML.

Save the file and start your local server at the command line:

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

Go to your browser at http://localhost:4567/sms-quickstart. You should see TwiML returned that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>Hey Monkey. Thanks for the message!</Message>
</Response>

If so, 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 so Twilio can reach it.

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.

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

Try It Out

Now, send a text message to your Twilio number. You should get a message back, "Hey Monkey. Thanks for the message!"

Respond with an MMS

Now modify your send-quickstart.rb on your server as follows specifying a URL of an image. If necessary, restart your server, then text your Twilio number again. You should receive a text message in response saying, "Hello, Mobile Monkey", but this time images will be included!

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
sms-quickstart.rb
require 'rubygems'
require 'twilio-ruby'
require 'sinatra'

get '/sms-quickstart' do
    twiml = Twilio::TwiML::Response.new do |r|
        r.Message do |message|
        message.Body "Body"
        message.MediaUrl "https://demo.twilio.com/owl.png"
        message.MediaUrl "https://demo.twilio.com/logo.png"
        end
    end
    twiml.text
end


Next: Replying to Messages »