No PhD

I work for Nature Publishing, but I haven't got a PhD

Clojure Robot on Google Wave

with 8 comments

I’ve wanted a little project to start learning Clojure in a bit more detail, and having got access to Google Wave a couple of weeks back, I figured a suitable challenge would be to build and deploy a simple Wave robot to Google App Engine. So I did. And may I present… CENSOR BOT!

Censor Bot (censorbot@appspot.com if you have a Wave account) listens to messages posted in a wave and removes naughty swear words. It’s a pretty simple beast (about 40 lines of Clojure in all), but is non-trivial enough to serve as a good first project. In this post I’ll go through the process of getting a Clojure Wave robot built, hosted on GAE and accessible in your wave account.

Firstly we have some dependencies:

  • Java 6
  • Ant
  • A Google App Engine account (sign up here)
  • A Google Wave account
  • The Google App Engine SDK
  • Clojure 1.0

Firstly, download and install the Google App Engine SDK from here. Just unzip the SDK (latest is v1.2.2) somewhere useful. Whilst you’re there, it’d be a good idea to set up your GAE account too. Once you’re signed in (at http://appengine.google.com) you can click the “Create an application” button and reserve your app identifier. Note this down as you’ll need it later.

Next, ensure you’re running Java 6; you should see the following:

$ java -version
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03-211)
Java HotSpot(TM) 64-Bit Server VM (build 11.3-b02-83, mixed mode)

If you’re running a pre Java 6/JDK 1.6 version, you’ll need to upgrade as the Wave API only runs on Java 6. I suffered a couple of gotchas here on OS X. Firstly, open Applications/Utilities/Java Preferences.app and update the list so Java 6 is selected. This ensures code runs under that version of the JVM. However, Ant will still try and compile under JDK 1.5. You have to adjust your JDK symlinks too.

$ cd /System/Library/Frameworks/JavaVM.framework/Versions
$ sudo rm CurrentJDK
$ sudo ln -s 1.6 CurrentJDK

You’ll also need to download and install Clojure. Instructions here.

Now the fun can begin. cd to your favourite project location and run:

$ git clone git://github.com/indmill/censorbot.git

This has everything you need to write a basic Clojure Wave robot. The only things you’ll need to change are:

  • In build.xml, update the sdk.dir property to point to your GAE SDK location.
  • In war/WEB-INF/appengine-web.xml, change the application to be your GAE application identifier.
  • That’s pretty much it, unless you’re feeling brave and want to rewrite my newbie Clojure code.

You should just be able to run:

$ ant deploy

now to deploy the app to GAE. It may complain that your GAE credentials are not stored. I had to run:

$ path/to/gae-sdk/bin/appcfg.sh update war

And voila, you should be able to add your newly deployed censorbot to a Wave and off you go. The censorbot doesn’t censor many actual rude words yet, but feel free to edit its dictionary.

The code itself is very straightforward if you have done any Clojure. The main entry point from Wave-land is the function:

(defn -processEvents
  [this bundle]
  (let [wavelet (.getWavelet bundle)]
    (welcome-if-new bundle wavelet)
    (doseq [event (blip-submitted-events (.getEvents bundle))]
      (censor (.getBlip event)))))

It retrieves the wavelet from the event bundle, welcomes itself to the wave then filters out BLIP_SUBMITTED events and censors the contents. I’ve tried to make it as functional as I can but proper Clojure hackers out there could probably tidy it up a lot. Anyway, I welcome any comments, and if my instructions have missed any steps let me know and I’ll improve. Thanks.

About these ads

Written by spanx

23rd July, 2009 at 5:01 pm

Posted in Code

8 Responses

Subscribe to comments with RSS.

  1. Nice work!

    Note: You can actually set the JVM prefs it /Application/Utilities using the java preferences app. If you delete the symlink and make your own, it will break when apple upgrades java.

    Tim Dysinger

    24th July, 2009 at 1:30 am

  2. It’s amazing how trivial this is to do in Clojure, though the amount of XML you have to write is pretty annoying!

    I did almost exactly the same in Clojure too, see http://www.fatvat.co.uk/2009/07/google-wave-and-clojure.html.

    Jeff

    24th July, 2009 at 11:50 am

  3. Oh god… I can’t believe we have to write war files to make a robot. Why couldn’t they have used OSGi or basically anything which didn’t involve writing huge amounts of XML?

    Zing

    28th July, 2009 at 11:38 pm

  4. I’d love to try this myself. Too bad I can’t get a Wave account. :(

    Marc

    29th July, 2009 at 4:16 am

  5. How do I find censorbot@appspot.com from my Wave account ?

    Brandon

    5th August, 2009 at 11:47 pm

    • Sorry, I missed this!

      From your Wave dashboard, just click the + button under your contacts and add censorbot@appspot.com to your contacts.

      spanx

      12th August, 2009 at 2:52 pm

  6. […] people have already experimented with writing robots in Clojure (for example here and here). However, these approaches still assume that the development is done on Google App Engine […]

  7. […] to end up skipping most of the gritty details of getting the actual robot set up and refer you to this well-written post about the subject, be sure to follow all the instructions and you should have a usable clojure-bot for Google Wave. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: