Keeping a Text Log of Received Messages

While receiving text messages is pretty simple – tie a URL to your phone number, any SMS that comes through is automatically redirected to that URL – what you do with the SMS information once it arrives is a different story all together. We already showed you how to work with CouchDB to create a full SMS Management system, but that might be more complex than you really need.

An easier and far simpler example would be a basic app that takes the redirected SMS content sent to your URL by SMSified, interprets it, formats the content so it’s easier to read and then writes the details into a plain text file. Every language has a way to do that, but since we currently have SMSified libraries available in PHP, Ruby and Node.js, we’ll show you how to do it in each of those languages. Each works a little differently, especially since the PHP SMSified library includes a couple shortcut functions to extract info out of the JSON content.

Since PHP is the unique case, we’ll start there:

require 'smsified-php/inbound.class.php';

$json = file_get_contents("php://input");

$inbound = new InboundMessage($json);

$time = $inbound->getTimeStamp();
$message = $inbound->getMessage();
$sender = $inbound->getSenderAddress();

$text = "From: ".$sender."n"."Message: ".$message."n"."Date & Time: ".$time."nn";

$fh = fopen('log.txt', 'a');
fwrite($fh, $text);


The app first requires inbound.class.php from the PHP SMSified library, retrieves the content of the JSON sent to the app by SMSified when an inbound message comes through, then splits it up into the time the message was sent, the message that was sent and who sent it. It then adds those together for one formatted variable, which is then added to a text file called log.txt using fopen and fwrite. Note the inclusion of n, which gives us some clean newline formatting both after each line and to space between records.

The Ruby library doesn’t have the same shortcut options available yet, but it’s still pretty short:

require 'sinatra'
require 'json'

post '/smsified.rb' do

  response = request.env["rack.input"].read
  response = JSON.parse(response)

  message =  response["inboundSMSMessageNotification"]["inboundSMSMessage"]["message"]
  callerID =  response["inboundSMSMessageNotification"]["inboundSMSMessage"]["senderAddress"]
  time =  response["inboundSMSMessageNotification"]["inboundSMSMessage"]["dateTime"]

  text = "From: #{callerID} nMessage: #{message}nDate & Time: #{time}nn"'log.txt', 'a') {|f| f.write(text) }


The app initially requires sinatra and json, to run the webserver and interpret JSON respectively, and then reads and interprets the JSON sent to the app by SMSified, parses it into individual fields, rebuilds the data into a single formatted variable and writes the content to log.txt using and fwrite.

The Node.js app is probably the most complex, mostly because the file open/file write portion is a little heavier than it is in the other languages:

var http = require('http');
var fs = require('fs');

var server = http.createServer(function (request, response) {

    request.addListener('data', function(data){
        json = data.toString();

    request.addListener('end', function() {

        var sms = JSON.parse(json);
        var sender = sms.inboundSMSMessageNotification.inboundSMSMessage.destinationAddress;
        var message = sms.inboundSMSMessageNotification.inboundSMSMessage.message;
        var time = sms.inboundSMSMessageNotification.inboundSMSMessage.dateTime;

        var text = "From: "+sender+"n"+"Message: "+message+"n"+"Date & Time: "+time+"nn";"log.txt", "a", undefined, function(err, fd) {
            if (err) throw err;
            fs.write(fd, text, null, "utf8", function(err, written) {
                if (err) throw err;



The app requires http to create the webserver and fs for the file open and file write portion. It then creates the server, adds a couple listeners to pull in the JSON and parses it into individual fields. The app next combines the fields into a single formatted variable and then writes them to the log.txt file.  Assuming we used each of these apps to send to the same file, the output should look like this:

From: tel:+14075550100
Message: PHP Test
Date & Time: 2011-06-21T18:22:33.946Z

From: tel:+14075550100
Message: Ruby Text
Date & Time: 2011-06-21T19:42:42.820Z

From: tel:+14075550100
Message: Node Test
Date & Time: 2011-06-21T20:14:53.389Z

Bam, quick and easy logging for inbound messages in three different languages. Let us know in the forums if you have any trouble with the apps or suggestions, we’re happy to help.

©2011 SMSified. All Rights Reserved.


Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − one =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>