I’ve had the pleasure of using Twilio on a number of occasions, but only recently did I ever have the chance to use their voice calling functionality. While it was pretty easy to use once I figured out what I was looking for, it’s a different setup from Twilio SMS. In the following post, I’ll describe how to both send a text with Twilio and use Twilio to make a phone call on your behalf to say anything of your choosing. We’ll be using Sinatra and Ruby for this example. I’ve also expanded on my previous post and now when a new email comes in, we don’t just text someone but also call them and read the subject of the email.
Let’s get started!
Step 1: Setup Twilio
First you’ll need to head on over to Twilio.com and create an account. On the Account Page, you’ll see your SID and your Auth Token, both of which are required to interact with their service. Take note of these and your Twilio Number.
Next you’ll need to install the twilio-ruby gem.
gem install twilio-ruby
require 'twilio-ruby'
We need to authenticate with Twilio, so we’ll need a method to do that and a way to bring in the tokens you grabbed earlier. I like to keep my tokens and such as environmental variables, and because I’m using Sinatra I can take advantage of their settings configuration.
configure do
set :twilio_number, ENV['TWILIO_NUMBER']
set :twilio_sid, ENV['TWILIO_SID']
set :twilio_token, ENV['TWILIO_TOKEN']
end
def twilio_auth
account_sid = settings.twilio_sid
auth_token = settings.twilio_token
client = Twilio::REST::Client.new account_sid, auth_token
end
To get access to the client, just do client = twilio_auth and you’ll be talking to the Twilio Services. Pretty easy!
Step 2: Send a Text
This is all the code you need once you’ve added authentication.
def send_sms_with_twilio(number, subject)
client = twilio_auth
client.account.sms.messages.create(
:from => "+#{settings.twilio_number}",
:to => "+#{number}",
:body => subject
)
end
Step 3: Make a Phone Call
To make a simple call with no feedback from the person who receives the call, we just need to be able to tell Twilio what to say.
The initial interaction with Twilio is the same as with texting, except you have to give Twilio a URL to call that describes what to do once a call has been initiated. I setup an endpoint within the application that just creates an XML page with the POST parameter “subject”. It’s simple, but it does what I need it to.
def call_with_twilio(number, subject)
client = twilio_auth
url_subject = URI.escape(subject)
client.account.calls.create(
:from => "+#{settings.twilio_number}", # From your Twilio number
:to => "+#{number}", # To any number
# Fetch instructions from this URL when the call connects
:url => "http://#{request.host}/twilio_speak?subject=#{url_subject}"
)
end
post '/twilio_speak' do
Twilio::TwiML::Response.new do |r|
r.Say params[:subject]
end.text
end
And that’s it-You’re done!
Checkout the Example
Combine this with last week’s post and you can now have your emails call you! I combined it all and put it up on Github. Or you can checkout the Sinatra app below.
The Twilio API is really powerful, and their documentation is excellent, so head over to https://www.twilio.com/voice/api and learn more about creating dynamic phone systems with Twilio. I can’t wait to try out a more complicated example!