Text Messages & Ruby on Rails - ruby-on-rails

Is there any way to use text messages to CRUD data in a Rails app? For instance, a user would send a text message to a specific number and the Rails app could use that data in any way. Just wondering if there's a solution out there.

Lots of web services exist to do this. Try out http://twilio.com/.

my name is Jarod and I work at Twilio.com. It would be very easy to build a Rails app that handles incoming SMS using the Twilio API. Here is a high-level overview of how it would work:
Sign up for twilio
Point your new Twilio number SMS Url to the Rails controller/action you'd like to invoke.
Process the incoming request and do something with the data.
Note: When Twilio makes the request to your Rails app it will pass helpful parameters with the request including:
From: The phone number that sent this message.
To: The phone number of the recipient.
Body: The text body of the message. Up to 1600 characters long.
NumMedia: The number of media items associated with your message
Some geographic data like City, State, etc.
Here is a simple Rails app that does just that.
Hope this helps!

Related

Twilio SMS Webhooks and Group Text Messages

Is it possible to maintain both group text messages and single text messages with users and keep them separate? I understand there is no concept of a group message per se, but wanted to see if any recent advances in tech has made this possible with Twilio or other providers.
In short, I am creating a POC where a user inside an iOS App can send invoke an API call to my backend application. This application receives a phone number from someone's contact list, and then connects to Twilio to send the SMS message to that target user.
When the user replies, I was researching Twilio Web hooks to receive the message, and then save it in the database. The originating user, then, would be able to see the message on a screen.
I would guess that when a user responds to the twilio message, the only metadata that comes in is their message and phone number, so the "foreign key" is the phone number.. Thus when I save it in my database, I have their phone number and message.
This works up until someone decides to target two or more people in an SMS message using my API, and then target one of those people individually. For example they select me as a sole recipient, and then select me and someone else as a group text message.
In this case, how could my system/Twilio differentiate between if I was responding to the group message, or to the single message?
Any ideas or work arounds? Maybe another technology? Thanks!
I wanted to provide an answer to this in case anyone else was looking into this.
Essentially you pay 3 cents (0.03) per month per active user in each group. Basically you buy phone numbers for each group chat you need.
https://www.twilio.com/conversations/pricing
So if you're doing a million group chats obviously it can get costly, but for simple POCs this isn't the end of the world.
Enjoy!

Twilio SMS - what am I doing wrong?

