Having a good old Natter
I've been thinking about an XMPP chat-bot based interface - something similar to the XMPP bot I created in May '08 - for a project that I've started playing with recently. This project is still very new so it doesn't have much code yet. A perfect time for experimenting. My recent foray into the world of 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 so I'll save that for another time.
I used Doodle to create something DSL-like that can describe, in Ruby, a chat-bot that talks XMPP. It doesn't do anything fancy yet - it doesn't deal with subscription requests for example - but it can login, send and receive messages, and it has the beginnings of a basic roster so it can track who it's seen and 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 can be retrieved using Git.
git clone http://barkingiguana.com/~craig/code/natter.git
You'll need to install xmpp4r-simple and doodle to get it to run.
sudo gem install xmpp4r-simple doodle
There's very little documentation at the moment, but there are a few simple examples in the examples/ subdirectory and there's a quick example in the README file too.
Verify database connections in long-running idle Rails processes
I've interfaced one of my xmpp4r bots with the Xeriom Networks control panel. I had intended to write a post about it to show how easy it is, but I've been beaten to it. I can, however, offer one piece of advice that will stop the bot dying after a few hours of idling: periodically verify your database connections.
RAILS_DEFAULT_LOGGER.debug "Launching database connection verifier"
Thread.new do
loop do
sleep 1800 # Half an hour
RAILS_DEFAULT_LOGGER.debug "Verifying database connections"
ActiveRecord::Base.verify_active_connections!
end
end
Adding the above code to a script will stop database connections getting dropped (or, at least, will reconnect them if it happens).
To see it in action, add support@xeriom.net to your XMPP roster and have a chat. It's not hugely intelligent, but it does support a few useful commands.
Update: the support bot is no longer running. It was fun, but not hugely useful in this context.
Love me!
If you've found this article useful I'd appreciate beer and recommendations at Working With Rails.
XMPP4R-Simple makes XMPP in Ruby uhh... simple...
I thought it might be cool to have a control interface that you could talk to using IM, something like the IM client for Twitter.
Initially I was looking at XMPP4R but a little reading pointed out that there's a gem called XMPP4R-Simple. Well, simple is always good so one gem install and 45 minutes later I had a Ruby script that could log in to an XMPP server, listen to (and log) what people said, and respond with a simple message.
#!/usr/bin/env ruby
require 'rubygems'
require 'xmpp4r-simple'
logfile = File.join('..', 'log', "#{File.basename(__FILE__)}.log")
logger = Hodel3000CompliantLogger.new(logfile)
jabber = Jabber::Simple.new "username@domain.com", "password"
sleep 1
jabber.status(:away, "No one here but us mice.")
sleep 1
jabber.deliver("craig@xeriom.net", "I woke up at #{Time.now}.")
loop do
begin
jabber.received_messages do |msg|
jid = msg.from.strip.to_s
logger.info "%s said: %s" % [ jid, msg.body ]
jabber.add(jid) if !jabber.subscribed_to?(jid)
jabber.deliver(jid, "Nom nom nom.")
end
jabber.presence_updates do |update|
jid, status, message = *update
logger.info "#{jid} is #{status} (#{message})"
end
jabber.new_subscriptions do |friend, presence|
logger.info "#{friend.jid} #{presence.type}"
jabber.add(friend.jid) if !jabber.subscribed_to?(friend.jid)
end
rescue Exception => e
logger.error e.to_s
end
sleep 1
end
Our own little pet XMPP client. How cute is that?
Quit yo jibba jabba, sucka!
If you've found this article useful I'd appreciate recommendations at Working With Rails.
