How to send SMS in Rails with Twilio

Tram Ho

Introduction

Twilio is kind of service that relate to building a communication services. Before Twilio, if we wanted to send text messages or place phone calls from our web application, we would have had to connect with different carriers and operators and pay much money to their service. Twilio have many services such as SMS, MMS, Voice call … And the price also depending on the location and the service that you use. You can go to visit page Twilio to know clearly https://www.twilio.com/pricing .

Now, I going to introduce all of you to use gem twilio-ruby for sending SMS.

Config twilio

  • If you do not have a twilio account, you need a new account at this link https://www.twilio.com/try-twilio
  • After entering the full information and clicking get started you will need to verify your phone number. The verification code will be sent to the phone number you use.
  • Go to the Twilio console to get ACCOUNT SID and AUTH TOKEN. These parameters will be needed when you call the send SMS function from within the rails project.

Gem twilio-ruby

Add twilio gem to the project and run bundle install

Using twilio gem, it is very easy, in controller need to handle sending sms that you want to send as my code below:

  • twilio_sid value get from twilio after we create account
  • twilio_token value get from twilio after we create account
  • from sender phone number
  • body content sending
  • to reciever phone number
Share the news now

Source : Viblo