You’re building a Rails app and you need to send SMS messages. “This could take some time,” you think. Not so! Here’s a video showing how quick it is to get started with Twilio in Ruby:
Video: How To Send SMS In Ruby 30 Seconds
As you can’t copy and paste code from a video, here’s how you’d do it:
Install the Twilio helper library for Ruby.
$ gem install twilio-ruby
Open up an IRB session and require the library.
$ irb irb(main):001:0> require "twilio-ruby" => true
Instantiate a REST client using your account sid and auth token, available in your Twilio account portal.
irb(main):002:0> client = Twilio::REST::Client.new(ENV["ACCOUNT_SID"], ENV["AUTH_TOKEN"]) => <Twilio::REST::Client>
You’ll now need three things:
- The number you are sending the message to
- The Twilio number you are sending the message from
- The body of the message
Wrap those arguments up in an hash and pass them to the
irb(main):003:0> client.messages.create :to => "YOUR_NUMBER", irb(main):004:0* :from => "YOUR_TWILIO_NUMBER", irb(main):005:0* :body => "Ahoy from Twilio!" => <Twilio::REST::Message>
Then wait for the magic to happen!
We can’t wait to see what you build
You’ve sent an SMS message and now you’re ready to take on the world of communications. Take a look at the Twilio REST API documentation to see what else you can do and the documentation for working with the Ruby helper library. Then check out our tutorials to see some more examples, like: sending SMS notifications, masking phone numbers for user privacy or two factor authentication for user security.
Excited about the possibilities? Then let me know! Drop me Twitter or just give me a shout in the comments below.