lib/chatterbot/bot.rb in chatterbot-2.0.5 vs lib/chatterbot/bot.rb in chatterbot-2.1.0
- old
+ new
@@ -3,11 +3,10 @@
#
# primary Bot object, includes all the other modules
class Bot
include Utils
- include Streaming
include Blocklist
include Safelist
include Config
include Logging
include Search
@@ -24,13 +23,10 @@
include Helpers
# handlers that can use the REST API
HANDLER_CALLS = [:direct_messages, :home_timeline, :replies, :search]
- # handlers that require the Streaming API
- STREAMING_ONLY_HANDLERS = [:favorited, :followed, :deleted]
-
#
# Create a new bot. No options for now.
def initialize(params={})
if params.has_key?(:name)
@botname = params.delete(:name)
@@ -44,11 +40,11 @@
# handle resets, etc
#
at_exit do
if !@handlers.empty? && @run_count <= 0 && skip_run? != true
- run_or_stream
+ run!
end
raise $! if $!
end
@handlers = {}
@@ -56,63 +52,11 @@
def screen_name
@screen_name ||= client.settings.screen_name
end
-
#
- # determine the right API to use and run the bot
- #
- def run_or_stream
- @run_count += 1
- if streaming?
- stream!
- else
- run!
- end
- end
-
- #
- # run the bot with the Streaming API
- #
- 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
-
- #
- # the REST API and Streaming API have a slightly different format.
- # tweak our search handler to switch from REST to Streaming
- #
- def streamify_search_options(opts)
- if opts.is_a?(String)
- { track: opts }
- elsif opts.is_a?(Array)
- { track: opts.join(", ") }
- else
- opts
- end
- end
-
- #
# run the bot with the REST API
#
def run!
before_run
@@ -135,26 +79,19 @@
def after_run
end
def call_api_immediately?
- !streaming?
+ true
end
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
+ h = @handlers[method]
+ self.send(method, *(h.opts)) do |obj|
+ h.call(obj)
end
-
end
end
end