Module: Chatterbot::Streaming
- Included in:
- Bot
- Defined in:
- lib/chatterbot/streaming.rb
Overview
simple twitter stream handler
Instance Method Summary collapse
-
#handle_streaming_object(object) ⇒ Object
Take the passed in object and call the appropriate bot method for it.
- #streaming_tweet_handler ⇒ Object
Instance Method Details
#handle_streaming_object(object) ⇒ Object
Take the passed in object and call the appropriate bot method for it
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chatterbot/streaming.rb', line 28 def handle_streaming_object(object) debug object case object when Twitter::Tweet if object.user == authenticated_user debug "skipping #{object} because it's from me" elsif (h = streaming_tweet_handler) && valid_tweet?(object) @current_tweet = object update_since_id(object) h.call(object) @current_tweet = nil end when Twitter::Streaming::DeletedTweet if (h = @handlers[:deleted]) h.call(object) end when Twitter::DirectMessage if object.sender == authenticated_user debug "skipping DM #{object} because it's from me" elsif (h = @handlers[:direct_messages]) @current_tweet = object update_since_id_dm(object) h.call(object) @current_tweet = nil end when Twitter::Streaming::Event if object.respond_to?(:source) && object.source == authenticated_user debug "skipping #{object} because it's from me" elsif object.name == :follow && (h = @handlers[:followed]) h.call(object.source) elsif object.name == :favorite && (h = @handlers[:favorited]) h.call(object.source, object.target_object) end when Twitter::Streaming::FriendList debug "got friend list" when Twitter::Streaming::StallWarning debug "***** STALL WARNING *****" end end |
#streaming_tweet_handler ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/chatterbot/streaming.rb', line 7 def streaming_tweet_handler usable_handlers = [:home_timeline, :search] name, block = @handlers.find { |k, v| usable_handlers.include?(k) } if block.nil? && ( block = @handlers[:replies] ) debug "No default handler, wrapping the replies handler" return Proc.new { |tweet| if tweet.text =~ /^@#{bot.screen_name}/i block.call(tweet) end } end block end |