Class: Chatterbot::Bot

Inherits:
Object
  • Object
show all
Includes:
Blocklist, Client, Config, DirectMessages, Favorite, Followers, Helpers, HomeTimeline, Logging, Profile, Reply, Retweet, Safelist, Search, Streaming, 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]
STREAMING_ONLY_HANDLERS =

handlers that require the Streaming API

[:favorited, :followed, :deleted]

Constants included from UI

UI::API_SIGNUP_URL

Constants included from Search

Search::MAX_SEARCH_TWEETS

Instance Attribute Summary

Attributes included from Client

#client, #screen_name, #streaming_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?

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 Streaming

#handle_streaming_object, #streaming_tweet_handler

Methods included from Utils

#id_from_tweet, #id_from_user

Constructor Details

#initialize(params = {}) ⇒ Bot

Create a new bot. No options for now.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chatterbot/bot.rb', line 34

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_or_stream
    end
    
    raise $! if $!
  end
  @handlers = {}
end

Instance Method Details

#after_runObject



132
133
134
# File 'lib/chatterbot/bot.rb', line 132

def after_run

end

#before_runObject



128
129
130
# File 'lib/chatterbot/bot.rb', line 128

def before_run
  @run_count = @run_count + 1
end

#call_api_immediately?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/chatterbot/bot.rb', line 136

def call_api_immediately?
  !streaming?
end

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chatterbot/bot.rb', line 140

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

  if STREAMING_ONLY_HANDLERS.include?(method)
    puts "Forcing usage of Streaming API to support #{method} calls"
    self.streaming = true
  elsif call_api_immediately?
    @run_count += 1
    h = @handlers[method]
    self.send(method, *(h.opts)) do |obj|
      h.call(obj)
    end
  end
 
end

#run!Object

run the bot with the REST API



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chatterbot/bot.rb', line 112

def run!
  before_run

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

  after_run
end

#run_or_streamObject

determine the right API to use and run the bot



60
61
62
63
64
65
66
67
# File 'lib/chatterbot/bot.rb', line 60

def run_or_stream
  @run_count += 1
  if streaming?
    stream!
  else
    run!
  end
end

#stream!Object

run the bot with the Streaming API



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chatterbot/bot.rb', line 72

def stream!
  before_run

  #
  # figure out how we want to call streaming client
  #
  if @handlers[:search]
    method = :filter
    args = streamify_search_options(@handlers[:search].opts)
  else
    method = :user
    args = {
      stall_warnings: "true"
    }
  end

  
  streaming_client.send(method, args) do |object|
    handle_streaming_object(object)
  end
  after_run
end

#streamify_search_options(opts) ⇒ Object

the REST API and Streaming API have a slightly different format. tweak our search handler to switch from REST to Streaming



99
100
101
102
103
104
105
106
107
# File 'lib/chatterbot/bot.rb', line 99

def streamify_search_options(opts)
  if opts.is_a?(String)
    { track: opts }
  elsif opts.is_a?(Array)
    { track: opts.join(", ") }
  else
    opts
  end
end