chatterbot 1.0.2 → 2.0.0.pre

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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -2
  3. data/LICENSE.txt +18 -9
  4. data/README.markdown +83 -65
  5. data/bin/chatterbot-register +0 -1
  6. data/chatterbot.gemspec +3 -10
  7. data/examples/class_bot.rb +0 -1
  8. data/examples/echoes_bot.rb +2 -2
  9. data/examples/search_bot.rb +1 -1
  10. data/examples/streaming_bot.rb +21 -15
  11. data/lib/chatterbot.rb +7 -12
  12. data/lib/chatterbot/blocklist.rb +61 -0
  13. data/lib/chatterbot/bot.rb +118 -13
  14. data/lib/chatterbot/client.rb +52 -20
  15. data/lib/chatterbot/config.rb +92 -215
  16. data/lib/chatterbot/config_manager.rb +49 -0
  17. data/lib/chatterbot/direct_messages.rb +46 -0
  18. data/lib/chatterbot/dsl.rb +157 -78
  19. data/lib/chatterbot/followers.rb +4 -0
  20. data/lib/chatterbot/handler.rb +29 -0
  21. data/lib/chatterbot/helpers.rb +14 -3
  22. data/lib/chatterbot/home_timeline.rb +5 -8
  23. data/lib/chatterbot/logging.rb +0 -17
  24. data/lib/chatterbot/profile.rb +0 -1
  25. data/lib/chatterbot/reply.rb +6 -11
  26. data/lib/chatterbot/retweet.rb +2 -6
  27. data/lib/chatterbot/safelist.rb +33 -0
  28. data/lib/chatterbot/search.rb +26 -16
  29. data/lib/chatterbot/skeleton.rb +7 -38
  30. data/lib/chatterbot/streaming.rb +26 -33
  31. data/lib/chatterbot/tweet.rb +0 -1
  32. data/lib/chatterbot/ui.rb +9 -2
  33. data/lib/chatterbot/utils.rb +13 -0
  34. data/lib/chatterbot/version.rb +1 -1
  35. data/spec/blocklist_spec.rb +170 -0
  36. data/spec/bot_spec.rb +83 -8
  37. data/spec/client_spec.rb +61 -7
  38. data/spec/config_manager_spec.rb +59 -0
  39. data/spec/config_spec.rb +33 -158
  40. data/spec/direct_messages_spec.rb +115 -0
  41. data/spec/dsl_spec.rb +95 -53
  42. data/spec/handler_spec.rb +27 -0
  43. data/spec/helpers_spec.rb +7 -11
  44. data/spec/home_timeline_spec.rb +42 -31
  45. data/spec/logging_spec.rb +0 -34
  46. data/spec/reply_spec.rb +10 -34
  47. data/spec/search_spec.rb +65 -6
  48. data/spec/spec_helper.rb +25 -1
  49. data/spec/streaming_spec.rb +56 -58
  50. data/spec/whitelist_spec.rb +10 -10
  51. data/specs.watchr +2 -4
  52. data/templates/skeleton.txt +148 -12
  53. metadata +20 -22
  54. data/bin/chatterbot-blacklist +0 -65
  55. data/bin/chatterbot-status +0 -55
  56. data/examples/loop_bot.rb +0 -44
  57. data/lib/chatterbot/blacklist.rb +0 -61
  58. data/lib/chatterbot/db.rb +0 -79
  59. data/lib/chatterbot/streaming_handler.rb +0 -96
  60. data/lib/chatterbot/whitelist.rb +0 -32
  61. data/spec/blacklist_spec.rb +0 -116
  62. data/spec/db_spec.rb +0 -53
  63. data/spec/streaming_handler_spec.rb +0 -78
data/spec/config_spec.rb CHANGED
@@ -14,12 +14,6 @@ describe "Chatterbot::Config" do
14
14
  end
15
15
 
16
16
  describe "loading" do
17
- it "overrides with incoming params" do
18
- expect(@bot).to receive(:global_config).and_return({:foo => :bar})
19
- tmp = @bot.load_config({:foo => :baz})
20
- expect(tmp[:foo]).to eq(:baz)
21
- end
22
-
23
17
  it "loads config when we need a variable" do
24
18
  expect(@bot).to receive(:load_config).and_return({:foo => :bar})
25
19
  @bot.config = nil
