Sending Text Messages With Ruby on Rails
I read somewhere recently that suggested that with the rise of text messaging there has been a decline in the use of email. I'm not sure if I buy that one, but I do think that text messaging is an often overlooked method of communicating. Websites such as Twitter have shown the value of text messaging, and I think that as mobile phones become more and more advanced, we will see more and more websites integrating with SMS text messaging services. That being said, let's take a look at how we can send text messages to a mobile phone from a Rails application.
Enter the SMS_Fu Plugin
To start with, we will need to install a plugin that makes sending text messages almost too simple, called SMS_fu, which was created by Brendan Lim. In order to install it, simply type the following in the root directory of your Rails application:
script/plugin install git://github.com/brendanlim/sms-fu.git
Once installed, the only other thing you need to be sure of is that you have your Rails application set up to send emails. That's right, nothing more than that. As long as you have some way of sending email, you can send text messages.
Sending Text Messages
Now in your controllers, you can use the deliver_sms method to send a text message. You will need to get the carrier for the number you are sending to, which presumably means having a user select their carrier. A list of carriers that can be utilized can be found in /config/sms_fu.yml. Take a look at the following example controller code:
def send_sms
number = params[:number]
carrier = params[:carrier]
message = params[:message]
deliver_sms(number, carrier, message)
flash[:notice] = "Text message sent!"
redirect_to :action => :index
end
In this example, a user is submitting a form (which you would want to validate) that has the number, carrier and text message that they want to send. Passing these values to the deliver_sms method sends the text message. There are a couple of other values that can be passed to deliver_sms as well:
:limitwill let you limit the number of characters that your message can have. Since some phones have a 128 character cut-off, it's sometimes nice to limit the length of messages.:fromwill allow you to override the from email address that the message is sent from, which defaults to the value that you set in theconfig/sms_fu.ymlfile.
A couple of things to keep in mind as well are that the number you are sending the text message to needs to be only integers, without spaces or dashes. In addition, you'll want to be sure that you are not sending SMS text messages to anyone that has not agreed to receiving them, since in some cases there are charges associated with receiving text messages.
What About Receiving Text Messages?
The next logical question, at least for me, was to ask how I go about receiving text messages. Sites like Twitter allow you to send text messages to the system, such as to respond to a message or to post new "tweets". However, it turns out that this is more involved than simply sending text messages.
The reason is that (at least in this case) sending text messages is really just like sending an email. In the same way that having a Rails application receive emails is a bit more involved than sending them, so is receiving text messages. However, the overview is that you will first need to get a "Short Code", which is basically the number that people will send messages to. Think of it like a phone number, only a bit shorter like 657733. Depending on your carrier, short codes can be expensive.
From there, you need to have some sort of gateway that allows messages sent to your short code to be routed to your application. There are various ways to do this, and it's way out of the realm of this post. But it is (obviously) possible.
This post is filed under Developers' Corner and has the following keyword tags: ruby, rails, sms, text messaging, Brendan Lim.