Class: Chatterbot::Bot

Inherits:
Object
  • Object
show all
Includes:
Blocklist, Client, Config, DirectMessages, Favorite, Followers, Helpers, HomeTimeline, Logging, Profile, Reply, Retweet, Safelist, Search, Tweet, UI, Utils
Defined in:
lib/chatterbot/bot.rb

Overview

primary Bot object, includes all the other modules

Constant Summary collapse

HANDLER_CALLS =

handlers that can use the REST API

[:direct_messages, :home_timeline, :replies, :search]

Constants included from UI

UI::API_SIGNUP_URL

Constants included from Search

Search::MAX_SEARCH_TWEETS

Instance Attribute Summary

Attributes included from Client

#client

Attributes included from Config

#config

Attributes included from Safelist

#safelist

Attributes included from Blocklist

#blocklist, #exclude

Instance Method Summary collapse

Methods included from Helpers

#botname, #botname=, #current_user, #from_user, #replace_variables, #tweet_user

Methods included from Client

#authenticated_user, #base_url, #consumer, #default_opts, #generate_authorize_url, #get_screen_name, #init_client, #login, #request_token, #require_login, #reset!, #reset_client, #reset_since_id, #reset_since_id_counters, #reset_since_id_dm, #reset_since_id_home_timeline, #reset_since_id_reply

Methods included from UI

#ask_yes_no, #deprecated, #display_oauth_error, #get_api_key, #get_oauth_verifier, #green, #red, #send_to_app_creation

Methods included from Followers

#follow, #followers

Methods included from Reply

#replies

Methods included from Favorite

#favorite

Methods included from Retweet

#retweet

Methods included from Profile

#profile_text, #profile_website

Methods included from Tweet

#reply, #tweet

Methods included from HomeTimeline

#home_timeline

Methods included from DirectMessages

#direct_message, #direct_messages

Methods included from Search

#exclude_retweets, #include_retweets, #search, #skippable_retweet?, #wrap_search_query

Methods included from Logging

#critical, #debug

Methods included from Config

attr_boolean, attr_since_id, #bot_config, #chatterbot_helper?, #client_params, #config_file, #global_config, #global_config_files, #load_config, #log_dest, #logging?, #max_id_from, #needs_api_key?, #needs_auth_token?, #no_update=, #no_update?, #slurp_file, #update_config?, #working_dir

Methods included from Safelist

#has_safelist?, #on_safelist?, #whitelist

Methods included from Blocklist

#blacklist, #interact_with_user?, #on_blocklist?, #skip_me?, #valid_tweet?

Methods included from Utils

#id_from_tweet, #id_from_user

Constructor Details

#initialize(params = {}) ⇒ Bot

Create a new bot. No options for now.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chatterbot/bot.rb', line 30

def initialize(params={})
  if params.has_key?(:name)
    @botname = params.delete(:name)
  end

  @config = load_config(params)
  @run_count = 0

  #
  # check for command line options
  # handle resets, etc
  #

  at_exit do
    if !@handlers.empty? && @run_count <= 0 && skip_run? != true
      run!
    end
    
    raise $! if $!
  end
  @handlers = {}
end

Instance Method Details

#after_runObject



79
80
81
# File 'lib/chatterbot/bot.rb', line 79

def after_run

end

#before_runObject



75
76
77
# File 'lib/chatterbot/bot.rb', line 75

def before_run
  @run_count = @run_count + 1
end

#call_api_immediately?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/chatterbot/bot.rb', line 83

def call_api_immediately?
  true
end

#register_handler(method, opts = nil, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/chatterbot/bot.rb', line 87

def register_handler(method, opts = nil, &block)
  # @todo raise error if method already defined
  @handlers[method] = Handler.new(opts, &block)

  h = @handlers[method]
  self.send(method, *(h.opts)) do |obj|
    h.call(obj)
  end
end

#run!Object

run the bot with the REST API



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chatterbot/bot.rb', line 60

def run!
  before_run

  HANDLER_CALLS.each { |c|
    if (h = @handlers[c])
      send(c, *(h.opts)) do |obj|
        h.call(obj)
      end
    end
  }

  after_run
end

#screen_nameObject



53
54
55
# File 'lib/chatterbot/bot.rb', line 53

def screen_name
  @screen_name ||= client.settings.screen_name
end