What’s In The Box!? Tracking What’s In Your Moving Boxes with Twilio SMS and Clarify.io

June 02, 2015
Written by

box

Nothing fragments your focus like moving. Even a simple task like boxing up picture frames can induce a nightmarish sense of dread. Once you finishing packing the picture frame box you’ll put it down atop the sea of other indistinguishable brown boxes, and realize you have absolutely no idea which box the frames are in. You have no idea which box anything is in. The anxiety is palpable, and also avoidable.

Graham Christensen, a developer at Clarify.io,  put an app together using Twilio’s API and Clarify.io to help make his move from house to new house smoother.

Here’s how Graham conquered the “too many boxes” trap. As he packed he labeled his boxes 1, 2, 3, etc and took a voice recording of what was in the boxes. He then uploaded all the audio data to Clarify.io, an API for audio and video that allows you to programmatically search for media and add actions.

Using Clarify.io in conjunction with Twilio, Graham easily found his stuff by texting a Twilio number. If he was looking for his silverware, he’d text the number “silverware” and immediately receive a text back, telling him what box it was in.
Graham said he knew he’d lose track of what he packed in which box, hence the app. “I knew that there would be two things that would not be misplaced no matter what: my phone and the dog. Since the dog didn’t volunteer to help, I opted to use the phone,” Graham said.

Graham outlined the flow of the app and included code samples below so you can get building your own MoveApp:

My flow was relatively simple:

  1. First, I label all my boxes 1, 2, 3…
  2. Then, I call my Twilio number
  3. It prompts me for the box number which I type in and press #
  4. Then as I packed things, I simply said what it is.

Building MoveApp

This looks like a lot of moving parts, but there are only four scripts that matter. You can follow along from my Github repository but here are the details.
 
The first script – start.php – receives the incoming call and prompts the user for a box number:
 

<?php
require 'config.php';
echo xml();
?>
<Response>
<Gather action="<?php echo $config['webroot']; ?>gather.php" method="GET">
<Say>Please enter the number of the box you are packing. When you are
done, press pound. We will keep all recordings of each box number.
</Say>
</Gather>
</Response>

The second script – gather.php – receives the box number, starts the recording, and sets the action callback. The action callback is the most important part. This will make sure that once the recording is done on the Twilio side, the recording URL will be sent to the next script.

<?php
require 'config.php';
$digit = (int)$_REQUEST['Digits'];
xml();
?>
<Response>
<Say>Recording contents for box number
<?php echo $digit; ?>.
Press pound when you're done.</Say>
<Record
timeout="3600"
finishOnKey="#"
action="<?php echo $config['webroot']; ?>save.php?d=<?php echo $digit; ?>"></Record>
</Response>

Note: By default, this will allow a recording of up to 3600 seconds (60 minutes) and only stop when you hang up or hit the # sign. Don’t forget to speak clearly!

The third script – save.php – catches the recording URL and the box number and submits it to Clarify for processing.

<?php
require 'config.php';
$number = (int)$_GET['d'];
$url = $_POST['RecordingUrl'];
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
echo xml();
echo '<Response><Say>Invalid URL.</Say></Response>';
die();
}
$clarify->saveBoxIDWithMedia($number, $url);
echo xml();
?>
<Response>
<Redirect method="GET"><?php echo $config['webroot'] ?>start.php</Redirect>
</Response>

Note: Clarify will take roughly one minute of processing for every minute of audio. Since this only happens once, it’s not an issue.

The final script – search.php – receives the incoming text message, submits the search to Clarify, processes the results and responds with a simple text:

<?php
require 'config.php';
$query = preg_replace("/[^A-Za-z ]/", "", $_REQUEST['Body']);
$names = $clarify->searchForBoxes($query);
if (count($names) > 0) {
$about = implode($names, ', ');
} else {
$about = '0 boxes';
}
echo xml();
?>
<Response>
<Message>We found "<?php echo htmlspecialchars($query); ?>" in <?php echo htmlspecialchars($about); ?>.</Message>
</Response>

A Few Parting Words From Graham

I’m happy to say that I used this for my move last month. While there were still a lot of complexities with moving, the application worked flawlessly, and it turns out that I didn’t need to be that organized. After the move, finding everything was faster and easier than I could have imagined.

You can get and deploy all the code from my Github repository or just click the “Deploy to Heroku” button on that page, fill in your Clarify credentials, and then set your Twilio voice and SMS urls to point at the app. It will be the easiest part of your move.