chatterbot 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d6048b3fe526c7dc7cdf2007b9f086ea9b84a41
4
- data.tar.gz: 8fcae3ba935e6d6703dad6dcdee8ebbea139d268
3
+ metadata.gz: bb0d7e5550a5496c33802ec97967e13ece9adc70
4
+ data.tar.gz: be65c23352fb603a2ff7694cdfeb5221f9ccfc9c
5
5
  SHA512:
6
- metadata.gz: c85326eb7f64cc05bfd670195a18855f4ee474a362226b0be3aa56ce91298542efe0afc30a74b1db12bee1bc5baa84226f8c349b899826f45d8a337e2a16609a
7
- data.tar.gz: 8415e97f10e4b6900e7f07a9c50616803fe71623de6c10743171bb2965a197f58eaa1c83bd71fbc0101fb74c6c848655d81fd98ddf9feb41b2e1f809bc7ff9a1
6
+ metadata.gz: 29c0d716108c6e654aab1aaf6f06daa46f5f934c0b090d2322ae0f5a12fa895067ff269b31e372b0b0f9c80d17510a76be50749590198613716ae6878a4937c9
7
+ data.tar.gz: d4377d0133715d08adf1a93f28b4f8e0453e06e27ca5b78f56eb5fc2311be711a42b77e6123b28005b110d7fbd462d5cdf01ab4066da6a3e3739d860bf342e21
@@ -37,6 +37,7 @@ module Chatterbot
37
37
  require "chatterbot/db"
38
38
  require "chatterbot/logging"
39
39
  require "chatterbot/blacklist"
40
+ require "chatterbot/whitelist"
40
41
  require "chatterbot/ui"
41
42
  require "chatterbot/client"
42
43
  require "chatterbot/search"
@@ -5,6 +5,7 @@ module Chatterbot
5
5
  class Bot
6
6
  include Utils
7
7
  include Blacklist
8
+ include Whitelist
8
9
  include Streaming
9
10
  include Config
10
11
  include Logging
@@ -214,14 +214,13 @@ module Chatterbot
214
214
  # figure out what config file to load based on the name of the bot
215
215
  def config_file
216
216
  dest = working_dir
217
- x = File.join(File.expand_path(dest), "#{botname}.yml")
217
+ File.join(File.expand_path(dest), "#{botname}.yml")
218
218
  end
219
219
 
220
220
  #
221
221
  # load in a config file
222
222
  def slurp_file(f)
223
223
  f = File.expand_path(f)
224
-
225
224
  tmp = {}
226
225
 
227
226
  if File.exist?(f)
@@ -225,6 +225,29 @@ module Chatterbot
225
225
  end
226
226
  end
227
227
 
228
+ #
229
+ # specify a bot-specific whitelist of users. accepts an array, or a
230
+ # comma-delimited string. when called, any subsequent calls to
231
+ # search or replies will only act upon these users.
232
+ #
233
+ # @param [Array, String] args list of usernames or Twitter::User objects
234
+ # @example
235
+ # whitelist "mean_user, private_user"
236
+ #
237
+ def whitelist(*args)
238
+ list = flatten_list_of_strings(args)
239
+
240
+ if list.nil? || list.empty?
241
+ bot.whitelist = []
242
+ else
243
+ bot.whitelist += list
244
+ end
245
+ end
246
+
247
+ def only_interact_with_followers
248
+ whitelist followers
249
+ end
250
+
228
251
  #
229
252
  # return a list of users following the bot. This passes directly
230
253
  # to the underlying Twitter API call
@@ -22,7 +22,9 @@ module Chatterbot
22
22
  @current_tweet = nil