So, I have WHMCS setup to use Twilio with a new module, however, I have managed to get as far as setting it up and having SMS messages go out to customers in Australia as I have an AUS number connected to my Twilio account.
The issue is that when say for an example a US customer signs up the SMS fails and says "cannot send sms", the logs aren't helpful for this Twilio newb.
Here is the example:
The process of sending SMS to the user phone number (+1310xxxxxxx) from the summary page has failed - Twilio (company name)
If I was to send a message to say Spain it seems to work, which is odd enough.
I also tried on a +34 and it sent. Also seems to work on the UK +44.
I have GEO options setup to work as they should within Twilio, though, the error message does not appear on my Twilio logs, simply internal.
Here is the module log.
I have only edited the text out to give you what is showing.
Request:
Array
(
[To] => +1310xxxxxxx
[Body] => test
[From] => Company name
)
Response
The 'To' phone number: +1310xxxxxxx, is not currently reachable using the 'From' phone number: Company name via SMS.
So presently SMS messages to the USA do not work, I cannot speak for every other country out there though, what can I do here or how would I set my Twilio account correctly to ensure my SMS messages are being delivered?
Many thanks.
Twilio developer evangelist here.
From what you're saying I think you're sending messages using an alphanumeric sender ID rather than a phone number?
If that's the case then you are getting rejected from the US because the US telephone system doesn't accept alphanumeric sender IDs (neither do some other countries, just not the ones you've tested). You can see the countries that do and don't support alphanumeric sender IDs in this table in our documentation.
To fix this you'll need a number to send the messages from in the US, I'd recommend getting a new US number for this.
Then you can either catch the error and retry using the number in place of the sender ID.
Or, for a more resilient method, take a look into Twilio Messaging Services that can do automatic failover to a number when sending with alphanumeric senders.
Let me know if this helps at all.

Twilio Messaging - Correlating SID

We use twilio for sending message.
We are not sure how to correlate the response with the message we send. We might send multiple messages to the same Mobile. But, not sure how to correlate response with the messages we sent as the SID's are different.
Is there anyway to relate the response with the message.
Thanks
No, SMS doesn't work like that.
I you send me 5 text messages from your cellphone and then I reply to one you have no way of telling which one I'm replying to.
It's not a Twilio limitation, the SMS standard has no provision to track replies to individual messages
As an afterthought I came up with a hacky solution to this. It's a bit involved so I guess it depends how much you want the functionality.
This works for me using Chrome beta on Android 7.0, YMMV.
Create a php script with the following code and put it on your webserver:
<?php
// increase last digit as necessary to suit string length of your variable
$smsid = substr($_SERVER["QUERY_STRING"],0,1);
// Query database for SMS id, record timestamp of request, optionally return text to be included at the beginning of the SMS reply
$msg= urlencode($databaseResult);
// Remove <?body=$msg> if you just want the link to create a blank reply. Change the phone number to your incoming Twilio number.
header( "Location: sms:+1555444333?body=$msg" )
Now sign up for a URL shortening service which passes URL parameters and create a shortened URL which points to your php script. I used tr.im.
Depending upon your volume of SMS you will have to adjust the length of your variable, but unless you spam people to death I'm going to assume a single character will be enough to identify a unique text.
Using the example tr.im/SMS as your shortened url, you append a variable to the end like so tr.im/SMS?A and put the link in your outgoing SMS. When the user clicks the link your server redirect will open the SMS app on their phone and create a text to your number. If you have included the "?body=$msg" in your php above the new message will have your text at the start.
Personally I probably wouldn't bother adding text, they might delete it before they send it anyway and it's just likely to confuse people. If you log the request variables and timestamps to your database you should be able to tie them together with the phone number as most people will send you their reply within a couple of minutes of the server request. You can also increase the length of your custom URL variable if you struggle to correlate messages. Recycle variables once you have linked a reply etc...
Finally change your Twilio configuration so your outgoing SMS present the company name instead of your Twilio number as the sender. Users cannot directly reply to messages if the sender isn't a number, so they will have to use your link.
Generate a sequential identifier for each message and append it to your link. Save the identifier to your database along with the corresponding message Sid from Twilio and the number you sent it to so you can match them up later.
Append "Click tr.im/SMS?$ to reply" to outgoing SMS, where $ is your variable.
Profit.

How do I use Twilio to receive text messages from users and reply to that text with a message?

1.) User sends a text message to a designated Twilio phone number
2.) Application receives this message and parses it for actionable data
3.) Application replies to sender with a text message delivering the requested information
How the heck do I do this with rails?!
Assuming your business logic doesn't take that long to execute, it's pretty simple to process a text and send a response. Here are the core steps:
Grab the twilio-ruby helper library.
Point your SMS Url to the Rails controller/action you'd like to invoke
Process the incoming request & execute your business logic
Instead of returning HTML, return TwiML
Here's a link to a simple app that does just this:
https://github.com/crabasa/twilio-rails-demo
Update: I've written two detailed blog posts on integration Twilio into a Rails 4.x app: Part 1 and Part 2.

Confusion with Moonshado Heroku Add-on

According to this documention, it seems as if Moonshado will automatically route any mobile-originated SMS message to your application if it contains the first word as your keyword. I do not understand how this is possible. Any SMS message in the US that contains the first word as your keyword will be routed to your app? How is it possible for Moonshado to read every message? There must be something I'm not getting.
For SMS messages originating in the US, any messages sent to 69940 containing your keyword as the first word, will be routed to your application.
So if you want your users to be able to send SMS messages to you application, you need to tell them to SMS your keyword to '69940'.
It mentions it very briefly at the bottom of the documentation:
Further reading:
US incoming SMS ShortCode is 69940 (International MO support coming soon)

Resources