Module: Chatterbot::Streaming

Included in:
Bot
Defined in:
lib/chatterbot/streaming.rb

Overview

simple twitter stream handler

Instance Method Summary collapse

Instance Method Details

#authenticated_userObject


7
8
9
# File 'lib/chatterbot/streaming.rb', line 7

def authenticated_user
  @user ||= client.user
end

#do_streaming(streamer) {|Twitter::Tweet, Twitter::Streaming:| ... } ⇒ Object

Streams messages for a single user, optionally including an additional search/etc

Parameters:

  • opts (Hash)

    options

  • options (Hash)

    a customizable set of options

Yields:

  • (Twitter::Tweet, Twitter::Streaming:)

    Twitter::Tweet, Twitter::Streaming:


21
22
23
24
25
26
# File 'lib/chatterbot/streaming.rb', line 21

def do_streaming(streamer)
  debug "streaming twitter client"
  streaming_client.send(streamer.endpoint, streamer.connection_params) do |object|
    handle_streaming_object(object, streamer)
  end    
end

#handle_streaming_object(object, streamer) ⇒ Object


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
# File 'lib/chatterbot/streaming.rb', line 28

def handle_streaming_object(object, streamer)
  debug object

  case object
  when Twitter::Tweet
    if object.user == authenticated_user
      debug "skipping #{object} because it's from me"
    elsif streamer.tweet_handler && !on_blacklist?(object) && !skip_me?(object)
      @current_tweet = object
      streamer.tweet_handler.call object
      @current_tweet = nil
    end
  when Twitter::Streaming::DeletedTweet
    if streamer.delete_handler
      streamer.delete_handler.call(object)
    end
  when Twitter::DirectMessage
    if streamer.dm_handler # && !on_blacklist?(object) && !skip_me?(object)
      @current_tweet = object
      streamer.dm_handler.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 && streamer.follow_handler
      streamer.follow_handler.call(object.source)
    elsif object.name == :favorite && streamer.favorite_handler
      streamer.favorite_handler.call(object.source, object.target_object)
    end
  when Twitter::Streaming::FriendList
    debug "got friend list"
    if streamer.friends_handler
      streamer.friends_handler.call(object)
    end
  end
end