Issue with sending sms using twilio in my rails app - ruby-on-rails

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!

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
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.

Related

How to receive webhook response to incoming messages on Twilio SMS Service

I'm using the Twilio API to create a Service that will send SMS. The Service should have its own unique webhook endpoint to receive replies (incoming SMS).
var service = ServiceResource.Create(
friendlyName: Campaign.SMSCampaignDisplayName,
usecase: "marketing",
stickySender: true,
useInboundWebhookOnNumber: false,
inboundRequestUrl: new Uri($"<my site>/Admin/twilio/receive/{Campaign.SMSCampaignGuid}")
);
var message = MessageResource.Create(
body: Campaign.SMSCampaignBody,
from: new PhoneNumber(fromNumber),
to: new PhoneNumber(Campaign.SMSCampaignTo),
messagingServiceSid: service.Sid
);
The Service is created and the SMS is successfully sent. In the Twilio console I can see the service has the correct webhook URL:
But when I respond to the SMS, the webhook is not sent. In fact, the incoming message no longer appears in the Twilio console's Monitor > Messaging logs. Note that webhooks and incoming SMS logging was working fine when I was not using a Service and the incoming SMS webhook was defined on the sender phone number.
What am I missing, or is this some issue with Services? Do I need to add a Sender to the Service even though it's sending fine? How do I get the phoneNumberSid?
Edit
I'm dumb, I could have just tested adding the phone number to the Service manually. That indeed did work, but I need to do it programmatically. I'm not having much luck finding the API/endpoint to get the correct phoneNumberSid for this call, does it not exist?
You can use the IncomingPhoneNumber resource, as documented below, to get the Phone Number SID.
List all IncomingPhoneNumber resources for your account

Pass variables from slack webhook to Autopilot

I would like to know if it's possible to use the user_name with Autopilot from the incoming post requests from Slack.
I've tried checking the documents but I'm not able to find any reference to handling incoming requests for Autopilot. Perhaps it isn't supported but does anyone know if it's possible?
Twilio developer evangelist here.
The documentation for handling incoming requests is here: https://www.twilio.com/docs/autopilot/actions/autopilot-request.
You should receive a UserIdentifier with the request which identifies the user in the platform the request came from. In the case of incoming phone calls or sms messages the identifier is the user's phone number. I haven't tested, but I assume that this would either be a user name from Slack, or an identifier you can use with the Slack API to find the user name.
Let me know if that helps at all.
I ended up being able to use the response data from Autopilot and found I was able to get the useridentifier from the json response as such:
user_name = request.form['UserIdentifier']
This has then allowed me to send direct messages via the incoming webhooks application in slack as using the above user name as the value for the channel key as follows.
payload={'text':"Ok, I'm working on it. I'll let you know when your request is ready.", "channel":user_name}
webhook_url= 'https://hooks.slack.com/services/<slack id>/<slack id>'
response = requests.post(
webhook_url, data=json.dumps(payload),
headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)

handle sending sms between users over Twilio

I've read THIS Twilio post on how to enable text messaging between users. I am building using Ruby on Rails.
At the moment, this is what I have:
A messaging system in my app where users can inbox each other.
When a user X messages another user Y, the application also sends that message to user Y's email address. User Y can reply to the message on the app or by responding to the email received by user X. If user Y choses to respond to the email (inbound email) my application processes the email received - it knows that it is intended for user X so it will forward that the email to user X's email address.
This is great because I'm giving the user several means of communication between each other (in app or email).
Now i'd like a similar but instead using the Twilio API.
Particularly i'd like to enable this type of communication:
As per Twilio help center
I'm having trouble understanding the concept of having multiple numbers that can be used by different user but somehow you know who's the message for...( Wow, I'm even having trouble describing what i'm having trouble with!)
I'm using the Ruby wrapper for the Twilio API
To send a message to user it is fairly simple:
# set up a client to talk to the Twilio REST API
#client = Twilio::REST::Client.new account_sid, auth_token
# send sms
#client.messages.create(
from: 'twilio_number',
to: 'a user number',
body: 'Hey there!'
)
To receive a message:
First configure the your Twilio phone number request url to point to an endpoint in your application. In my case its 'yourappurl.com/twilio/create'.
class TwilioController < ApplicationController
include Webhookable
after_filter :set_header
skip_before_action :verify_authenticity_token
def create
# inbound messages arrive here. Need to do something with the message, identify who's it for
# the receiving user and where its coming from ( the user who sent the text message)
render_twiml response
end
end
However this is for my application sending a message to a user's phone number. How can I handle communication between two users where user X can communicate with user Y via text messages (all going through my application)
Twilio developer evangelist here.
The help centre article on sending messages between users has a great explanation of how to implement this in theory, however you probably want to see some code.
Your best bet is to work through this tutorial on Masked numbers with Twilio which shows you practically how you would purchase a number for a user and then connect two users through that number.
In brief, you need to get a number that represents a relationship between two users. When you receive a message at that number and you get the webhook from Twilio then you can look up the relationship based on the Twilio number the message was sent to and the number it was sent from. You can then connect and send the message on to the number on the other side of the relationship.
But, as I said, I recommend going through the tutorial to really learn this in depth.
Let me know if this helps at all.

SMS parameters through twilio

I'm new to twilio and have the following situation. I want to send an SMS to someone for an appointment confirmation. They'll text back CONFIRM or CANCEL etc to the message.
Is there any way to pass an internal database id in the SMS to them and get it back in their response so i know what they're confirming or canceling? I'd rather it not be shown to the end user receiving the text.
Thanks!
Twilio evangelist here.
Unfortunately there isn't any way to transparently include metadata in an SMS and have that metadata returned to you when they reply, but there ways to address your scenario.
If you are sending the user a text message you already have their phone number and you can use that as a unique ID. When the user replies to your message, Twilio will as part of our HTTP request to your application, pass you the users phone number. You can use that number to locate them in your database and mark them as confirming or canceling.
Hope that helps.

retrieve the smsstatus after sending sms Twilio api

i m using Twilio sms api to send message form my Asp.Net application
var delivery = twilioRestClient.SendSmsMessage("+123456", mobilenumber,
string.Format("Your New Password is: {0}", genpass));
the variable delivery contains object of twiliosendsms which also contain sms status currently the status is 'queued' when the message was sent i want to know how to check if the status is changed to Sending,sent,failed,received
Twilio evangelist here.
You can use the StatusCallback to have Twilio tell you when your message was sent.
Hope that helps.

Resources