twbot2.rb (new version)

twbot.rb (old version)

* No longer accessible to Twitter (OAuth authentication is not used)※現在はTwitterへのアクセス不可能(OAuthによる認証に未対応のため)

twbot2.rb version 0.21 is released, working with . APIs of twbot2.rb are refined, however, programs for library version 0.20 still works with library version 0.21. The document for version 0.21 is available at . (2013.2.5)

Since , please re-authenticate accounts if you use direct message APIs. To re-authenticate a user USERNAME, first remove 'users/USERNAME' entry (including 'token', 'secret' or like section under it) from config.yml, then execute 'ruby BOT_SCRIPT_NAME.rb add=USERNAME'. (2011.6.28)

twbot2.rb, the renewed version of twbot.rb, has released. As old twbot.rb does not support OAuth, . You can change the library from old twbot.rb to twbot2.rb with a little work. (2010.5.16)

twbot2.rbのバージョン0.21をリリースしました。への対応済みです。またtwbot2.rbのAPIが更新されています(なお、バージョン0.20向けに書かれたプログラムはだいたいバージョン0.21でも動きます)。バージョン0.21向けドキュメントはをご覧ください(現在は英語のみ)。 (2013.2.5)

。利用されたい場合は、再度認証をお願いします。なお再度認証を行うには、まずconfig.ymlファイルから'users/USERNAME'のエントリ(直下の'token'や'secret'も含む)を削除し、その後'ruby BOT_SCRIPT_NAME.rb add=USERNAME'を実行します。(2011.6.28)

twbot.rbの新しいバージョン「twbot2.rb」を公開開始しました。古いtwbot.rbはOAuthに対応しておらず、のでご注意下さい。なお、twbot.rbからtwbot2.rbへの移行は少ない労力で行えるようになっております。(2010.5.16)

twbot2.rb is a library for , which supports making bots: mainly managing OAuth tokens. This program might be said a framework.

twbot2.rbは、にてのbot(プログラムにより自動的に発言を行うアカウント)作成を行う際に、その補助を行う(主に、OAuthトークンの管理を行う)ライブラリです。どちらかといえばフレームワークに近いかもしれません。

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.

特に、定期的に特定の位置からデータを取得し(RSSやTwitterのタイムラインなど)、それに基づいて発言を行うbotを作るのに適したものとなっています。

A PC for executing program repeatedly with interpreter

インタプリタの導入されている、定期的にプログラムを実行させるためのPC

A bot tweeting random greetings

あいさつをランダムに発言するbot

require "./twbot2"
TwBot.create("config-file.yml", "log-file.log").cui_menu do
  # Define what the bot tweets hereここに話す内容を定義する
  tweet_list = ["Good morning", "Good afternoon", "Good evening"]
  [tweet_list[rand(tweet_list.size)]] # return as an array返り値は配列
end

A bot tweeting RSS contents

RSSの内容を発言するbot

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

RSS_URL = "http://d.hatena.ne.jp/maraigue/rss"

require "cgi"
require "open-uri"
require "./twbot2"

SELFDIR = File.dirname(__FILE__)
TwBot.create("#{SELFDIR}/config-rsspost.yml", "#{SELFDIR}/error-rsspost.log").cui_menu do
  # Download RSS
  buf = nil
  open(RSS_URL){ |file| buf = file.read }
  
  result = []
  @config["already_retrieved"] ||= ""
  newest_entry = false
  
  # Retrieve entries in RSS
  # (It is better to use REXML library; this program omits it
  # for simpleness.)]
  buf.scan(/<item.*?>.*?<\/item>/m).each do |entry|
    # Extract titles and URLs of entries from downloaded RSS
    title = nil
    entry.scan(/<title>(.*?)<\/title>/){|tmp| title = CGI.escapeHTML(tmp[0])}
    link = nil
    entry.scan(/<link>(.*?)<\/link>/){|tmp| link = CGI.escapeHTML(tmp[0])}
    
    # Ignore entries already retrieved by this program
    #
    # NOTE: @config["..."] values are kept for the next running
    # of the program.
    break if link == @config["already_retrieved"]
    
    # Adds a message to the list to be tweeted later.
    #
    # Because RSS stores entries from newer one, to post messages in the
    # original order, the message have to be added to the bottom of the
    # array 'result'.
    result.unshift(TwBot."[AUTO POST] #{link} #{title}") if title && link
    
    # Keep the URL of the newest entry in the data retrieved now
    unless newest_entry
      newest_entry = true
      @config["already_retrieved"] = link
    end
  end
  
  
  # Set logged message
  @logmsg = "(#{result.size} post added)"
  
  # Return the result
  #
  # NOTE: the returned value must be an array of tweets when a bot
  # is constructed (called by 'ruby SCRIPTNAME.rb load')
  result
end
More details: maraigue/twbot2.rb · GitHub 詳細(英語): maraigue/twbot2.rb · GitHub

(English)

OLD VERSIONS:

Version 0.10(2008.10.26)
First version
Version 0.11(2008.12.14)
Being able to post same messages successively (As this change, key ["last_post/"] is added to the variable @config)
Version 0.12(2008.12.18)
Showing "from twbot.rb" with twbot.rb's post (since Maraigue got permission from Twitter administrator)
Version 0.13(2009.03.09)
Version 0.14(2009.04.14)
Version 0.15(2009.12.10)
Version 0.20(2010.05.16)
Newly written to use OAuth
Version 0.21(2013.02.05)
Remade for Twitter API v1.1.
APIs of twbot2.rb are refined.
Version 0.22(2015.10.30)
File lock for configuration file is added.
Version 0.23(2018.07.29)
Version 0.10(2008.10.26)
初版公開
Version 0.11(2008.12.14)
同一の発言を2連続で投稿可能にした。これに伴い、@config変数に["last_post/"]キーを追加。
Version 0.12(2008.12.18)
Twitter運営からの認可を得て、twbot.rbからの投稿時に「from twbot.rb」と表示されるようにした。
Version 0.13(2009.03.09)
Version 0.14(2009.04.14)
Version 0.15(2009.12.10)
Version 0.20(2010.05.16)
OAuth対応のため一から書き直した。
Version 0.21(2013.02.05)
Twitter API v1.1のために作り直した。
twbot2.rbのAPIを更新した。
Version 0.22(2015.10.30)
設定ファイルをファイルロックするようにした。
Version 0.23(2018.07.29)

(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 .

twbot.rbの複製・改変の条件は、(新)BSDライセンスに合わせます。BSDライセンス(原文) BSDライセンス(参考訳)
簡単に言えば、このソフトは無保証であり、また再配布(改変した上での再配布含む)の場合にも元の著作権表示などの部分が記されている必要があります。

私が使いやすいように書いているため、使いにくい点があるかもしれません。あらかじめご了承下さい。要望は可能な限りお受けします。

スクリプトに関する報告は、Twitter/h_hiro_および、メールアドレスmain(あっとまーく)hhiro.netにお願いします。