how to associate received sms messages (via twilio) to specific user in my rails app - ruby-on-rails

I'm making an app that sends text messages to a group of people and then people can respond to the text. I want to be able to record the response, know who sent it, and associate it with the user who sent the original message. I've gotten the sending working, and I've begun putting together receiving sms messages but i'm confused. If twilio sends a post request to my server with the from number and message, how will I know which of my users sent the original message?
what if two different users send a message to the same person around the same time? what's the best way to handle this? do I need a separate 'to' number for each of my users? seems like that would get quite expensive. also, if each user has multiple campaigns (messages), how would I associate a response to a specific campaign?
i'm building a rails app.thanks

You'll want to use a different Twilio number for each contact a user sends to. This will establish a unique mapping between the 2 mobile devices and a Twilio number. This allows you to forward incoming messages sent to the Twilio number back to the appropriately mapped mobile phone. You'll do the lookup on your mapping based on who the incoming message is From.
You won't need a Twilio number for every one of your users, but you will need a pool of numbers which is the maximum number of contacts a single person can make.
There is an article on Twilio's site which gives additional information:
https://www.twilio.com/help/faq/sms/how-can-i-have-users-send-text-messages-to-each-other-over-twilio

Related

How to track different conversation threads between 2 participants in twilio?

I have an application which uses Twilio to send SMSs.
There is a situation where the application sends multiple messages to a person, for example a message with info about product P1, then another message with information about product P2, and so on. So my question is, when the person replys the SMS, how could I know to exactly what SMS he is replying?
Or is it possible that each SMS that my application sends to the same person creates different conversations in Twilio, so I can track each response per each different SMS?, if so, how I can do it?
If you trigger the SMS messages by API it's hard to know what messages the customers answer.
We got to solve this problem using Twilio Sync (https://www.twilio.com/docs/sync/api/document-resource) which is basically cloud storage, so when we need to trigger an SMS or Whatsapp message, we save some information on Twilio Sync, like Twilio number, client number and the messages that were sent.
When the customer answers the message, we send him to a Twilio Studio that runs a Function that read the Sync document and check if this customer comes from a message response, if yes, we get the triggered message and added on the conversations, so we know the message that customer answered.
The Sync isn't a database and has a limit, so depending on your message volume, it can not work well.

Is there a way to send a conversation unique identifier to the Twilio webhook?

I'm trying to figure out if there is a way to uniquely identify the conversation an SMS is replying to without having to buy a new 'from' phone number each time. I know that one can store whatever you want in cookie or session storage, but those both expire in different ways. What I really want is somehow to pass or use an existing unique id for each separate conversation. So that I can map the messages to the right conversations.
I'm not sure but I wonder if its possible to send a expireless unique identifier to a webhook. So that I know which conversation it should be mapped to?
If not is there some way to use the conversation api to achieve the effect of tracking which sms messages belong to which conversations?
A common challenge, since SMS is a stateless, is to try and match up the specific request with the response. Twilio Studio's Send and Wait for Reply widget does this by only allowing one sent message (to the destination number) until there is a response OR the configurable timeout parameter expires, and then moving to the next widget, so keeping state of the interaction.
As you said, if you are attempting to have multiple simultaneous threads to the same destination phone number, you will need to use a unique From number to separate out the threads - from the received parties perspective, they don't see threading in their SMS client, and cannot pick which question to respond to.
Best Practices for SMS Message Logging
Additional Questions Q: Can a user send a customer identifier for a
message, or is only the Twilio-defined SID available?
A: Only the Twilio-defined SID is available.

Voice/SMS testing API or call lists

I am building an app using Twilio to send out text messages and phone calls. It works great but I know there are limits to the amount of messages to send out per number in any given day. The app is going to be used to send out messages in the thousands at times, for instance to alert contacts about weather cancellations.
Since I am still developing, I do not want to send out real messages to thousands of real numbers a bunch of times while I test and configure my code.
Is there any phone apis, lists, etc. of dummy numbers that can be used as my example contacts that will at least return some form of TRUE that the number exists and received my call/message? Or return something else if I hit a use limit because of the number of messages I am sending.
I think I could get everything setup with a list of 1000 numbers since I can plan out every 250-500 calls, do this or that.
Twilio developer evangelist here.
There are no lists like that that I know of.
You do have a set of test credentials that you can use to send messages to a few "magic" numbers that will behave as if the message was successful (or failed for some reason). However there's only one success message here.
The thing about the limits of 200-250 messages per number per day is that they aren't hard limits. They are just around the level that carriers will start to consider blocking your messages. At that point you will likely still get a positive response from the Twilio API as a message is queued to be sent, but it may or may not fail at delivery time.
I recommend you look into Twilio's messaging services, they allow you to create a number pool that messages are sent from. If you are sending a number of the same messages at the same time, you can also check out Twilio Notify for sending notifications. I recently wrote up a blog post on how to set up a messaging service, number pool and Notify for bulk SMS.
Let me know if that helps at all.

Twilio: How to get answers from message log with just phone number

I want to get received messages of the same day of a phone number with the help of API or anything.
(I have purchased ofcourse a phone number)
**
Requirement :
**
I have a twilio app which sends 5 questions to 1000 user and users reply's to it. Like a survey. App is deployed on Azure.
I will get 5000 messages in log.
I am planning to retrieve messages by phone number and update my database from the user.
Implemented :
Implemented this https://www.twilio.com/docs/guides/sms/how-to-receive-and-reply-in-csharp. Any other idea for the requirement is also appreciated.
Twilio developer evangelist here.
I see that you've implemented the basic flow for responding to incoming SMS messages. If you are implementing surveys across a number of users my advice would be to expand on that application.
Firstly, you would need to run through your database to get all your users' phone numbers and send the first question to all of them using the Twilio REST API to send each a text message.
Then, when you receive an incoming message from one of your respondents you can find out the number that sent the message by inspecting the From parameter on the incoming request body. You can then look up your user in your database by phone number and save that response for them. Then you can use the TwiML <Message> to respond to the user and send them the next question.
This way you can collect the responses as they come in, rather than call the REST API to list all responses.
Let me know if that helps at all.

Linking multiple phones to a single twilio account

How does one link a single twilio number to multiple phones such that:
When new people text the twilio number, all of the phones linked to the twilio number receive the text.
When any one of the twilio phones respond to the text, it appears to the original texter that a single number is responding to them (irregardless of which of the twilio phones sends the text).
Picture texts work also in the way described in (1)-(2).
This is very similar to what Google Voice can do, except google voice sometimes doesn't work with picture texts.
You would need a backend of some sort, where you can store subscribers for a number, you can then handle in your language of choice, when you receive a sms perhaps containing the word "subscribe", you would then simply save that into your backend along with the number that the sms was received upon.
When receiving a sms that contains other text, you would then simply query your backend and find all the numbers that have subscribed to the number that you have just received the sms on, you would then simply use the Twilio api, to send the message to all the results from your backend query.
You could also allow unsubscribe functionality, where users only have to send unsubscribe to the number, and you would remove them from your backend or flag them as to not send to them again.

Resources