Codementor Events

How to use Ruby to Send Text Messages with Twilio

Published Aug 04, 2019
How to use Ruby to Send Text Messages with Twilio

Have you ever wondered how to send SMS in ruby from the terminal?
thinking.gif

Yes! You are in the right place!
Let me walk you through the steps on how I was able to send SMS using Twilio on my terminal.

Prerequisites
For this tutorial I assume you will:

  • Have a basic understanding of Ruby and Rails
  • Have both Ruby and Rails installed on your machine

Open an account with Twillo if you don't have any
After installing Twilio then open your terminal
Install the twilio-ruby gem by typing
gem install twilio-ruby
Screenshot 2019-08-02 at 12.31.48 AM.png
Type irb in your terminal to switch to the Interactive Ruby environment
Screenshot 2019-08-02 at 12.35.22 AM.png
Then next you type require twilio-ruby to use the twilio-ruby gem
In case you get this error just like I encountered on my Mac machineĀ :

LoadError: Could not open library 'sodium': dlopen(sodium, 5): image not found. 
Could not open library 'libsodium.dylib': dlopen(libsodium.dylib, 5): image not found

Screenshot 2019-08-02 at 12.39.12 AM.png

Sodium is a modern, easy-to-use software library for encryption, decryption, signatures, password hashing and more

I fixed this error by installing libsodium with Homebrew brew install libsodium with the solution, I got this from Stack Overflow and could now run require twilio-ruby
Screenshot 2019-08-02 at 1.04.16 PM.png
You need to set your Account SID ENV["ACCOUNT_SID"] and Auth Token ENV["AUTH_TOKEN"] generated from your Twilio account settings in the environment variables.
Screenshot 2019-08-03 at 9.36.13 PM.png
Type on the below piece of code on the console

client = Twilio::REST::Client.new(ENV["ACCOUNT_SID"], ENV["AUTH_TOKEN"])

Screenshot 2019-08-02 at 1.35.30 PM.png

Next, you run

client.messages.create :to => "+234806XXXXXXX", :from => "+234803XXXXXXX", :body => "Hi Nelson it's me Johnwealth from Twilio!"

The to is the receiver's phone number, from is the sender phone number while the body is the body of the message.
You might run into the error below if you are using a phone number not registered with Twilio

Twilio::REST::RestError: [HTTP 400] 21606 : Unable to create record The From phone number +234803XXXXXXX is not a valid, SMS-capable inbound phone number or short code for your account.

Screenshot 2019-08-02 at 1.41.40 PM.png
To fix the above error, you need to go and get your own Twilio phone number of which in my own case I got this Twilio phone number "+120XXXXXXXX".
I later used it as the sender number i.e. from:

client.messages.create :to => "+234806XXXXXXX", :from =>"+120XXXXXXXX", :body => "Hi Nelson it's me Abieno saying hi!"

Screenshot 2019-08-05 at 12.08.34 AM.png

Yippee! I got the SMS alert on my phone!
WhatsApp Image 2019-08-02 at 16.06.26.jpeg

It's so easy, right?
Thank you!
Your feedback is highly welcomed.
You can also check here for more about the Twilio documentation.

Discover and read more posts from Rotimi Ifedayo Nelson
get started