@@ -27,30 +21,19 @@ describe "Chatterbot::Config" do
27
21
  expect(@bot.config[:foo]).to eq(:bar)
28
22
  end
29
23
 
30
- it "loads both global config and local config" do
31
- expect(@bot).to receive(:global_config).and_return({:foo => :bar, :a => :b})
32
- expect(@bot).to receive(:bot_config).and_return({:foo => :baz, :custom => :value})
33
-
34
- @bot.config = nil
35
-
36
- expect(@bot.config[:foo]).to eq(:baz)
37
- expect(@bot.config[:a]).to eq(:b)
38
- expect(@bot.config[:custom]).to eq(:value)
39
- end
40
-
41
24
  context "environment variables" do
42
25
  before(:each) do
43
26
  ENV["chatterbot_consumer_key"] = "env_chatterbot_consumer_key"
44
27
  ENV["chatterbot_consumer_secret"] = "env_chatterbot_consumer_secret"
45
- ENV["chatterbot_token"] = "env_chatterbot_token"
46
- ENV["chatterbot_secret"] = "env_chatterbot_secret"
28
+ ENV["chatterbot_access_token"] = "env_chatterbot_token"
29
+ ENV["chatterbot_access_secret"] = "env_chatterbot_secret"
47
30
  end
48
31
 
49
32
  after(:each) do
50
33
  ENV.delete "chatterbot_consumer_key"
51
34
  ENV.delete "chatterbot_consumer_secret"
52
- ENV.delete "chatterbot_token"
53
- ENV.delete "chatterbot_secret"
35
+ ENV.delete "chatterbot_access_token"
36
+ ENV.delete "chatterbot_access_secret"
54
37
  end
55
38
 
56
39
  it "checks for environment variables" do
@@ -59,16 +42,16 @@ describe "Chatterbot::Config" do
59
42
 
60
43
  expect(@bot.config[:consumer_key]).to eq("env_chatterbot_consumer_key")
61
44
  expect(@bot.config[:consumer_secret]).to eq("env_chatterbot_consumer_secret")
62
- expect(@bot.config[:token]).to eq("env_chatterbot_token")
63
- expect(@bot.config[:secret]).to eq("env_chatterbot_secret")
45
+ expect(@bot.config[:access_token]).to eq("env_chatterbot_token")
46
+ expect(@bot.config[:access_token_secret]).to eq("env_chatterbot_secret")
64
47
  end
65
48
 
66
49
  it "works if env var is nil" do
67
50
  ENV["chatterbot_consumer_key"] = nil
68
51
  @bot.config = nil
69
- allow(@bot).to receive(:slurp_file).and_return({:chatterbot_consumer_key => "foo"})
52
+ allow(@bot).to receive(:slurp_file).and_return({:consumer_key => "foo"})
70
53
 
71
- expect(@bot.config[:chatterbot_consumer_key]).to eq("foo")
54
+ expect(@bot.config[:consumer_key]).to eq("foo")
72
55
  end
73
56
  end
74
57
 
@@ -77,7 +60,7 @@ describe "Chatterbot::Config" do
77
60
  end
78
61
 
79
62
  it "update_config? is false if this is a dry run" do
80
- @bot.config[:no_update] = true
63
+ @bot.no_update = true
81
64
  expect(@bot.update_config?).to eq(false)
82
65
  end
83
66
 
@@ -90,7 +73,7 @@ describe "Chatterbot::Config" do
90
73
  end
91
74
 
92
75
  it "checks for an auth_token" do
93
- expect(@bot).to receive(:load_config).and_return({:token => "123"})
76
+ expect(@bot).to receive(:load_config).and_return({:access_token => "123"})
94
77
  @bot.config = nil
95
78
 
96
79
  expect(@bot.needs_auth_token?).to eq(false)
@@ -118,49 +101,35 @@ describe "Chatterbot::Config" do
118
101
  describe "debug_mode=" do
119
102
  it "works" do
120
103
  @bot.debug_mode = true
121
- expect(@bot.config[:debug_mode]).to eq(true)
104
+ expect(@bot.debug_mode?).to eq(true)
122
105
  end
123
106
  end
124
107
 
125
108
  describe "no_update=" do
126
109
  it "works" do
127
110
  @bot.no_update = true
128
- expect(@bot.config[:no_update]).to eq(true)
111
+ expect(@bot.no_update?).to eq(true)
129
112
  end
