Thursday, July 7, 2011

The 5-minutes Twilio App

This week, I interviewed for a position at Twilio. Twilio sells an API that enables your web applications and mobile app to use voice and SMS messaging capabilities. You are charged a small fee per incoming/outgoing call/message, and you get full backend support, including IVR, call statistics and the ability to reply automatically based on content and context.

The recruiter suggested I build something using the Twilio API. Since I have so many APIs running in my head these days, I was a bit hesitant at first to learn another one, especially if I didn’t see an immediate use for it. I was further less impressed by the use of XML (over JSON) in the API. But I gave it a shot, and within 5 minutes of starting I had my one (and only) Twilio API written: the SMS Calculator (no tm or c need apply). One minute after that and I had it tested. 5 minutes after twitting about it, someone asked me if he can demo my app in a code meetup. Once I said ‘yes’, my app officially became open sourced.

So, without further ado, here’s the code to my app, and the steps you need to take to create your 5-minutes Twilio app in 5 easy steps:

1. Go to twilio.com and create a free account. All you need is an email and a password.
You get free $30 on your account to use for testing. Good for 3000 SMS messages.

2. Paste the following PHP code into a text file and name it calc.php

<?php
 $body = $_REQUEST['Body'];
 $expression = explode(" ", $body, 3);
 $a = intval($expression[0]);
 $b = intval($expression[2]);
 $result = 0;
 switch($expression[1]) {
  case "+":
   $result = $a + $b;
   break;
  case "-":
   $result = $a - $b;
   break;
  case "*":
   $result = $a * $b;
   break;
  case "/":
   if($b != 0)
    $result = $a / $b;
   else
    $result = "Division by 0";
   break;
  default:
   $result = "ERROR";
   break;
 }
 $reply = "You asked: $body. The reply is: $result";
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
 <Sms><?php echo $reply ?></Sms>
</Response>

Code explanation:
This is a rudimentary, "stoopid", expression calculator. It expects an operand-space-operator-space-operand syntax (i.e. 2 + 2), does not use any regular expressions and does not check for correctness. The only error it checks for explicitly is division by 0 (and that was added only because I had points deducted for a similar exercise years ago by an evil TA).
  • We get the SMS body in the $_REQUEST[‘Body’] HTTP variable.
  • We proceed to split that string, using space as a delimiter. I limit the split to 3 strings.
  • We parse the 2 expected integers
  • We attempt to parse the operand (only the main 4 are supported)
  • We calculate the result and compose the reply. I could probably use more string tricks here, but what’s the use?
  • Finally, we compose the SMS XML that Twilio expects and output it.

3. Put the page on a web server that supports PHP. There are plenty of free hosting choices if you don’t have one ready (off the top of my head, try Zymic)

4. In your Twilio account page, scroll down to the section “Developer Tools”. Input the full path to the calc.php page in the “SMS URL” field, and notice the phone number and PIN you were allocated.

Twilio

At this point you’re actually done. Step 5 is the testing.

5. Using your phone, send an SMS message to the phone number. Start by putting in your PIN number (only required with test accounts) and then add an expression. It should look like:
xxxx-xxxx
25 / 5
You should receive an immediate reply, echoing the given expression and the result. For the full free experience, test using your Google Voice account. Google provides free SMS texts in the US and Canada, and the Twilio number is a US one.

I will certainly keep Twilio in mind for future uses in my apps, but as for now, I'm just happy that it took me longer to write this post than to write the code.

My next step: slapping an Eliza or an 8-ball app at the end of this :)

Update:

Andrew Watson from Twilio asked to borrow my sample for his presentation. Here's a link to the Google Doc Andrew has published. Thanks Andrew!

Oh, and it seems like I won't be working for Twilio after all, so I'm free to play with other new toys :)