The Prologuer Bot

January 31st 2008

UPDATE: Prologuer v0.2 is out, with cool new features.

So, I got pretty excited when Matt Mullenweg introduced Prologue, a Twitter-inspired theme for Wordpress. It got even better when the theme was updated a little (from community requests), as to bear more resemblance with the Twitter experience.

I’ve been playing with Twitter-likes for a while (Noumba while I worked at af83, there’s this idea, coded one from scratch a few months ago, recently made a little toy I hope to publish soon enough, now this), exploring other ways to use the “micro-blogging / post-from-anywhere / push-and-pull equilibrium / social network” thing.

So I ran to write a bot that would make use of the oh-so-beautifully-simple Prologue interface combined with its WordPress goodness (read “available XML-RPC, tagging, easy to deploy, includes an admin interface, out-of-the-box!”) and give me the Twitter confort of having a easily accessible interface from where to post information.

Using Ruby’s XML-RPC client, the “made in Twitter” xmpp4r-simple gem and WP’s XML-RPC support of tags (through “mt_keywords”) it’s a piece of cake. You can even use daemons and have a start|stop|restart interface. Yawn! (almost).

What the Prologuer Bot does

Once you’ve added it to your buddy list, it’ll sit there waiting for you to tell him things. As soon as you do, it parses your message, scans it for hashtags and publishes it to your Prologue URL, with the hashtags as tags (works with more than one hashtag).

Prologuer Bot in action (1 of 2)

Prologuer Bot in action (2 of 2)

How it does it

If you’re looking for an IM (Jabber) bot that will post to Wordpress’s Prologue, you can start here.

Warning: The following code should not be used as is! It’s just an idea, and it works as is, but!! It lacks many a security feature, it won’t degrade gracefully at all, etc. I’m giving no further details as of how it works because if you don’t know it by reading the code you should consult all the links above. I’m a lousy dev. You’re warned.

Request:Please let me know of all things that could be enhanced (regex, logic, rescues, etc.)! Thanks

The code:

You can download it here: prologuer-01.rb.zip. Notice that the regexp is pretty dumb and you should adapt it to your own needs.

## Prologuer.rb
          # This simple code, meant to be used with WP's Prologue theme, creates a Jabber bot that will
          # 1) publish received messages to your WP account as new posts
          # 2) detect hastags (e.g #tag) and tag your new post with them
          # It's like having your own Twitter bot for your own Twitter
          # you need the xmpp4r-simple gem (sudo gem install xmpp4r-simple --include-dependencies)
          # you need a Jabber account and a WordPress install with the Prologue theme
          ##
           
          require 'rubygems'
          require 'xmlrpc/client'
          require 'xmpp4r-simple'
           
          ## Wordpress user config
          @wpuser = ''                                   #your WP username
          @wppass = ''                                    #your WP password
          @wpRPCurl = '' #example = 'http://yoursite.com/wordpress/xmlrpc.php
           
          ## Jabber user config
          @jabuser = '' # Your Jabber username: 'you@jabber.org', 'you@gmail.com', etc.
          @jabpass = ''           # your Jabber account password
           
           
          ##
          @client = Jabber::Simple.new(@jabuser, @jabpass)
          @server = XMLRPC::Client.new2(@wpRPCurl)
           
           
          def get_messages
             @client.received_messages.each do |msg|
               desc = msg.body
               from = msg.from.strip.to_s
           
               wptags = Array.new
               hashtags = desc.scan(/#[a-zA-Z]+/i)
           
               hashtags.each do |w|
                 b = w.to_s.split(/#/)
                 c = b[1].to_s.downcase
                 wptags.push c
               end
           
           
               newPost = {}
               newPost['description'] = desc.to_s
               newPost['mt_keywords'] = wptags.join(', ').to_s
           
               @server.call('metaWeblog.newPost','unused_blogid',@wpuser,@wppass,newPost,true)
               @client.deliver(from, 'OK')
             end
           rescue
          end
           
          loop do
            get_messages
            sleep(5)
          end

OK, have fun!