How to Set Up an Anonymous Texting Service – Now w/ Ruby & Heroku!
Last week we showed you how to create an Anonymous Texting Service using SMSified, Node.js and Nodester; while we know Node.js and Nodester are the new cool, there’s a whole lotta developers out there that love them some Ruby and Heroku, too, and we don’t intend to forget about you.
Presenting the same Anonymous Texting Service, written using Ruby, the Ruby SMSified Library and hosted on Heroku! This simple Ruby app receives and sends text messages to and from your phone using a completely different number, which is provided – for free – by SMSified when you sign up! By hosting the app up on Heroku, you don’t even need your own webserver .
Before we can start making our apps, you first need to go to smsified.com/signup to sign up and get your free number – then let the fun begin!
Now that you have your phone number, let’s start writing some code. This example is written in Ruby because Heroku is a Ruby based platform; other languages have other platforms to select other than Heroku, such as the previously mentioned Nodester for Node.js. To start, as always, our program will require a few requires to be able to access the libraries that we will need – which will look something like this:
require 'sinatra' require 'json' require 'net/https' require 'smsified'Your application is launched by using the HTTP method POST; the information received from the POST will need to be parsed into JSON, which will enable us to extract all of the information we need from a nice hash:
post '/' do response = JSON.parse(request.env["rack.input"].read)***JSON Hash***
{"inboundSMSMessageNotification": {"inboundSMSMessage": {"destinationAddress":"tel: 1xxxxxxxxxx", "senderAddress":"tel: 1xxxxxxxxxx", "message":"Hello", "messageId":"3034c89c31eecd1d0b86d5e4a5b82dc1", "dateTime":"2011-05-23T17:34:54.823Z"} } }The next step is to create a couple of variables: myPhoneNumber - which holds your actual phone number, callerID – which is the sender’s phone number, and finally message – , which holds the actual message from the text. The variable myPhoneNumber will be set by me; the other two variables will be set by using the hash that we created by parsing the JSON.
myPhoneNumber = 1xxxxxxxxxx message = response["inboundSMSMessageNotification"]["inboundSMSMessage"]["message"] callerID = response["inboundSMSMessageNotification"]["inboundSMSMessage"]["senderAddress"]Now that we have the information we need, there is still one thing important to figure out – am I receiving a message or sending one? An if and else statement will be required to decipher; this is determined by the callerID variable. If it is identical to the variable myPhoneNumber, then the app identifies the message as outbound (sent by me); otherwise, it is an incoming message (sent to me). Once the message’s direction is identified, then the next step is to decode the message itself.
Whenever I send an outbound text message, the first 10 characters of my message will need to be the phone number of the receiver, which is then followed by the actual message (an example would be - 14071234567 Hello world). Knowing that, when I reach the if statement, I extract and save the first 10 characters into the variable to, then slice it out of the string so the message is the only thing left. On the other hand, if the message is incoming, to will be set to myPhoneNumber, since I am the one receiving the message.
The only remaining piece left of this code section is the addition of the callerID to the message so I can tell who is sending an inbound text – which is very necessary, since I need to include that phone number in my message to be able to text them back.
if callerID == "tel: #{myPhoneNumber}" to = message[0..10] message.slice!(0..10) else to = myPhoneNumber message = callerID " " message endFinally, the last piece to the program as a whole is sending the actual request to SMSified to transfer the text messages. Looking at the code below, the first line defines the SMSified URL we’ll use for our request (SMSified URL request format). The next line uses net/http to define we’re working with a POST, followed by BASIC authorization (this is your SMSified username and password). The next line defines the request body – in this case, the receiver’s address and the message. The final bit of code actually launches the request.
url = URI.parse('http://api.smsified.com/v1/smsmessaging/ outbound/1585326xxxx/requests') req = Net::HTTP::Post.new(url.path) req.basic_auth 'username', 'password' req.set_form_data({'address' => "#{to}", 'message' => "#{message}"}) res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }To recap, you signed up for a new account on SMSified and wrote an app that forwards messages to and from your phone. The next thing to think about is the platform to run this app – as mentioned earlier, since this app is written in Ruby, there is a great platform called Heroku tailored made to host your app.
First things first – go to Heroku.com and signup for an account (which is free to try). Once you are done, you will be directed to your main Heroku page where you can see all of the tunnels to the apps that you create – so let’s create a tunnel! Getting started is quite easy and well documented – you can follow along with me or go straight to Heroku for the Quickstarts. Before continuing, there are a few prerequisites that need to be established:
Once this is setup, it is time to get things rolling!
***Written in the Terminal***
First, navigate to the folder where your app lies.
$ cd PATH/TO/MY_APP
Then you need to initialize an empty Git repository.
$ git init
Before you can add your app to this directory, there are a couple files we need to add to it. Heroku is not going to recognize Sinatra – which is a Ruby based framework app we used in our app – because Heroku is looking for primarily Ruby on Rails. We need to override this with these files: config.ru and .gems.
As of right now, you should still be in the same directory as your app – which is important. Typing the following will open Textmate in a new window and create that file in this directory.
$ mate config.ru
Textmate isn’t required, you can choose to open it with any other code editing program. When Textmate (or your editor) opens, enter in the following (the first line of code is where you put your app’s name inside the quotes – notice there is not an included extension). When this is complete, save and exit the document.
require 'hello' run Sinatra::ApplicationThe other file that needs to be added will have the same procedure.
$ mate .gems
However, the only thing that goes inside this document is the word sinatra. Once that is done, save and exit. Now you will be able to add all of these files to your repository.
$ git add .
Finally, you will commit all of the changes that are made. The message inside of the quotes are the changes that were made, if any. It is good for logging and to see any and all changes that were made. In this case since we just made the app, I will write “new app”.
$ git commit -am “new app”
Now that our app is ready to be pushed onto Heroku, we need to create a space for it there.
$ heroku create
When you run this line, Heroku will ask for your credentials – this is just your login information for Heroku. Once complete, it will create a Heroku app which you will be able to see on your main page in Heroku.
The last thing we need to do is push the app:
$ git push heroku master
You have officially put your app on Heroku! Before we consider everything good to go, let’s visit our main page to see the newly created app. If everything went well, a new app should now be visible! If you click on the app’s name, you will see all of the information about it; the first line under Git Repo is going to be the URL associated with your application, which we’ll need to connect your SMSified phone number to Heroku.
To handle the connection, go back to your SMSified account and directly across from your SMSified number there are three buttons – click the middle one (Edit Number).
Once you click the button, another box will pop up – in this box, name your app and set the Subscription URL to the URL from Heroku. Once those fields are placed then hit update and you are connected!
To test your program, all you have to do is send a text message to your SMSified number. For testing purposes, use your phone number in the message so you will receive the message.
This should work just fine; however, in the unlikely chance that you do not receive your message, there is a solution. Heroku does have logging information located here. There is a lot on this page about logging, however, you only need to know two lines of input for your terminal.
***Written in the Terminal****
$ heroku addons:upgrade logging:expanded
This upgrades the Heroku logs to give you real time logs.
$ heroku logs – -tail
This launches the logs in the terminal, which enables you to locate any bugs in your app.
This sums up how to use Heroku as a gateway to connect a Ruby app with your SMSified number. I hope this was enjoyable and informative and that you have some fun with it!
You can view my Ruby app in full here at Github.com.
©2011 SMSified. All Rights Reserved.
.
©2011 Voxeo Labs. All Rights Reserved.
.
Originally from All Voxeo Blogs Feed




Leave a Reply