handle sending sms between users over Twilio - ruby-on-rails

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.

Related

MSGraph - Getting external email conversation from reply

My team is building a ticketing system. The goal is when we receive a new email we create a new ticket. All responses to that email are saved on the same ticket.
We have these basic goals working in simple cases, however, there is one case that we are struggling to find a good solution for. A client will email us, which creates a ticket, and we reply back requiring information. The client will send our reply to someone internal to their company. Then they will send the response back to us by replying with "see below". This response will have the conversation between them and their co-worker in the comment section of the email. The comment section will also contain our entire email chain which we don't want to duplicate.
The issue we are having is grabbing the conversation they had from the comment section to include with their response of "see below" and add them to the ticket. The only method we have come up with to solve this is manually parsing the comment section of the email, however, this is error-prone.
Does anyone know of a better way of tracking conversations they send you through the email?
We are using msgraph internally to send and receive emails and using their apis they have uniqueBody and body, but they don't seem to have a way to break the body up into its different parts.
What I need any of these options
1- get list of unique bodies from the email chain without using conversation Id as it will not be sufficient in some cases.
2- get the previous conversation Id of the incoming email. I mean if that email is a list of emails and it forwards to me.
UUID uuid = UUID.randomUUID();
message.addProperty("InternetMessageId", String.format("%s",uuid.toString()));
send InternetMessageId with a unique identifier to grab conversation id
change order only

Issue with sending sms using twilio in my rails app

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.

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.

Trying to create multiple conversations to the same person/participant

I'm wondering is there someway to create more than 1 conversation to the same person? We receive inbound messages through an SMS service and then forward the text message on to the user via a Lync conversation. When we create the conversation we pass in some contextual information so that if the user responds in the lync conversation we can send the reply back to the sender via SMS.
My problem is that if more than one text message comes in from different people for the same internal user then the lync sdk is sending both message into the same conversation (which is the default behaviour for Lync) which means I lose the contextual info for the first message that initially created the conversation, also if the user writes a reply in the lync conversation I have no way of knowing to whom that message should go to. Hopefully I haven't lost you....
I can't see a setting or property in the sdk when creating conversations to create a new instance of a conversation even if one already exists for the intended sip address.
Any suggestions?
troy
I came across the same problem recently, it looks like the Lync (specificly the 2013 client, we had no problems before) automaticly merges conversations from the same user address.
If we set up a second conversation to a user from the same sip uri (our application endpoint), the first conversation will be terminated, and the second conversation becomes active. This is all seamlessly merged in the Lync 2013 client, you dont even notice.
The (crude but working) fix was to call Conversation.Impersonate() with a different uri for each new conversation. Mind you this only works when creating conversations on a Trusted Application Endpoint.

Rails and email openened notifications

I am developing an application on Rails and I need to know if its possible to write code which will notify me when the user opens an email that has been sent from my application (need to track this info) ?
The main bit of data I need is was it opened.
thanks
You can do this but there is no 100% certainty you will always get a notification.
SMTP has 2 standards, they are DSN and MDN. Both are in effect optional, there is no guarantee that the email system of the targeted email recipient (your user) will implement them too.
The easiest way is to pit in a "Return-Receipt-To:" (RRT) email header. Put some address as the content of the header. Now when a user opens an email message containing this header, the clients email reader will msot likely prompt your users whether or not to send a return receipt. If the do comply and email will be sent to the address you specified.
In Rails it could be something like:
themailer < ActionMailer::Base
def notify_read
headers['Return-Receipt-To:'] = 'notifyread#mysite.com'
mail(:to => 'users#somecompany.com')
end
end
You could just use an email address you monitor and read them manually OR you could set up rails to read these emails as well. But there is no guarantee you will ALWAYS get an acknowledgment.
Additionally you could check each email domain, many of the big free email providers have proprietary methods of requesting the return receipt. If you add ".readnotify.com" onto the end of your recipients email address you will get a return receipt. You will have to research all the big ones though.
For example:
user#yahoo.com.readnotify.com
Hope that helps
Source: http://railsforum.com/viewtopic.php?pid=147997#p147997
A common way of implenting this is to include a link to an invisible image file, with the link including sufficient details about the email for you to be able to identify which email is being viewed.
When the image is requested by the mail client, your server can then record the viewing attempt. If you use a 3rd party email provider (such as sendgrid, postageapp) then sometimes they'll do that for you and ping your server with the appropriate event. I strongly suspect that this is what readnotify is doing under the hood (someone took the trouble of looking at this a while ago
This isn't completely accurate as some (many?) users turn off remote image viewing in their mail viewers.

Resources