twbot2.rb (new version)
twbot.rb (old version)
twbot2.rb, the renewed version of twbot.rb, has released. As old twbot.rb does not support OAuth, It will not work from August 31st, 2010. You can change the library from old twbot.rb to twbot2.rb with a little work. (2010.5.16)
twbot2.rb is a library for Ruby programming language, which supports making Twitter bots. This program might be said a framework.
twbot.rb especially help you make Twitter bots that get data periodically from certain place (RSS, Twitter timeline, etc.) and create updates from that data.
A PC for executing program repeatedly with Ruby interpreter
In twbot.rb, there is a class definition of class TwBot. Class TwBot can not be used by itself, but be used with following step:
This is a sample program of Twitter bot with twbot.rb, which posts how many replies came to the bot. (This is attached to the zip archive.)
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
$: << File.dirname(__FILE__)
require "twbot2"
require "rexml/document"
class SimpleBot < TwBot
def load_data
# =============================================================
# What the bot talks should be defined in 'load_data' method.
# This example is to fetch mentions to the bot and notify them.
# =============================================================
# ---------- fetch replies ----------
# downloading the XML
response = auth_http.get("/1/statuses/mentions.xml")
xml = REXML::Document.new(response.body)
# extracting only message IDs of the XML
mentions_ids = xml.elements.to_a("/statuses/status/id")
mentions_ids.map!{ |x| x.text.to_i }
# ---------- exclude old replies ----------
# If you create a new key in the Hash @config,
# this will be kept in the config file.
@config['last_id'] ||= 0
# keeping the newest ID among the downloaded mentions
new_last_id = mentions_ids.first
# excluding the IDs representing old messages
mentions_ids.reject!{ |m_id| m_id < @config['last_id'] }
# renewing the newest ID
@config['last_id'] = new_last_id
# ---------- return the message to be posted ----------
# The return value must be an array.
# If you specify strings as the return value,
# the bot will talk it as it is,
# when you run 'ruby simplebot.rb post'.
["#{mentions_ids.size} new replies found!"]
end
end
self_path = File.dirname(__FILE__)
ARGV.each do |mode|
SimpleBot.new mode, "#{self_path}/config.yml", "#{self_path}/error.log"
end
This program is run by:
What is important is that you have to write code for creating messages posted by the bot.
TwBot#get_followers, TwBot#get_friends, TwBot#follow, TwBot#unfollow, TwBot#following_status, TwBot.retrieve_followers, TwBot.retrieve_friends, TwBot.make_following, TwBot.make_unfollowing, TwBot.check_following)(C)2008- H.Hiro(Maraigue)
twbot.rb can be copied and changed under (new) BSD License. BSD License
Simply, this script is distributed under no warranty, and re-distributed version have to contain original copyright information.
To report about this script, talk to Twitter/h_hiro, or send mail to main (AT) hhiro.net .