130
113
  end
131
114
 
132
115
  describe "verbose=" do
133
116
  it "works" do
117
+ expect(@bot.verbose?).to eq(false)
134
118
  @bot.verbose = true
135
- expect(@bot.config[:verbose]).to eq(true)
136
- end
137
- end
138
-
139
- describe "reset_bot?" do
140
- it "works when reset_bot isn't set" do
141
- expect(@bot.reset_bot?).to eq(false)
142
- end
143
-
144
- it "works when reset_bot is set" do
145
- @bot.config[:reset_since_id] = false
146
- expect(@bot.reset_bot?).to eq(false)
147
-
148
- @bot.config[:reset_since_id] = true
149
- expect(@bot.reset_bot?).to eq(true)
119
+ expect(@bot.verbose?).to eq(true)
150
120
  end
151
121
  end
152
-
153
-
122
+
154
123
  describe "debug_mode?" do
155
124
  it "works when debug_mode isn't set" do
156
125
  expect(@bot.debug_mode?).to eq(false)
157
126
  end
158
127
 
159
128
  it "works when debug_mode is set" do
160
- @bot.config[:debug_mode] = false
129
+ @bot.debug_mode = false
161
130
  expect(@bot.debug_mode?).to eq(false)
162
131
 
163
- @bot.config[:debug_mode] = true
132
+ @bot.debug_mode = true
164
133
  expect(@bot.debug_mode?).to eq(true)
165
134
  end
166
135
  end
@@ -169,39 +138,31 @@ describe "Chatterbot::Config" do
169
138
  it "works" do
170
139
 
171
140
  @bot.since_id_reply = 123
172
- expect(@bot.config[:tmp_since_id_reply]).to eq(123)
141
+ expect(@bot.config[:since_id_reply]).to eq(123)
173
142
  end
174
143
  end
175
144
 
176
145
  describe "update_since_id_reply" do
177
146
  it "works with tweets" do
178
- @bot.config[:tmp_since_id_reply] = 100
147
+ @bot.config[:since_id_reply] = 100
179
148
 
180
149
  data = fake_tweet(1000, 1000)
181
150
  @bot.update_since_id_reply(data)
182
- expect(@bot.config[:tmp_since_id_reply]).to eq(1000)
183
- end
184
-
185
- it "doesn't work with searches" do
186
- data = fake_search(1000, 1).search
187
-
188
- @bot.config[:tmp_since_id_reply] = 100
189
- @bot.update_since_id_reply(data)
190
- expect(@bot.config[:tmp_since_id_reply]).to eq(100)
151
+ expect(@bot.config[:since_id_reply]).to eq(1000)
191
152
  end
192
153
 
193
154
  it "never rolls back" do
194
- @bot.config[:tmp_since_id_reply] = 100
155
+ @bot.config[:since_id_reply] = 100
195
156
  data = fake_tweet(50, 50)
196
157
  @bot.update_since_id(data)
197
- expect(@bot.config[:tmp_since_id_reply]).to eq(100)
158
+ expect(@bot.config[:since_id_reply]).to eq(100)
198
159
  end
199
160
  end
200
161
 
201
162
  describe "since_id=" do
202
163
  it "works" do
203
164
  @bot.since_id = 123
204
- expect(@bot.config[:tmp_since_id]).to eq(123)
165
+ expect(@bot.config[:since_id]).to eq(123)
205
166
  end
206
167
  end
207
168
 
@@ -209,42 +170,21 @@ describe "Chatterbot::Config" do
209
170
  it "works with searches" do
210
171
  data = fake_search(1000, 1).search
211
172
 
212
- @bot.config[:tmp_since_id] = 100
173
+ @bot.config[:since_id] = 100
213
174
  @bot.update_since_id(data)
214
- expect(@bot.config[:tmp_since_id]).to eq(1000)
215
- end
216
-
217
- it "works with SearchResults" do
218
- s = Twitter::SearchResults.new(
219
- double(Twitter::REST::Request,
220
- :client => nil,
221
- :verb => nil,
222
- :path => nil,
223
- :options => nil,
224
- :perform => {
225
- :search_metadata => {
226
- :max_id => 1000
227
- }
228
- }
229
- )
230
- )
231
-
232
-
233
- @bot.config[:tmp_since_id] = 100
234
- @bot.update_since_id(s)
235
- expect(@bot.config[:tmp_since_id]).to eq(1000)
175
+ expect(@bot.config[:since_id]).to eq(1000)
236
176
  end
