Issue with sending sms using twilio in my rails app

822 views Asked by At

I am building a rails application (based out of India) wherein I need to send sms to users whose bookings are confirmed. I am using a trial account on Twilio for this purpose. However, when I try to send sms, the status always shows as 'pending' and I don't receive the sms to my desired destination number. What could be the reason?

I have created a notification_controller with the below code:

class NotificationController < ApplicationController

  skip_before_action :verify_authenticity_token

  def notify
    client = Twilio::REST::Client.new 'my_account_SID', 'my_account_Token'
    message = client.account.messages.create from: 'twilio_number_assigned', to: 'my_number', body: 'Learning to send SMS you are.'
    render plain: message.status
  end

end

and made a POST request to /notifications/notify.

UPDATE:

In case another bulk sms provider service has worked for your rails app, feel free to share the related docs. Thanks!

1

There are 1 answers

7
rmagnum2002 On BEST ANSWER

Try to send it as hash, that's what Twilio suggest and that's how I used it in my app:

client = Twilio::REST::Client.new account_sid, auth_token 
client.account.messages.create({
    :from => 'twilio_number_assigned', 
    :to => 'my_number', 
    :body => 'Learning to send SMS you are.',  
})

Also you can test a message send request:

https://www.twilio.com/user/account/developer-tools/api-explorer/message-create

You'll fill in the needed fields and you'll get below the Request the code example you should use in your app.

About the Status of a message:

What you do in controller is returning current status of the message at the time of delivery, normally this will be as pending. You need to set up a callback that will be used by Twilio to notify you when the message was sent.

https://www.twilio.com/docs/api/rest/sending-sms

StatusCallback

A URL that Twilio will POST to when your message is processed. Twilio will POST the SmsSid as well as SmsStatus=sent or SmsStatus=failed.

Sending messages to Indian numbers

https://www.twilio.com/help/faq/sms/are-there-limitations-on-sending-sms-messages-to-indian-mobile-devices

  1. They cannot be sent to any phone number in India’s Do Not Call Registry

If you’ve been having trouble sending SMS messages to an Indian number, see if that number is registered on the National Do Not Call Registry.

If the owner of the phone number wishes to start receiving SMS messages from Twilio, they can update the DNC settings by following the instructions here.

Please note that the above limitations are regulations set up by Indian government.