chatterbot 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/docs/streaming.md DELETED
@@ -1,98 +0,0 @@
1
- ---
2
- layout: page
3
- title: "Streaming API"
4
- category: doc
5
- ---
6
-
7
- Chatterbot has basic support for Twitter's [Streaming API](https://dev.twitter.com/streaming/overview). If
8
- you are an advanced developer, or want to create something very
9
- involved, it might make more sense to use a different library.
10
- However, if you do use Chatterbot, you can continue to use the DSL,
11
- and you get access to a bunch of helpful routines.
12
-
13
- Here's an example bot using the Streaming API:
14
-
15
- ```
16
- #!/usr/bin/env ruby
17
-
18
- ## This is a very simple working example of a bot using the streaming
19
- ## API. It's basically a copy of echoes_bot.rb, just using streaming.
20
-
21
- #
22
- # require the dsl lib to include all the methods you see below.
23
- #
24
- require 'rubygems'
25
- require 'chatterbot/dsl'
26
-
27
- consumer_secret 'foo'
28
- secret 'bar'
29
- token 'biz'
30
- consumer_key 'bam'
31
-
32
-
33
- puts "Loading echoes_bot.rb using the streaming API"
34
-
35
- exclude "http://", "https://"
36
-
37
- blacklist "mean_user, private_user"
38
-
39
- streaming do
40
- favorited do |user, tweet|
41
- reply "@#{user.screen_name} thanks for the fave!", tweet
42
- end
43
-
44
- followed do |user|
45
- tweet "@#{user.screen_name} just followed me!"
46
- follow user
47
- end
48
-
49
- replies do |tweet|
50
- favorite tweet
51
-
52
- puts "It's a tweet!"
53
- src = tweet.text.gsub(/@echoes_bot/, "#USER#")
54
- reply src, tweet
55
- end
56
- end
57
- ```
58
-
59
- By default, chatterbot will use the
60
- [user endpoint](https://dev.twitter.com/streaming/userstreams), which
61
- returns events for the bot -- mentions, follows, etc. If you want to
62
- perform a search, or use the sample endpoint, you will need to specify
63
- that:
64
-
65
-
66
- ```
67
- #
68
- # sample the twitter stream
69
- #
70
- streaming(endpoint:"sample") do
71
- sample do |tweet|
72
- puts tweet.text
73
- end
74
- end
75
- ```
76
-
77
-
78
- ```
79
- #
80
- # run a search
81
- #
82
- streaming(endpoint:"search") do
83
- search("Streaming API") do |tweet|
84
- puts tweet.text
85
- end
86
- end
87
- ```
88
-
89
- ```
90
- #
91
- # find geocoded tweets
92
- #
93
- streaming(endpoint: :filter, locations:"-180,-90,180,90") do
94
- user do |tweet|
95
- puts tweet.text
96
- end
97
- end
98
- ```
@@ -1,49 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- ## This is a very simple working example of a bot using the streaming
4
- ## API. It's basically a copy of echoes_bot.rb, just using streaming.
5
-
6
- #
7
- # require the dsl lib to include all the methods you see below.
8
- #
9
- require 'rubygems'
10
- require 'chatterbot/dsl'
11
-
12
- consumer_secret 'foo'
13
- secret 'bar'
14
- token 'biz'
15
- consumer_key 'bam'
16
-
17
-
18
- puts "Loading echoes_bot.rb using the streaming API"
19
-
20
- exclude "http://", "https://"
21
-
22
- blocklist "mean_user, private_user"
23
-
24
- use_streaming
25
-
26
- replies do |tweet|
27
- favorite tweet
28
-
29
- puts "It's a tweet!"
30
- src = tweet.text.gsub(/@echoes_bot/, "#USER#")
31
- reply src, tweet
32
- end
33
-
34
- direct_messages do |tweet|
35
- puts "well, here i am #{tweet.text}"
36
- puts tweet.inspect
37
- direct_message "got it", tweet.sender
38
- end
39
-
40
- favorited do |user, tweet|
41
- reply "@#{user.screen_name} thanks for the fave!", tweet
42
- end
43
-
44
- followed do |user|
45
- tweet "@#{user.screen_name} just followed me!"
46
- follow user
47
- end
48
-
49
-
@@ -1,72 +0,0 @@
1
- module Chatterbot
2
-
3
- #
4
- # simple twitter stream handler
5
- module Streaming
6
-
7
- def streaming_tweet_handler
8
- usable_handlers = [:home_timeline, :search]
9
- name, block = @handlers.find { |k, v| usable_handlers.include?(k) }
10
-
11
- if block.nil? && ( block = @handlers[:replies] )
12
- debug "No default handler, wrapping the replies handler"
13
- return Proc.new { |tweet|
14
- if tweet.text =~ /^@#{bot.screen_name}/i
15
- block.call(tweet)
16
- end
17
- }
18
- end
19
-
20
- block
21
- end
22
-
23
- #
24
- # Take the passed in object and call the appropriate bot method
25
- # for it
26
- # @param [Class] object a streaming API object
27
- #
28
- def handle_streaming_object(object)
29
- debug object
30
-
31
- case object
32
- when Twitter::Tweet
33
- if object.user == authenticated_user
34
- debug "skipping #{object} because it's from me"
35
- elsif (h = streaming_tweet_handler) && valid_tweet?(object)
36
- @current_tweet = object
37
- update_since_id(object)
38
-
39
- h.call(object)
40
- @current_tweet = nil
41
- end
42
- when Twitter::Streaming::DeletedTweet
43
- if (h = @handlers[:deleted])
44
- h.call(object)
45
- end
46
- when Twitter::DirectMessage
47
- if object.sender == authenticated_user
48
- debug "skipping DM #{object} because it's from me"
49
- elsif (h = @handlers[:direct_messages])
50
- @current_tweet = object
51
- update_since_id_dm(object)
52
- h.call(object)
53
- @current_tweet = nil
54
- end
55
- when Twitter::Streaming::Event
56
- if object.respond_to?(:source) && object.source == authenticated_user
57
- debug "skipping #{object} because it's from me"
58
- elsif object.name == :follow && (h = @handlers[:followed])
59
- h.call(object.source)
60
- elsif object.name == :favorite && (h = @handlers[:favorited])
61
- h.call(object.source, object.target_object)
62
- end
63
- when Twitter::Streaming::FriendList
64
- debug "got friend list"
65
- when Twitter::Streaming::StallWarning
66
- debug "***** STALL WARNING *****"
67
- end
68
- end
69
-
70
- end
71
- end
72
-
@@ -1,172 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Chatterbot::Streaming" do
4
- let(:bot) { test_bot }
5
- let(:user) { fake_user('user', 100) }
6
- let(:tweet) { fake_tweet(12345) }
7
-
8
-
9
- describe "streaming_tweet_handler" do
10
- before(:each) do
11
- bot.skip_run = true
12
- bot.streaming = true
13
- allow(bot.client).to receive(:user).and_return(user)
14
- end
15
-
16
- it "defaults to home_timeline" do
17
- bot.register_handler(:home_timeline) { @result = :home_timeline }
18
- bot.register_handler(:search, "foo") { @result = :search }
19
-
20
- bot.handle_streaming_object(tweet)
21
- expect(@result).to eql(:home_timeline)
22
- end
23
-
24
- it "will return search" do
25
- bot.register_handler(:search, "foo") { @result = :search }
26
-
27
- bot.handle_streaming_object(tweet)
28
- expect(@result).to eql(:search)
29
- end
30
- end
31
-
32
- describe "handle_streaming_object" do
33
- before(:each) {
34
- bot.skip_run = true
35
- bot.streaming = true
36
- allow(bot.client).to receive(:user).and_return(user)
37
- }
38
-
39
- describe "Twitter::Tweet" do
40
- it "works if no handler" do
41
- bot.handle_streaming_object(tweet)
42
- end
43
-
44
- context "with handler" do
45
- before(:each) do
46
- bot.register_handler(:home_timeline) { |t| @last_object = t }
47
- end
48
-
49
- it "ignores tweets from authenticated user" do
50
- expect(tweet).to receive(:user).and_return(user)
51
- bot.handle_streaming_object(tweet)
52
- expect(@last_object).to be_nil
53
- end
54
-
55
- it "passes to handler" do
56
- bot.handle_streaming_object(tweet)
57
- expect(@last_object).to eql(tweet)
58
- end
59
-
60
- it "ignores tweets from blocklist" do
61
- bot.blocklist = ['chatterbot']
62
- bot.handle_streaming_object(tweet)
63
- expect(@last_object).to be_nil
64
- end
65
-
66
- it "ignores tweets if skip_me is true" do
67
- bot.exclude = ['tweet']
68
- bot.handle_streaming_object(tweet)
69
- expect(@last_object).to be_nil
70
- end
71
- end
72
- end
73
-
74
- describe "Twitter::Streaming::DeletedTweet" do
75
- it "works if no handler" do
76
- obj = Twitter::Streaming::DeletedTweet.new(:id => 1)
77
- bot.handle_streaming_object(obj)
78
- end
79
-
80
- it "passes to handler" do
81
- bot.register_handler(:deleted) { |t| @last_object = t }
82
- obj = Twitter::Streaming::DeletedTweet.new(:id => 1)
83
- bot.handle_streaming_object(obj)
84
- expect(@last_object).to eql(obj)
85
- end
86
- end
87
-
88
- describe "Twitter::DirectMessage" do
89
- it "works if no handler" do
90
- obj = Twitter::DirectMessage.new(:id => 1)
91
- bot.handle_streaming_object(obj)
92
- end
93
-
94
- it "passes to handler" do
95
- bot.register_handler(:direct_messages) { |t| @last_object = t }
96
- obj = Twitter::DirectMessage.new(:id => 1)
97
- bot.handle_streaming_object(obj)
98
- expect(@last_object).to eql(obj)
99
- end
100
- end
101
-
102
- describe "Twitter::Streaming::Event" do
103
- it "ignores events generated by authenticated user" do
104
- event = Twitter::Streaming::Event.new(
105
- :event => :follow,
106
- :source => {:id => user.id, :name => 'name', :screen_name => 'name'},
107
- :target => {:id => user.id, :name => 'name', :screen_name => 'name'})
108
-
109
- bot.register_handler(:followed) { |t| @last_object = t }
110
-
111
- bot.handle_streaming_object(event)
112
- expect(@last_object).to be_nil
113
- end
114
-
115
- describe "follow" do
116
- before(:each) do
117
- @event = Twitter::Streaming::Event.new(
118
- :event => :follow,
119
- :source => {:id => 12345, :name => 'name', :screen_name => 'name'},
120
- :target => {:id => user.id, :name => 'name', :screen_name => 'name'})
121
-
122
- end
123
-
124
- it "works if no handler" do
125
- bot.handle_streaming_object(@event)
126
- expect(@last_object).to be_nil
127
- end
128
-
129
- it "passes to handler" do
130
- bot.register_handler(:followed) { |t| @last_object = t }
131
- bot.handle_streaming_object(@event)
132
- expect(@last_object.class).to be(Twitter::User)
133
- expect(@last_object.id).to be(12345)
134
- end
135
- end
136
-
137
- describe "favorite" do
138
- before(:each) do
139
- @event = Twitter::Streaming::Event.new(
140
- :event => :favorite,
141
- :source => {:id => 12345, :name => 'name', :screen_name => 'name'},
142
- :target => {:id => user.id, :name => 'name', :screen_name => 'name'},
143
- :target_object => {
144
- :from_user => "chatterbot",
145
- :id => 56789,
146
- :text => "I am a tweet!",
147
- :user => { :id => 1, :screen_name => "chatterbot" }
148
- })
149
- end
150
-
151
- it "works if no handler" do
152
- bot.handle_streaming_object(@event)
153
- expect(@last_object).to be_nil
154
- end
155
-
156
- it "passes to handler" do
157
- bot.register_handler(:favorited) { |_, t| @last_object = t }
158
- bot.handle_streaming_object(@event)
159
- expect(@last_object.class).to be(Twitter::Tweet)
160
- expect(@last_object.text).to eq("I am a tweet!")
161
- end
162
- end
163
- end
164
-
165
- describe "Twitter::Streaming::FriendList" do
166
- it "works if no handler" do
167
- obj = Twitter::Streaming::FriendList.new
168
- bot.handle_streaming_object(obj)
169
- end
170
- end
171
- end
172
- end