twbot.rbは、RubyにてTwitterのbot(プログラムにより自動的に発言を行うアカウント)作成を行う際に、その補助を行うライブラリです。どちらかといえばフレームワークに近いかもしれません。
特に、定期的に特定の位置からデータを取得し(RSSやTwitterのタイムラインなど)、それに基づいて発言を行うbotを作るのに適したものとなっています。ただしそれ以外の用途であっても、程度は落ちますがbot作成の助けにはなるでしょう。
※ただし、データを保持しておくことにあまり意味のないbotを作成する場合は、より簡便な方法もあります。ついったのお知らせbot(@miru氏提供)のような方法をお勧めします。またTwitter4Rを使うのもよいでしょう。
Rubyインタプリタの導入されている、定期的にプログラムを実行させるためのPC
twbot.rbの中では、TwBotというクラスが定義されています。ただしTwBotはそのままで利用するものではなく、次の手順を踏む必要があります。
例として、RSSを定期的に取得し投稿するためのbotを作成する場合のコードを示します(上記zipファイルにも同梱しています)。
# [en] Twitter bot of posting contents in RSS using twbot.rb
# [ja] twbot.rbを用い、RSSを取得してTwitterに投稿するbotプログラムのサンプル
require "cgi"
require "open-uri"
require "twbot"
RSS_URL = "http://d.hatena.ne.jp/maraigue/rss"
class RSSPost < TwBot
def load_data
# [en] Getting RSS
# [ja] RSSを取得する
buf = nil
open(RSS_URL){ |file| buf = file.read }
result = []
@config["already_gotten"] ||= ""
# [en]
# Getting entries in RSS
# (Using REXML library is better for strictness, but this program
# does not use for simpleness.)]
# Each of RSS entry is stored in the variable "entry".
#
# [ja]
# RSSの各エントリを取得する
# 厳密にはREXMLを使うべきだが、ここでは簡略化している
# entryにはRSSでの各記事が格納される
buf.scan(/<item.*?>.*?<\/item>/m).each do |entry|
# [en] Get title and URL of the entry
# [ja] エントリのタイトル・URLを取得
title = nil
entry.scan(/<title>(.*?)<\/title>/){|tmp| title = CGI.escapeHTML(tmp[0])}
link = nil
entry.scan(/<link>(.*?)<\/link>/){|tmp| link = CGI.escapeHTML(tmp[0])}
# [en]
# If the entry is already gotten by this program, ignore this entry and
# all following(older) entry.
#
# [ja]
# すでに取得したことのあるデータとURLが一致していたら、
# それ以降の(それよりも古い記事の)データはすべて無視する
break if link == @config["already_gotten"]
# [en]
# If the title and the URL of the entry is gained, add a message to
# the list.
# Because RSS lists entries from newer one, to post messages in the
# original order, the message have to be added to the bottom of the
# array("result").
#
# [ja]
# タイトル・URLが取得できていたら、投稿する発言のリストに追加する
# RSSでは新しい記事ほど先に並ぶので、元の記事の順に発言するため、
# 結果の配列(result)の後ろから投稿内容を追加している
result.unshift("#{link} #{title}") if title && link
end
# [en] Keeping the URL of the newest entry in the data gotten now
# [ja] 取得したデータのうち、最も新しい記事のURLを保持しておく
unless result.empty?
@config["already_gotten"] = result.last.split(/ /).first
end
# [en] Returning logged message and the result
# [ja] ログされるメッセージと結果を返す
@logmsg = "(#{result.size} post added)"
result
end
end
SELFDIR = File.dirname(__FILE__)
ARGV.each do |mode|
RSSPost.new mode, "#{SELFDIR}/config.yml", "#{SELFDIR}/error.log"
end
# [en]
# To run this program, use commands like these:
# ruby rsspost-sample.rb load # Load RSS and add messages to the list
# ruby rsspost-sample.rb post # Post one message in the list
# ruby rsspost-sample.rb post post # Post two messages in the list
# ruby rsspost-sample.rb load post # Load RSS and add messages to the list,
# # and post one message in the list
#
# [ja]
# プログラム起動時は以下のようにする
# ruby rsspost-sample.rb load # RSSをロードして発言リストに追加する
# ruby rsspost-sample.rb post # ロード済みの発言を1つ発言する
# ruby rsspost-sample.rb post post # ロード済みの発言を2つ発言する
# ruby rsspost-sample.rb load post # RSSをロードしたのち、発言を1つ発言する
これを起動する手順は以下の通りです。
ポイントは、根本的に書かなければならないコードは「botが何を発言するか」だけであることです。それさえ書けば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の複製・改変の条件は、(新)BSDライセンスに合わせます。BSDライセンス(原文) BSDライセンス(参考訳)
簡単に言えば、このソフトは無保証であり、また再配布(改変した上での再配布含む)の場合にも元の著作権表示などの部分が記されている必要があります。
私が使いやすいように書いているため、使いにくい点があるかもしれません。あらかじめご了承下さい。要望は可能な限りお受けします。
スクリプトに関する報告は、Twitter/h_hiroおよび、メールアドレスmain(あっとまーく)hhiro.netにお願いします。