- API & Docs
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:
twilio-rubyhelper 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:
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
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.
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".
Now, send a text message to your Twilio number. You should get a message back, "Hey Monkey. Thanks for the message!"
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!
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