How to Send SMS through MSG91 in Ruby on Rails?
In one of our recent project we had a requirement of sending an OTP through the transactional SMS to any number even if the number is in the DND mode. Especially to the Indian mobile numbers.
Transnational SMS VS Promotional SMS
Any SMS that enters India is divided into 2 categories, Promotional and Transnational. See below the difference.
Generally, we use the Twilio service for sending the SMS. Twilio is the cloud communication platform which provides many communication services like Voice, SMS, Video and many more. But It has certain limitations for promotional messages in India. It falls under Promotional, hence it will be blocked if the number is registered in the DND.
How to check my number is blocked or not?
We can visit the following link to see that the number is indeed blocked to receive any commercial content:
MSG91 is the SMS service provider which provides services like sending Transactional, Promotional and SendOTP. It is easy to implement and no need to provide any detail or documents to enable the Transactional SMS. Also, it is cheaper compared to Twilio. See the pricing plans.
Before we start the implementation, we need the Authkey and the SenderId to create the connection with MSG91 and send the SMS through API.
AuthKey: Navigate to Dashboard → API and click on the AuthKey.
SenderID: Navigate to Dashboard → Settings → Account tab. Add the Sender ID as per your needs, select an Industry and the Time Zone and update.
Now, let’s start with the Rails part.
Add the msg91ruby gem to your Gemfile and run the
$ bundle command.
Create a new service file
msg91_message_service.rb under /app/services directory and paste below script.
- to_number – Pass the mobile number on which you want to send.
- message – Pass the message whatever you want to send.
- MSG91_AUTH_KEY and MSG91_SENDER_ID – Set the environment variables – AuthKey and SenderID
Now, call this function and pass the required parameters to send the SMS. for e.g.,
Msg91MessageService.new.send_sms(9898989898, 'Hi, Parthiv')
Send to multiple phone numbers,
Msg91MessageService.new.send_sms([9898989898, 9999999999], 'Hi, Parthiv')
Thanks for reading!
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
Subscribe to Our Newsletter
- Digital Marketing
- IT Consulting
- Project Management