Dec 05

I pulled the Zork Twitter bot apart and rebuilt it using more sensible means.   HTMLUnit for scraping the actual WebZork game and Apache Commons Config for configuration reading.  The code was also restructured to support Twiiter Bot plugins. 

It is a failry simple interface constisting of 4 callbacks that are invoked on interesting Twitter events.
The methods are passed a Twitter client object that allows the bot to send tweets. 

The default example that simple echos any tweet sent to it is:

package com.glulogic.twitter.bot

public class DefaultTweetHandler {

  // Each method called is passed a 'context' hashmap
  // containing the following keys:
  //
  //  from    -  the shortname (login) of the user who sent the Tweet
  //  body    -  the contents of the Tweet (@... removed)
  //  twitter -  an object that can send tweets to Twitter users

  // called when someone sends an  metion (e.g. '@...')
  def mention(context) {
    context.twitter.mention('to': context.from, 'message': "You said ${context.body}")
  }

  // called when someone sends a direct message (e.g. 'd ...')
  def direct(context) {
    context.twitter.direct('to': context.from, 'message': "You said ${context.body}")
  }

  // called when a user begins following
  def follow(context) {
  }

  // called when a user stops following
  def unfollow(context) {
  }
}

Leave a Reply

Subscribe without commenting