Wed, 02 Sep 2009

Simple Example of Twilio and Windows Azure

As you may have already seen, Twilio chose Windows Azure as this week’s category for their developer contest.  (UPDATE: Now more like two weeks!)  In addition to Twilio’s normal prize of a netbook, we’ve kicked in a couple additional prizes for this week: a copy of Windows 7 and $500 of free Windows Azure use after our commercial launch in November.

To help give everyone a head start on getting their contest entry ready, I thought I’d put together a simple example of using Twilio from a Windows Azure application.  While not quite as cool as my earlier experiment with Twilio (The CIA Pickup), this example should serve as a basic, how-to-make-it-work sample.

The Application

wazsong screenshotEarly in the days of the Windows Azure CTP, Tobias Zimmergren recorded a song called “I Can (Windows) Azure You.”  It’s kind of catchy.

My sample Twilio + Windows Azure application calls you at a phone number you specify and plays the song to you over the phone.  Play with it at http://wazsong.cloudapp.net and download the full source.

What You’ll Need

  1. A Windows Azure account (Register for your invitation code, and get it instantly!)
  2. A paid Twilio account with one Outgoing CallerID Number (This sample makes outbound phone calls, which free trial accounts can’t do.) UPDATE [9/2/2009 @ 5:15PM]: I got this wrong initially.  Trial accounts can do this as long as you put in a valid Caller ID Number of your own.  See Dennis’s comment below.
  3. The Windows Azure SDK and tools (These require Windows Server 2008, Vista, or Windows 7.)

The Code

There’s exactly one page to this application.  The markup isn’t terribly interesting; it’s basically just a text box and a submit button.  The code-behind handles making the call to Twilio (using Twilio’s C# helper library).  Here’s the full code-behind (default.aspx.cs):


using System;
using System.Collections;
using Microsoft.ServiceHosting.ServiceRuntime;
using TwilioRest;

namespace WindowsAzureSong_WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void singButton_Click(object sender, EventArgs e)
        {
            var id = RoleManager.GetConfigurationSetting("TwilioID");
            var token = RoleManager.GetConfigurationSetting("TwilioToken");
            var account = new Account(id, token);
            var vars = new Hashtable();
            vars["Caller"] = RoleManager.GetConfigurationSetting("OutboundNumber");
            vars["Called"] = phoneNumber.Text;
            vars["Url"] = string.Format("http://{0}{1}", Request.Url.Host, ResolveUrl("~/callscript.xml"));
            vars["Method"] = "GET";
            account.request(string.Format("2008-08-01/Accounts/{0}/Calls", id), "POST", vars);
            submittedLabel.Visible = true;
        }
    }
}

As you can see, it generates a reference to callscript.xml, which just looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
  <Play>http://cdn.blog.smarx.com/files/WindowsAzureYou.mp3</Play>
</Response>

That URL is where I’ve stored the song (in a public container in Windows Azure blob storage), but you could put it anywhere (including in your app).

Links

Play with the sample at http://wazsong.cloudapp.net and download the full source code.  Enjoy, and good luck if you enter the contest!