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)


twbot2.rb is a library for , which supports making bots: mainly managing OAuth tokens. 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 interpreter


A bot tweeting random greetings


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返り値は配列

A bot tweeting RSS contents


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

RSS_URL = ""

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 = }
  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
  # 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')
More details: maraigue/twbot2.rb · GitHub 詳細(英語): maraigue/twbot2.rb · GitHub



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)
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)
Version 0.21(2013.02.05)
Twitter API v1.1のために作り直した。
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) .

twbot.rbの複製・改変の条件は、(新)BSDライセンスに合わせます。BSDライセンス(原文) BSDライセンス(参考訳)