23
23
  results.each { |s|
24
24
  update_since_id_reply(s)
25
- unless ! block_given? || on_blacklist?(s) || skip_me?(s)
25
+ if has_whitelist? && !on_whitelist?(s)
26
+ debug "skipping because user not on whitelist"
27
+ elsif block_given? && !on_blacklist?(s) && !skip_me?(s)
26
28
  @current_tweet = s
27
29
  yield s
28
30
  end
@@ -4,11 +4,21 @@ module Chatterbot
4
4
  # handle Twitter searches
5
5
  module Search
6
6
 
7
+ @skip_retweets = true
8
+
7
9
  #
8
10
  # modify a query string to exclude retweets from searches
9
11
  #
10
- def exclude_retweets(q)
11
- q.include?("include:retweets") ? q : q += " -include:retweets"
12
+ def exclude_retweets
13
+ @skip_retweets = true
14
+ end
15
+
16
+ def include_retweets
17
+ @skip_retweets = false
18
+ end
19
+
20
+ def skippable_retweet?(t)
21
+ @skip_retweets && t.retweeted_status?
12
22
  end
13
23
 
14
24
  # internal search code
@@ -25,16 +35,15 @@ module Chatterbot
25
35
  #
26
36
  queries.each { |query|
27
37
  debug "search: #{query} #{default_opts.merge(opts)}"
28
- result = client.search(
29
- exclude_retweets(query),
30
- default_opts.merge(opts)
31
- )
38
+ result = client.search( query, default_opts.merge(opts) )
32
39
  update_since_id(result)
33
40
 
34
41
  @current_tweet = nil
35
42
  result.each { |s|
36
43
  debug s.text
37
- if block_given? && !on_blacklist?(s) && !skip_me?(s)
44
+ if has_whitelist? && !on_whitelist?(s)
45
+ debug "skipping because user not on whitelist"
46
+ elsif block_given? && !on_blacklist?(s) && !skip_me?(s) && !skippable_retweet?(s)
38
47
  @current_tweet = s
39
48
  yield s
40
49
  end
@@ -31,7 +31,7 @@ module Chatterbot
31
31
  case object
32
32
  when Twitter::Tweet
33
33
  if object.user == authenticated_user
34
- puts "skipping #{object} because it's from me"
34
+ debug "skipping #{object} because it's from me"
35
35
  elsif streamer.tweet_handler && !on_blacklist?(object) && !skip_me?(object)
36
36
  @current_tweet = object
37
37
  streamer.tweet_handler.call object
@@ -49,7 +49,7 @@ module Chatterbot
49
49
  end
50
50
  when Twitter::Streaming::Event
51
51
  if object.respond_to?(:source) && object.source == authenticated_user
52
- puts "skipping #{object} because it's from me"
52
+ debug "skipping #{object} because it's from me"
53
53
  elsif object.name == :follow && streamer.follow_handler
54
54
  streamer.follow_handler.call(object.source)
55
55
  elsif object.name == :favorite && streamer.favorite_handler
@@ -1,3 +1,3 @@
1
1
  module Chatterbot
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -0,0 +1,32 @@
1
+ module Chatterbot
2
+
3
+ #
4
+ # methods for only tweeting to users on a specific list
5
+ module Whitelist
6
+ attr_accessor :whitelist
7
+
8
+ # A list of Twitter users that we can communicate with
9
+ def whitelist
10
+ @whitelist || []
11
+ end
12
+
13
+ def whitelist=(b)
14
+ @whitelist = b
15
+ @whitelist = @whitelist.flatten.collect { |e|
16
+ (e.is_a?(Twitter::User) ? from_user(e) : e).downcase
17
+ }
18
+ @whitelist
19
+ end
20
+
21
+ def has_whitelist?
22
+ !whitelist.empty?
23
+ end
24
+
25
+ #
26
+ # Is this tweet from a user on our whitelist?
27
+ def on_whitelist?(s)
28
+ search = (s.respond_to?(:user) ? from_user(s) : s).downcase
29
+ whitelist.any? { |b| search.include?(b.downcase) }
30
+ end
31
+ end
32
+ end
@@ -10,7 +10,7 @@ describe "Chatterbot::Blacklist" do
10
10
  describe "skip_me?" do
11
11
  before(:each) do
12
12
  @bot = test_bot
13
- allow(@bot).to receive(:exclude).and_return(["junk", "i hate bots", "foobar", "spam"])
13
+ @bot.exclude = ["junk", "i hate bots", "foobar", "spam"]
14
14
  end
15
15
 
16
16
  it "blocks tweets with phrases we don't want" do
@@ -34,7 +34,7 @@ describe "Chatterbot::Blacklist" do
34
34
  describe "on_blacklist?" do
35
35
  before(:each) do
36
36
  @bot = test_bot
37
- allow(@bot).to receive(:blacklist).and_return(["skippy", "blippy", "dippy"])
37
+ @bot.blacklist = ["skippy", "blippy", "dippy"]
38
38
  end
39
39
 
40
40
  it "blocks users we don't want" do
@@ -32,6 +32,33 @@ describe "Chatterbot::DSL" do
32
32
  end
33
33
  end
34
34
 
35
+ describe "whitelist" do
36
+ it "#whitelist passes along to bot object" do
37
+ expect(@bot).to receive(:whitelist=).with(["foo"])
38
+ whitelist ["foo"]
39
+ end
40
+
41
+ it "#whitelist turns single-string arg into an array" do
42
+ expect(@bot).to receive(:whitelist=).with(["foo"])
43
+ whitelist "foo"
44
+ end
45
+
46
+ it "#whitelist turns comma-delimited string arg into an array" do
47
+ expect(@bot).to receive(:whitelist=).with(["foo", "bar"])
48
+ whitelist "foo, bar"
49
+ end
50
+ end
51
+
52
+ describe "only_interact_with_followers" do
53
+ it "sets whitelist to be the bot's followers" do
54
+ f = fake_follower
55
+ allow(@bot).to receive(:followers).and_return([f])
56
+ expect(@bot).to receive(:whitelist=).with([f])
57
+ only_interact_with_followers
58
+ end
59
+ end
60
+
61
+
35
62
  [:no_update, :debug_mode, :verbose].each do |method|
36
63
  describe method.to_s do
37
64
  it "#{method.to_s} with nil passes along true to bot object" do
@@ -1,24 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Chatterbot::Search" do
4
- describe "exclude_retweets" do
5
- before(:each) do
6
- @bot = Chatterbot::Bot.new
7
- end
8
-
9
- it "should tack onto query" do
10
- expect(@bot.exclude_retweets("foo")).to eq("foo -include:retweets")
11
- end
12
-
13
- it "shouldn't tack onto query" do
14
- expect(@bot.exclude_retweets("foo -include:retweets")).to eq("foo -include:retweets")
15
- end
16
-
17
- it "shouldn't tack onto query" do
18
- expect(@bot.exclude_retweets("foo include:retweets")).to eq("foo include:retweets")
19
- end
20
- end
21
-
22
4
  it "calls search" do
23
5
  bot = Chatterbot::Bot.new
24
6
  expect(bot).to receive(:search)
@@ -40,8 +22,8 @@ describe "Chatterbot::Search" do
40
22
  bot = test_bot
41
23
 
42
24
  allow(bot).to receive(:client).and_return(fake_search(100, 1))
43
- expect(bot.client).to receive(:search).once.ordered.with("foo -include:retweets", {:result_type=>"recent"})
44
- expect(bot.client).to receive(:search).once.ordered.with("bar -include:retweets", {:result_type=>"recent"})
25
+ expect(bot.client).to receive(:search).once.ordered.with("foo", {:result_type=>"recent"})
26
+ expect(bot.client).to receive(:search).once.ordered.with("bar", {:result_type=>"recent"})
45
27
 
46
28
  bot.search(["foo", "bar"])
47
29
  end
@@ -50,7 +32,7 @@ describe "Chatterbot::Search" do
50
32
  bot = test_bot
51
33
 
52
34
  allow(bot).to receive(:client).and_return(fake_search(100, 1))
53
- expect(bot.client).to receive(:search).with("foo -include:retweets", {:lang => "en", :result_type=>"recent"})
35
+ expect(bot.client).to receive(:search).with("foo", {:lang => "en", :result_type=>"recent"})
54
36
 
55
37
  bot.search("foo", :lang => "en")
56
38
  end
@@ -59,7 +41,7 @@ describe "Chatterbot::Search" do
59
41
  bot = test_bot
60
42
 
61
43
  allow(bot).to receive(:client).and_return(fake_search(100, 1))
62
- expect(bot.client).to receive(:search).with("foo -include:retweets", {:result_type=>"recent"})
44
+ expect(bot.client).to receive(:search).with("foo", {:result_type=>"recent"})
63
45
 
64
46
  bot.search("foo")
65
47
  end
@@ -70,7 +52,7 @@ describe "Chatterbot::Search" do
70
52
  allow(bot).to receive(:since_id_reply).and_return(456)
71
53
 
72
54
  allow(bot).to receive(:client).and_return(fake_search(100, 1))
73
- expect(bot.client).to receive(:search).with("foo -include:retweets", {:since_id => 123, :result_type => "recent", :since_id_reply => 456})
55
+ expect(bot.client).to receive(:search).with("foo", {:since_id => 123, :result_type => "recent", :since_id_reply => 456})
74
56
 
75
57
  bot.search("foo")
76
58
  end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Chatterbot::Whitelist" do
4
+ describe "on_whitelist?" do
5
+ before(:each) do
6
+ @bot = test_bot
7
+ @bot.whitelist = [fake_user('user', 100), "skippy", "blippy", "dippy"]
8
+ end
9
+
10
+ it "includes users we want" do
11
+ expect(@bot.on_whitelist?("user")).to eq(true)
12
+ expect(@bot.on_whitelist?("skippy")).to eq(true)
13
+ end
14
+
15
+ it "excludes users we don't want" do
16
+ expect(@bot.on_whitelist?("flippy")).to eq(false)
17
+ end
18
+
19
+ it "isn't case-specific" do
20
+ expect(@bot.on_whitelist?("FLIPPY")).to eq(false)
21
+ expect(@bot.on_whitelist?("SKIPPY")).to eq(true)
22
+ end
23
+
24
+ it "works with result hashes" do
25
+ expect(@bot.on_whitelist?(Twitter::Tweet.new(:id => 1,
26
+ :user => {:id => 1, :screen_name => "skippy"}))).to eq(true)
27
+ expect(@bot.on_whitelist?(Twitter::Tweet.new(:id => 1,
28
+ :user => {:id => 1, :screen_name => "flippy"}))).to eq(false)
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatterbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Mitchell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-05 00:00:00.000000000 Z
11
+ date: 2014-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet
@@ -229,6 +229,7 @@ files:
229
229
  - lib/chatterbot/ui.rb
230
230
  - lib/chatterbot/utils.rb
231
231
  - lib/chatterbot/version.rb
232
+ - lib/chatterbot/whitelist.rb
232
233
  - spec/blacklist_spec.rb
233
234
  - spec/bot_spec.rb
234
235
  - spec/client_spec.rb
@@ -249,6 +250,7 @@ files:
249
250
  - spec/streaming_spec.rb
250
251
  - spec/tweet_spec.rb
251
252
  - spec/utils_spec.rb
253
+ - spec/whitelist_spec.rb
252
254
  - specs.watchr
253
255
  - templates/skeleton.txt
254
256
  homepage: http://github.com/muffinista/chatterbot