twbot.rb Library Reference

[Japanese]

Back to twbot.rb top

Introduction

To try working a bot:

Pre-defined Variables

@config (instance variable)

A hash represents the content of the configuration file (The 2nd argument in TwBot#new). If content of @config is changed, the content of the configutration file is changed into the content of @config. (The configuration file is overwritten only if the program is properly ended.)

In @config, those keys are reserved for special situations. These values is recommended not to be changed unless needed.

Instance methods

TwBot#load_data() (private method)

Method for making messages the bot posts. This method have to return an array of strings. If no messages are newly added to the list, return empty array. You can set arrays to the returned array's elements instead of string.(The item-of-array arrays are called "internal arrays" for convenience.) If you do so, first element of an internal array is treated as the message, and second element of the internal array is treated as the value of "in_reply_to_status_id" (message ID to be replied).
You do not have to call the method load_data directly. This method is called in TwBot#new(TwBot#initialize).
When TwBot#load_data itself is called, exception "NotImplementedError" is raised. Make a new class inheriting TwBot, and re-define this method.

TwBot#new(mode, config_file, log_file = nil, list_name = '', keep_config = false, testmode = false)

Method for starting getting/posting messages.

TwBot#get_followers()

Returns a hash for followers of current bot. Equivalent to TwBot.retrieve_followers(@config['login/'], @config['password/']).

TwBot#get_friends()

Returns a hash for friends of current bot. Equivalent to TwBot.retrieve_friends(@config['login/'], @config['password/']).

TwBot#follow(user)

Lets current bot follow the user. Equivalent to TwBot.make_following(@config['login/'], @config['password/'], user).

TwBot#unfollow(user)

Lets current bot not follow the user. Equivalent to TwBot.make_unfollowing(@config['login/'], @config['password/'], user).

TwBot#following_status(target)

Checks whether current bot follows and is followed by target. Equivalent to TwBot.check_following(@config['login/'], @config['password/'], target).

Class methods

TwBot.remove_reply(str){ |username| ... }

Converts all replies (format of "@USERNAME") to "@ USERNAME"("@" + space + "USERNAME") in "str", and returns converted string. This method is used for avoiding excessive replying.
You can attach a block to this method. If so, "@USERNAME" is converted if the block (user name excluding "@" is given) returns true.


From ver0.15, not only half-width "@"'s but full-width "@"'s before the screen names are converted. (Because of the change in Twitter's specification)

# Example
puts TwBot.remove_reply("@user1 @user2 Hello!")
# This puts "@ user1 @ user2 Hello!".

puts TwBot.remove_reply("@user1 @user2 Hello!"){ |u| u =~ /1/ }
# This puts "@ user1 @user2 Hello!", because "user1" meets the condition
# in the block, but "user2" not.

TwBot.retrieve_followers(login, pass, retry_count = 3)

Retrieves all followers of the user login. pass should be the password of the user login. If there are retry_count errors of API calls, this method will stop retrieving.

Returns a hash whose format is

{:result => ["user1", "user2", ...],
 :error => [exception1, exception2, ...]}

where an array of the screen names of followers is set to :result, and an array of the exceptions occurred when retrieving to :error. If errors occurred for retry_count times, the value for :result will be nil.

TwBot.retrieve_friends(login, pass, retry_count = 3)

Retrieves all friends of the user login. The formats of the arguments and the return value are same as that of TwBot.retrieve_followers shown above.

TwBot.make_following(login, pass, user)

Let the user login be following the user user. pass should be the password of the user login.

If failed in following, this method will cause an exception, including if login already follows user. The exceptions are:

TwBot.make_unfollowing(login, pass, user)

Let login be not following user. pass should be the password of the user login.

If failed in un-following, this method will cause an exception, including if login does not follow user. The exceptions to be raised is same as that of TwBot.make_following (replace "following" by "not following").

TwBot.check_following(login, pass, target)

Check whether the user login follows the user target and the other way. pass should be the password of the user login.

Returns a hash of the format {:following => boolean1, :followed => boolean2}. (boolean1 and boolean2 are either of true or false.) boolean1 means whether login follows target, and boolean2 whether target follows login.


Back to twbot.rb top
Back to top
(C)2008- H.Hiro (Maraigue / Sinryow)

Creative Commons Attribution-Noncommercial-NoDerivs (表示-非営利-改変禁止) 2.1 Japan Otherwise noted, contents in HHiro.net are available under Creative Commons Attribution-Noncommercial-NoDerivs 2.1 Japan.