I had never built an app that needed to send SMS, but our last project at the firm required it. I talked to a friend about it and he told me about Twilio. I did some quick research and found the docs really complete and easy to follow.
For those who don't know Twilio, it's a platform that lets you integrate voice, text messages, video, notifications and other things into your app through an API. Integrations are available in Ruby, Java, .NET, Node.js, PHP, and others.
I'll show how easy it is to send SMS from the Ruby console. Soon I plan to do a screencast on how to implement this in a Ruby on Rails app.
Step 1: Create a Twilio account
Create a Twilio account and access the Twilio Console.
Sign up: https://www.twilio.com/try-twilio
Console: http://twilio.com/console
When you create the account you get a Trial for running tests:
Don't forget to register a verified number.
https://www.twilio.com/console/phone-numbers/verified
Note: Verified numbers are required to send SMS in TRIAL mode.
Step 2: Send the SMS
Install the Twilio gem:
gem install twilio-ruby -v 5.21.2
Open a Ruby console:
irb
Inside the console we import the twilio-ruby gem, set up the variables and send a message.
require 'twilio-ruby'
# Get your SID and Auth Token from twilio.com/console
# DANGER! This is insecure.
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
number = 'your_number' # Number you rented from twilio
@client = Twilio::REST::Client.new(account_sid, auth_token)
message = @client.messages.create(
from: number, # +15017122661
body: 'My first message sent with ruby.',
to: '+5544999801281' # +5544998761234
)
puts message.sid
Keep in mind this is insecure. It's important to keep credentials like the SID and Auth Token stored in a way that prevents unauthorized access. Since this is just a test example, I used them straight in the console.
Wrapping up
As you can see, it's pretty easy to send SMS with Ruby.
Twilio offers many other services and you can check the pricing for all of them at https://www.twilio.com/pricing.
Originally posted at guilherme44.com.

Top comments (0)