Class: Chatterbot::Bot
- Inherits:
-
Object
- Object
- Chatterbot::Bot
- 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
Constants included from Search
Instance Attribute Summary
Attributes included from Client
Attributes included from Config
Attributes included from Safelist
Attributes included from Blocklist
Instance Method Summary collapse
- #after_run ⇒ Object
- #before_run ⇒ Object
- #call_api_immediately? ⇒ Boolean
-
#initialize(params = {}) ⇒ Bot
constructor
Create a new bot.
- #register_handler(method, opts = nil, &block) ⇒ Object
-
#run! ⇒ Object
run the bot with the REST API.
-
#run_or_stream ⇒ Object
determine the right API to use and run the bot.
- #screen_name ⇒ Object
-
#stream! ⇒ Object
run the bot with the Streaming API.
-
#streamify_search_options(opts) ⇒ Object
the REST API and Streaming API have a slightly different format.
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
Methods included from Reply
Methods included from Favorite
Methods included from Retweet
Methods included from Profile
#profile_text, #profile_website
Methods included from Tweet
Methods included from HomeTimeline
Methods included from DirectMessages
#direct_message, #direct_messages
Methods included from Search
#exclude_retweets, #include_retweets, #search, #skippable_retweet?
Methods included from Logging
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
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_run ⇒ Object
135 136 137 |
# File 'lib/chatterbot/bot.rb', line 135 def after_run end |
#before_run ⇒ Object
131 132 133 |
# File 'lib/chatterbot/bot.rb', line 131 def before_run @run_count = @run_count + 1 end |
#call_api_immediately? ⇒ Boolean
139 140 141 |
# File 'lib/chatterbot/bot.rb', line 139 def call_api_immediately? !streaming? end |
#register_handler(method, opts = nil, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/chatterbot/bot.rb', line 143 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
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/chatterbot/bot.rb', line 116 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 |
#run_or_stream ⇒ Object
determine the right API to use and run the bot
65 66 67 68 69 70 71 72 |
# File 'lib/chatterbot/bot.rb', line 65 def run_or_stream @run_count += 1 if streaming? stream! else run! end end |
#screen_name ⇒ Object
57 58 59 |
# File 'lib/chatterbot/bot.rb', line 57 def screen_name @screen_name ||= client.settings.screen_name end |
#stream! ⇒ Object
run the bot with the Streaming API
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/chatterbot/bot.rb', line 77 def stream! before_run # # figure out how we want to call streaming client # if @handlers[:search] method = :filter args = (@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
103 104 105 106 107 108 109 110 111 |
# File 'lib/chatterbot/bot.rb', line 103 def (opts) if opts.is_a?(String) { track: opts } elsif opts.is_a?(Array) { track: opts.join(", ") } else opts end end |