Platform
Docs
Solutions
ContactLog In

3 Ways To Send Text Messages (SMS) with Ruby [How-to Guide]

SMS is one of the most widely used communication methods in the world. Hence, many organizations require messaging features for their application. Organizations can use SMS to easily reach out to end users with maximum reach and response rates compared to emails.

As developers, we should have the knowledge to implement SMS features using multiple languages and frameworks to deliver these requirements. So, in this article, I will discuss three ways to send SMS using Ruby to give you a better understanding.

1. Using Twilio API

Twilio API is one of the best services for sending SMS using Ruby. It provides a programmable SMS API with multiple additional features to make it a whole in one package. For example, you can use Twilio SMS API to send and receive SMS, monitor delivery statuses of messages, schedule deliveries, and more.

Advantages of using Twilio API

  • A most reliable way to send messages.
  • Good documentation and customer support.
  • Support multiple programming languages.

Disadvantages of Using Twilio API

  • More expensive than competitors.
  • Not very mobile-friendly.
  • Not easy for non-developer users to get started.

Tutorial: How to Send SMS Using a Twilio API

Step 1: Create a Twilio account and get a phone number

As the first step, you need to create a Twilio account. You can easily create a free Twilio account by following the steps in this guide.

Step 2: Add the Twilio gem to your project.

Install Twilio-Ruby gem to your project using gem install twilio-ruby -v 5.73.0 command.

Step 3: Update your code

Then, create a new file named sms_send.rb and update it with the below code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 require 'twilio-ruby' module SMS @account_sid = ENV['TWILIO_ACCOUNT_SID'] @auth_token = ENV['TWILIO_AUTH_TOKEN'] @client = Twilio::REST::Client.new(@account_sid, @auth_token) def send_sms(from_phone_number, to_phone_number, message) @client.messages.create( from: from_phone_number, to: to_phone_number, body: message ) end end

Finally, call the send_sms method by passing the required input parameters like below:

1 2 3 4 5 @from = '+123456789' @to= '+123456789' @message= 'This is my first SMS' send_sms(@from, @to, @message)

2. Using Plivo API

Plivo is another popular service for sending SMS using Ruby. It offers SDKs and RESTful APIs to implement SMS, audio messages, video messages, push notifications, and many other communication methods. In addition, Plivo API supports web, mobile and IoT applications.

Advantages of Using Plivo API

  • Affordable pricing.
  • Good customer and tech support.
  • Combination of long messages.
  • Support two-factor authentication.
  • APIs are customizable.

Disadvantages of Using Plivo API

  • No API channels are available for WhatsApp, social media, or online chat.
  • Does not support chatbots or multi-channel customer care.
  • Lack of proper error messages.

Tutorial: How to Send SMS Using a Plivo API

Step 1: Create a Plivo account and get a phone number

To begin, you will need a Plivo account and a phone number that can receive SMS messages. You can easily create a Plivo account by following the steps in this guide.

Step 2: Add the Twilio gem to your project

Install plivo-ruby gem to your project using gem install plivo command.

Step 3: Update your code

Then, create a new file named sms_send.rb and update it with the below code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 require 'rubygems' require 'plivo' include Plivo module SMS @client = RestClient.new("<auth_id>", "<auth_token>") def send_sms(from_phone_number, to_phone_number, message) message_created = client.messages.create( from_phone_number, [to_phone_number], message ) end end

Finally, call the send_sms method by passing the required input parameters like below:

1 2 3 4 5 @from = '+123456789' @to= '+123456789' @message= 'This is my first SMS' send_sms(@from, @to, @message)

3. Using Multi-Channel Notification API

In addition to the above APIs, you can use a multi-channel notification service like Courier to send SMS. Multi-channel notification services help you reach customers on multiple channels using a uniform API.

With the modern application requirements, using a multi-channel notification service like Courier can streamline your notification flow by creating all the relevant channels with them a single API.

Advantages of Using Multi-Channel Notification API

  • You can add additional notification channels without updating the code.
  • Smaller code base since you do not need to write code for each channel.
  • You can alert users via many notification channels using a single API.
  • Even non-technical users can manage your notification service.

Disadvantages of Using Multi-Channel Notification API

  • Limitations on the number of notifications you can get each month.
  • It can be expensive to consume numerous notification channels.

Tutorial: How to Send SMS Using a Multi-Channel Notification API

I will use Courier for this demonstration since it supports well-known SMS providers like Twillio, Plivo, MessageBird, etc.

Step 1: Creating a Courier account

You can easily create a free Courier account using this link - https://app.courier.com/signup.

Step 2: Creating a New Channel

Afterwards, create a new channel by selecting the Channels tab from the Courier dashboard. Next, you will see a list of SMS providers, and you can choose one from the list. In this case, I have selected MessageBird.

Ruby Send SMS Notification

After that, you need to enter an Originating Number and an Access Key. Then, click the Install Provider button.

Step 3: Creating a Notification

Select the Designer tab from the Courier dashboard and click the Create Notification button to create a new notification. There, select SMS as the channel and MessageBird as the provider.

Ruby Create SMS Notification

Afterwards, click the Publish Changes button and move to the Preview tab. There, click the Create Test Event button to create a new event.

Ruby Test SMS Notification

Step 4: Install Courier SDK

Install trycourier gem to your project using gem install trycourier command.

Step 5: Update your code

Then, create a new file named sms_send.rb and update it with the below code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 client = Courier::Client.new "your-auth-token" res = client.send_message({ "message": { "to": { "phone_number": "+491575556511" } }, "data" => { "test" => "test" } }) puts res.code puts res.request_id rescue Courier::CourierAPIError => re puts re.message end

Conclusion

In this article, I have discussed three different ways to send SMS using Ruby. In addition, I have discussed the advantages and disadvantages of Twilio API, Plivo API, and multi-channel notification services. My suggestions will help you to select the best method to send SMS using Ruby based on your requirements.

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.