Sun, 07 Dec 2008

Windows Azure to Twitter and Back

I’ve just added some Twitter sweetness to my Windows Azure blog.

The sweetness comes in two forms.  First, in the layout for the blog, I’ve added a sidebar with cool stuff like who I am and how to reach me.  In there is also the five most recent tweets that mention my Twitter name “smarx.”  Second, every time I create a blog post, I’m now automatically tweeting a short message announcing the blog post.

Pulling from Twitter

There’s nothing specific to Windows Azure here, but I thought I’d share what I did.  I ended up modifying the solution Twitter suggests for adding tweets to your blog.  They basically give a little bit of JavaScript and encourage you to use the user_timeline API method.  I was a bit disappointed with this, though… I wanted to also show any public @ replies.  One option would be to authenticate, but that meant I’d have to do it server-side, and I’d have to be careful about privacy.  (What if someone’s protecting their tweets, but I pull it because it’s an @ reply to me?)

I ended up using search.twitter.com, which provides its own API.  Then I adapted Twitter’s blogger.js to work with the format returned from the search API.  Feel free to grab my code if you’d like to do the same thing on your blog.  (Note that because it’s just a search for “smarx,” it may pick up other references.  It might be better to search for “@smarx” and merge that with the user timeline.)

Pushing to Twitter

I’ll go ahead and admit it now: I could have just used TwitterFeed, but that would hardly be a challenge.  Instead, I used a Windows Azure worker role to handle tweeting my posts.  This works almost identically to my spam detection.  Every time a blog post is created, a message is queued that references the partition key and row key of the new entry.  A worker pulls the message from the queue, queries the table to find the blog entry, and then uses Twitter’s update method to create a tweet.

The great thing about this pattern is that, just like with my spam detection, if something goes wrong (like my worker has a bug or Twitter is down), normal operation of my blog continues.  Because of the reliable queue in Windows Azure, when things get back to normal, the worker will just pick up the unfinished work and create the tweet.  Loose coupling is awesome for reliability and efficiency.

Worker code

The code in the worker role to handle creating tweets looks like this:


var emsg = entryQueue.GetMessage();
if (emsg != null)
{
    var split = emsg.ContentAsString().Split('/');
    var partitionkey = split[0];
    var rowkey = split[1];

    var entry = (from e in svc.BlogEntryTable
                 where e.PartitionKey == partitionkey && e.RowKey == rowkey
                 select e).FirstOrDefault();
    if (entry != null)
    {
        var shortened = new StreamReader(
            WebRequest.Create(string.Format(
                "http://is.gd/api.php?longurl=http://blog.smarx.com/posts/{0}", entry.Permalink
            )).GetResponse().GetResponseStream()
        ).ReadToEnd();
        twitter.Update("smarx", "GETYOUROWNPASSWORD!",
            string.Format("blog: \"{0}\" {1}",
                entry.Title,
                shortened
            ), Twitter.OutputFormatType.JSON);
    }
    entryQueue.DeleteMessage(emsg);
}

I’m using the Yedda Twitter C# Library to talk to Twitter, and is.gd to shorten URLs.

Postscript

Everything worked!  I hit refresh on my blog and just saw this tweet appear on the right:

smarx blog: "Windows Azure to Twitter and Back" http://is.gd/axci less than a minute ago