Having a Good Old Natter

January 13, 2009

I've been thinking about an XMPP chat-bot interface — something like the XMPP bot I built back in May '08 — for a project I've recently started playing with. The project is still brand new, barely any code, which makes it the perfect time to experiment. My recent foray into ActiveCouch reminded me of a library called Doodle that I've been meaning to get to grips with. Can you see where this is going?

Doodle is a Ruby library and gem for simplifying the definition of Ruby classes by making attributes and their properties more declarative.

Doodle has a number of advantages over the ActiveCouch approach, but this isn't a post about Doodle — I'll save that for another time.

I used Doodle to build something DSL-like that can describe, in Ruby, a chat-bot that speaks XMPP. It doesn't do anything fancy yet — it doesn't handle subscription requests, for example — but it can log in, send and receive messages, and it has the beginnings of a basic roster so it can track who it's seen, who it's talked to, and when.

Natter.bot do
  channel do
    username "username@domain.com"
    password "sekrit"
  end
  on :message_received do |message|
    puts Time.now.to_s + "> " + message.body
    reply_to message, "Thanks for your message!"
  end
end

If you'd like to play with it, the code is available via Git:

git clone http://barkingiguana.com/~craig/code/natter.git

You'll need xmpp4r-simple and doodle installed:

sudo gem install xmpp4r-simple doodle

Documentation is thin on the ground for now, but there are a few simple examples in the examples/ directory and a quick walkthrough in the README.

Questions or thoughts? Get in touch.