Skip to content

cjohansen.no

Twibot gets a bunch of new features

A couple of weeks ago I received a pull request from Mike Demers for Twibot. However, in contrast to the occasional bug fix I receive, this branch included several new features.

New search and hashtag handlers

Twibot now has native search and hashtag handlers, that work exactly like existing handlers. Pass the method a block that accept the message and a hash of parameters (resulting from regex searches, see the part on "routes" in the readme):

# Search for tweets matching a query.  The available search operators
# are explained here: <http://search.twitter.com/operators>
#
search "twibot" do |message, params|
  # do_something
end

# Search for tweets with a hashtag
#  see: <http://twitter.pbworks.com/Hashtags>
#
# Note: hashtag is just a convenience wrapper
# around search.  It will invoke the search
# before and after filters.
#
hashtag "twibot" do |message, params|
  # do_something
end

# Search for tweets with one of a number of hashtags
#  see: <http://twitter.pbworks.com/Hashtags>
#
# Note: hashtags is just an alias to hashtag
#
hashtags [:twibot, :ruby, "twitter4r"] do |message, params|
  # do_something
end

Automated following

One sorely missed feature in Twibot is the ability to easily follow other tweeters as well as get notification when the bot itself is followed. Thanks to Mike Demers, this is now possible:

# Process any new followers.  user_id will be
# the user's Numeric id and params will always
# be an empty Hash.
#
# add_friend!(id) is a convenience wrapper around the
# twitter4r friendship method.  remove_friend!(id)
# is also available.
#
follower do |user_id, params|
  # keep out the riff-raff...
  bot.add_friend!(user_id) unless user_id == 890631
end

Custom callbacks

The final addition is the ability to set up some code that will run before or after (or both) each handler. You may specify which handler is to be pre- or post-processed, or you may process all handlers:

# add some set-up code that will be called
# before each polling cycle.  :all is the
# default, so it can safely be omitted
#
before :all do
  MyApp.log("Started polling at #{Time.now}")
end

The after hook for the polling cycle gets passed the number of messages that were processed:

after :all do |message_count|
  MyApp.log("Finished polling at #{Time.now}.  Got #{message_count} messages.")
end

These callbacks are invoked for each iteration in the main loop (which spins until the bot is aborted).

Each action has before and after hooks available:

There can be only one before and one after callback registered for a given type. The callback block will be called with no arguments. Note: hashtag and hashtags are just wrappers around search and do not have their own hooks. Use the search hooks when using hashtag or hashtags.

before :message do
  MyApp.is_processing_a_message = true
end

after :message do
  MyApp.is_processing_a_message = false
end

after :follower do
  MyApp.log("I have another follower!")
end

When can I use this?

If you want: now! Officially: soon. The new features unfortunately lack tests, and I'm not comfortable doing a gem release on RubyForge without tests, so until I can find the time to add tests for these, there'll be no new gem. Adding these features does however give me the sense of fairly function complete, so the next Twibot gem will probably be 1.0.0. Sub 1.0 release numbers are just a hassle anyway.

If anyone else wants to help out adding the tests, just fork away and start hacking: you're more than welcome to do so!

Thanks to Mike

Once more I'd like to extend my thanks to Mike, who developed these features for his two sites LyricRat.com and BlameDrewsCancer.com. Even though the features don't have tests, these two sites had used Twibot to process more than 40.000 tweets without fail using two weeks ago, so it's not completely unsafe to play around with. Great work!

Side note: I seem to have some trouble pushing the merge to GitHub right now, so meanwhile check out Mike's repository.

Possibly related

2006 - 2012 Christian Johansen Creative Commons License