237
177
 
238
178
  it "works with tweets" do
239
- @bot.config[:tmp_since_id] = 100
179
+ @bot.config[:since_id] = 100
240
180
 
241
181
  data = fake_tweet(1000, 1000)
242
182
  @bot.update_since_id(data)
243
- expect(@bot.config[:tmp_since_id]).to eq(1000)
183
+ expect(@bot.config[:since_id]).to eq(1000)
244
184
  end
245
185
 
246
186
  it "works with arrays" do
247
- @bot.config[:tmp_since_id] = 100
187
+ @bot.config[:since_id] = 100
248
188
 
249
189
  data = [
250
190
  fake_tweet(500, 1000),
@@ -253,60 +193,24 @@ describe "Chatterbot::Config" do
253
193
  ]
254
194
 
255
195
  @bot.update_since_id(data)
256
- expect(@bot.config[:tmp_since_id]).to eq(1000)
196
+ expect(@bot.config[:since_id]).to eq(1000)
257
197
  end
258
198
 
259
199
  it "works with an id" do
260
- @bot.config[:tmp_since_id] = 100
200
+ @bot.config[:since_id] = 100
261
201
 
262
202
  @bot.update_since_id(1000)
263
- expect(@bot.config[:tmp_since_id]).to eq(1000)
203
+ expect(@bot.config[:since_id]).to eq(1000)
264
204
  end
265
205
 
266
206
  it "never rolls back" do
267
- @bot.config[:tmp_since_id] = 100
207
+ @bot.config[:since_id] = 100
268
208
  data = fake_tweet(50, 50)
269
209
  @bot.update_since_id(data)
270
- expect(@bot.config[:tmp_since_id]).to eq(100)
210
+ expect(@bot.config[:since_id]).to eq(100)
271
211
  end
272
212
  end
273
213
 
274
-
275
- describe "update_config" do
276
- it "doesn't update the config if update_config? is false" do
277
- expect(@bot).to receive(:update_config?).and_return(false)
278
- expect(@bot).not_to receive(:has_db?)
279
- @bot.update_config
280
- end
281
-
282
- it "doesn't update keys from the global config" do
283
- allow(@bot).to receive(:global_config).and_return({:foo => :bar, :a => :b})
284
- allow(@bot).to receive(:bot_config).and_return({:foo => :bar, :custom => :value})
285
-
286
- @bot.config = nil
287
-
288
- expect(@bot.config_to_save).to eq({ :custom => :value })
289
- end
290
-
291
- it "does update keys from the global config if they've been customized" do
292
- allow(@bot).to receive(:global_config).and_return({:foo => :bar, :a => :b})
293
- allow(@bot).to receive(:bot_config).and_return({:foo => :baz, :custom => :value})
294
-
295
- @bot.config = nil
296
-
297
- expect(@bot.config_to_save).to eq({ :foo => :baz, :custom => :value })
298
- end
299
-
300
- it "updates since_id" do
301
- @bot.config[:tmp_since_id] = 100
302
- expect(@bot.config_to_save[:since_id]).to eq(100)
303
- end
304
-
305
- it "updates since_id_reply" do
306
- @bot.config[:tmp_since_id_reply] = 100
307
- expect(@bot.config_to_save[:since_id_reply]).to eq(100)
308
- end
309
- end
310
214
 
311
215
 
312
216
  describe "global config files" do
@@ -368,35 +272,6 @@ describe "Chatterbot::Config" do
368
272
 
369
273
  expect(@bot.slurp_file(src.path)).to eq({ :since_id => 0 })
370
274
  end
371
-
372
- it "doesn't store local file if we can store to db instead" do
373
- expect(@bot).to receive(:has_db?).and_return(true)
374
- expect(@bot).to receive(:store_database_config)
375
- @bot.update_config
376
- end
377
-
378
- it "stores local file if no db" do
379
- expect(@bot).to receive(:has_db?).and_return(false)
380
- expect(@bot).not_to receive(:store_database_config)
381
- expect(@bot).to receive(:store_local_config)
382
- @bot.update_config
383
- end
384
275
  end
385
276
 
