Tuesday, December 2, 2008

Create phone applications the easy way with Twilio

Those of you who read this blog regularly know how easily excited about new technology I tend to become. But I have to admit that it's been a very long time since anything knocked me out and filled my eyes with stars as much as, new kid on the Web 2.0 block, Twilio has.

Twilio is a new service that offers a simple, REST based API on which developers can create telephony applications. Using Twilio, anyone with a basic understanding of REST and their particular language of choice can easy create an application that serves info to callers, records and responds to things the callers do, and just about anything else your techy mind can dream up.

Using Twilio is dead simple too. In less than a day, I learned the API and created a fully functioning application for OpenEMR that I'm now selling to customers. In less than a week, I've just about completely developed a new service around Twilio that should go live by Friday. Give Twilio a month and who knows what one can develop?

One of the other attractive features of Twilio is its price: free to learn and as little as $0.03 per call and $5.00 a month if you want an inbound number. Really, they've taken all of the hard work out of building telephony applications.

Lastly, let me revisit how easy it is to actually use Twilio. Once you've signed up for a free account and received your account id and authorization token, it only takes a few lines of code and a simple XML file to actually make a call.

Here's an example in PHP:


$ApiVersion = "2008-08-01";
$AccountSid = "YOUR_ACCOUNT_SID";


/* Instantiate a new Twilio Rest Client */
$client = new TwilioRestClient($AccountSid, $AuthToken);

$response = $client->request("/$ApiVersion/Accounts/
$AccountSid/Calls", "POST", array("Caller" =>
$CallerID, "Called" => "NUMBER_TO_CALL",
"Url" => "http://yourserver.com/play2caller.xml"));

That's all the PHP code it takes to make a call. You'll notice that we're also passing a standard URL to the API. This URL should contain instructions for Twilio do do once the call is connected. It can be a static XML file or something dynamic that returns XML. In our case, let's call the file "play2caller.xml". All play2caller does is plays a .WAV file to the caller then hangs up.

Here's the code (note that the periods were inserted so the code would show up in Blogger. Remove them if you plan to use this code):

<.?xml version='1.0' encoding='UTF-8' ?>

That's it! It's that easy to make a call using Twilio. Now using other Twilio "verbs" as they are called, you can enhance the system and allow your program do do things like let the called enter touch tones, dial other number, do branch logic, record spoken information, and a whole bunch of other things.

Once you get a handle on Twilio (which you can do in a weekend) you can build incredibly powerful applications that interact with the phone. Twilio is probably one of the most innovative new API's and technologies I've seen in the last two years and I'm really excited to see where they take their product.

So go ahead, sign up for an account. Build something cool and unleash it on the world. Let your application dreams run wild.