386
- describe "store_local_config" do
387
- before(:each) do
388
- tmp = {:x => 123, :foo => :bar}
389
-
390
- @src = Tempfile.new("config")
391
-
392
- allow(@bot).to receive(:config_file).and_return(@src.path)
393
- allow(@bot).to receive(:config_to_save).and_return(tmp)
394
- end
395
-
396
- it "should work" do
397
- @bot.store_local_config
398
- expect(@bot.slurp_file(@src.path)).to eq({ :x => 123, :foo => :bar })
399
- end
400
- end
401
-
402
277
  end
@@ -0,0 +1,115 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chatterbot::DirectMessages" do
4
+ let(:bot) { test_bot }
5
+
6
+ describe "direct_message" do
7
+ it "calls require_login" do
8
+ expect(bot).to receive(:require_login).and_return(false)
9
+ bot.direct_message("hello")
10
+ end
11
+
12
+ context "when authenticated" do
13
+ before(:each) do
14
+ expect(bot).to receive(:require_login).and_return(true)
15
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
16
+ allow(bot).to receive(:debug_mode?).and_return(false)
17
+ end
18
+
19
+ it "calls create_direct_message" do
20
+ test_str = "test!"
21
+ test_user = fake_user("DM Enthusiast")
22
+
23
+ expect(bot.client).to receive(:create_direct_message).with(test_user, test_str)
24
+ bot.direct_message(test_str, test_user)
25
+ end
26
+
27
+ it "grabs user from most recent tweet object if needed" do
28
+ test_str = "test!"
29
+ test_user = fake_user("DM Enthusiast", 1000)
30
+
31
+ x = {
32
+ :from_user => "chatterbot",
33
+ :id => 100,
34
+ :text => "I am a tweet",
35
+ :user => { :id => 1000, :screen_name => "DM Enthusiast" }
36
+ }
37
+
38
+ t = Twitter::Tweet.new(x)
39
+ bot.instance_variable_set(:@current_tweet, t)
40
+
41
+ expect(bot.client).to receive(:create_direct_message).with(test_user, test_str)
42
+ bot.direct_message(test_str)
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "direct_messages" do
48
+ it "calls require_login" do
49
+ expect(bot).to receive(:require_login).and_return(false)
50
+ bot.direct_messages
51
+ end
52
+
53
+ it "updates since_id_dm when complete" do
54
+ expect(bot).to receive(:require_login).and_return(true)
55
+ results = fake_direct_messages(1, 1000)
56
+
57
+ allow(bot).to receive(:client).and_return(results)
58
+
59
+ bot.direct_messages do
60
+ end
61
+
62
+ expect(bot.config[:since_id_dm]).to eq(1000)
63
+ end
64
+
65
+ describe "results handling" do
66
+ before(:each) do
67
+ expect(bot).to receive(:require_login).and_return(true)
68
+ allow(bot).to receive(:client).and_return(fake_direct_messages(3))
69
+ end
70
+
71
+ it "iterates results" do
72
+
73
+ expect(bot).to receive(:update_since_id_dm).exactly(3).times
74
+
75
+ indexes = []
76
+ bot.direct_messages do |x|
77
+ indexes << x.id
78
+ end
79
+
80
+ expect(indexes).to eq([1,2,3])
81
+ end
82
+
83
+ it "checks blocklist" do
84
+ allow(bot).to receive(:on_blocklist?).and_return(true, false, false)
85
+ indexes = []
86
+ bot.direct_messages do |x|
87
+ indexes << x.id
88
+ end
89
+
90
+ expect(indexes).to eq([2,3])
91
+ end
92
+
93
+ it "checks safelist" do
94
+ allow(bot).to receive(:has_safelist?).and_return(true)
95
+ allow(bot).to receive(:on_safelist?).and_return(false, true, false)
96
+
97
+ indexes = []
98
+ bot.direct_messages do |x|
99
+ indexes << x.id
100
+ end
101
+
102
+ expect(indexes).to eq([2])
103
+ end
104
+
105
+
106
+ it "passes along since_id_dm" do
107
+ allow(bot).to receive(:since_id_dm).and_return(123)
108
+
109
+ expect(bot.client).to receive(:direct_messages_received).with({:since_id => 123, :count => 200})
110
+
111
+ bot.direct_messages
112
+ end
113
+ end
114
+ end
115
+ end