chatterbot 1.0.2 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,96 +0,0 @@
1
-
2
- class StreamingHandler
3
- attr_reader :tweet_handler
4
- attr_reader :favorite_handler
5
- attr_reader :dm_handler
6
- attr_reader :follow_handler
7
- attr_reader :delete_handler
8
- attr_reader :friends_handler
9
- attr_reader :search_filter
10
-
11
- attr_accessor :opts
12
-
13
- def initialize(bot, opts = {})
14
- @bot = bot
15
- @opts = opts
16
- end
17
-
18
- def bot
19
- @bot
20
- end
21
-
22
- def config
23
- bot.config
24
- end
25
-
26
- # filter, firehose, sample, user
27
- def endpoint
28
- opts[:endpoint] || :user
29
- end
30
-
31
- def search(query, opts = {}, &block)
32
- @search_filter = query
33
- @search_opts = opts
34
- apply_main_block(&block) if block_given?
35
- end
36
-
37
- def user(&block)
38
- apply_main_block(&block)
39
- end
40
- alias_method :replies, :user
41
- alias_method :timeline, :user
42
- alias_method :sample, :user
43
- alias_method :filter, :user
44
-
45
- def favorited(&block)
46
- @favorite_handler = block
47
- end
48
-
49
- def direct_message(&block)
50
- @dm_handler = block
51
- end
52
-
53
- def followed(&block)
54
- @follow_handler = block
55
- end
56
-
57
- def delete(&block)
58
- @delete_handler = block
59
- end
60
-
61
- def friends(&block)
62
- @friends_handler = block
63
- end
64
-
65
- def connection_params
66
- opts = {
67
- #:with => 'followings',
68
- #:replies => false,
69
- :stall_warnings => false
70
- }.merge(@opts)
71
-
72
- opts.delete(:endpoint)
73
-
74
- # convert true/false to strings
75
- opts.each { |k, v| opts[k] = v.to_s }
76
-
77
- if @search_filter
78
- opts[:track] = @search_filter
79
- end
80
-
81
- opts
82
- end
83
-
84
- def apply_main_block(&block)
85
- if !@tweet_handler.nil?
86
- warn "WARNING: when using streaming, you may only have one block of user/replies/timeline/sample/filter"
87
- raise RuntimeError, 'Unable to load bot'
88
- end
89
- @tweet_handler = block
90
- end
91
-
92
-
93
- def apply(block)
94
- instance_exec &block
95
- end
96
- end
@@ -1,32 +0,0 @@
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
@@ -1,116 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Chatterbot::Blacklist" do
4
- it "has a list of excluded phrases" do
5
- @bot = test_bot
6
- @bot.exclude = ["junk", "i hate bots", "foobar", "spam"]
7
- expect(@bot.skip_me?("did you know that i hate bots?")).to eq(true)
8
- end
9
-
10
- describe "skip_me?" do
11
- before(:each) do
12
- @bot = test_bot
13
- @bot.exclude = ["junk", "i hate bots", "foobar", "spam"]
14
- end
15
-
16
- it "blocks tweets with phrases we don't want" do
17
- expect(@bot.skip_me?("did you know that i hate bots?")).to eq(true)
18
- end
19
-
20
- it "isn't case-specific" do
21
- expect(@bot.skip_me?("DID YOU KNOW THAT I HATE BOTS?")).to eq(true)
22
- end
23
-
24
- it "allows users we do want" do
25
- expect(@bot.skip_me?("a tweet without any bad content")).to eq(false)
26
- end
27
-
28
- it "works with result hashes" do
29
- expect(@bot.skip_me?(Twitter::Tweet.new(:id => 1, :text => "did you know that i hate bots?"))).to eq(true)
30
- expect(@bot.skip_me?(Twitter::Tweet.new(:id => 1, :text => "a tweet without any bad content"))).to eq(false)
31
- end
32
- end
33
-
34
- describe "on_blacklist?" do
35
- before(:each) do
36
- @bot = test_bot
37
- @bot.blacklist = ["skippy", "blippy", "dippy"]
38
- end
39
-
40
- it "blocks users we don't want" do
41
- expect(@bot.on_blacklist?("skippy")).to eq(true)
42
- end
43
-
44
- it "allows users we do want" do
45
- expect(@bot.on_blacklist?("flippy")).to eq(false)
46
- end
47
-
48
- it "isn't case-specific" do
49
- expect(@bot.on_blacklist?("FLIPPY")).to eq(false)
50
- expect(@bot.on_blacklist?("SKIPPY")).to eq(true)
51
- end
52
-
53
- it "works with result hashes" do
54
- expect(@bot.on_blacklist?(Twitter::Tweet.new(:id => 1,
55
- :user => {:id => 1, :screen_name => "skippy"}))).to eq(true)
56
- expect(@bot.on_blacklist?(Twitter::Tweet.new(:id => 1,
57
- :user => {:id => 1, :screen_name => "flippy"}))).to eq(false)
58
- end
59
- end
60
-
61
- describe "on_global_blacklist?" do
62
- before(:each) do
63
- @bot = test_bot
64
- end
65
-
66
- it "doesn't freak out if no db" do
67
- expect(@bot).to receive(:has_db?).and_return(false)
68
- expect(@bot.on_global_blacklist?("foobar")).to eq(false)
69
- end
70
-
71
- it "collects name from the db if it exists" do
72
- allow(@bot).to receive(:has_db?).and_return(true)
73
- blacklist_table = double(Object)
74
- double_dataset = double(Object, {:count => 1})
75
- expect(blacklist_table).to receive(:where).
76
- with({ :user => "a"}).
77
- and_return( double_dataset )
78
-
79
-
80
- missing_dataset = double(Object, {:count => 0})
81
- expect(blacklist_table).to receive(:where).
82
- with({ :user => "b"}).
83
- and_return( missing_dataset )
84
-
85
- allow(@bot).to receive(:db).and_return({
86
- :blacklist => blacklist_table
87
- })
88
- expect(@bot.on_global_blacklist?("a")).to eq(true)
89
- expect(@bot.on_global_blacklist?("b")).to eq(false)
90
- end
91
- end
92
-
93
-
94
- describe "db interaction" do
95
- before(:each) do
96
- # @db_tmp = Tempfile.new("blacklist.db")
97
- @db_uri = "sqlite:/"
98
-
99
- @bot = Chatterbot::Bot.new
100
- @bot.config[:db_uri] = @db_uri
101
- @bot.db
102
- end
103
-
104
- describe "add_to_blacklist" do
105
- it "adds to the blacklist table" do
106
- @bot.add_to_blacklist("tester")
107
- end
108
-
109
- it "doesn't add a double entry" do
110
- @bot.add_to_blacklist("tester")
111
- @bot.add_to_blacklist("tester")
112
- end
113
- end
114
-
115
- end
116
- end
data/spec/db_spec.rb DELETED
@@ -1,53 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- #require 'sequel'
4
-
5
- describe "Chatterbot::DB" do
6
- before(:each) do
7
- @db_uri = "sqlite:/tmp/chatterbot.db"
8
- File.delete("/tmp/chatterbot.db") if File.exist?("/tmp/chatterbot.db")
9
- end
10
-
11
- context "prerequisites" do
12
- describe "get_connection" do
13
- it "should make sure sequel is actually installed" do
14
- allow_any_instance_of(Chatterbot::Bot).to receive(:has_sequel?) { false }
15
- @bot = Chatterbot::Bot.new
16
- @bot.config[:db_uri] = @db_uri
17
- expect(@bot).to receive(:display_db_config_notice)
18
- @bot.db
19
- end
20
- end
21
- end
22
-
23
- context "db interactions" do
24
- before(:each) do
25
- @bot = Chatterbot::Bot.new
26
- allow(@bot).to receive(:update_config_at_exit)
27
- @bot.config[:db_uri] = @db_uri
28
- end
29
-
30
- after(:each) do
31
- @bot.db.disconnect unless @bot.db.nil?
32
- end
33
-
34
- describe "table creation" do
35
- [:blacklist, :tweets, :config].each do |table|
36
- it "should create table #{table}" do
37
- @tmp_conn = @bot.db
38
- expect(@tmp_conn.tables.include?(table)).to eq(true)
39
- end
40
- end
41
- end
42
-
43
- describe "store_database_config" do
44
- it "doesn't fail" do
45
- @bot = Chatterbot::Bot.new
46
- @bot.config[:db_uri] = @db_uri
47
-
48
- @bot.db
49
- expect(@bot.store_database_config).to eq(true)
50
- end
51
- end
52
- end
53
- end
@@ -1,78 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Chatterbot::StreamingHandler" do
4
- let(:bot) { test_bot }
5
- let(:handler) { StreamingHandler.new(test_bot) }
6
-
7
- def apply_to_handler(&block)
8
- handler.apply block
9
- end
10
-
11
- describe "endpoint" do
12
- it "should default to :user" do
13
- expect(handler.endpoint).to eql(:user)
14
- end
15
-
16
- it "should pull from opts" do
17
- expect(StreamingHandler.new(test_bot, {:endpoint => :bar}).endpoint).to eql(:bar)
18
- end
19
- end
20
-
21
- it "should set search query" do
22
- apply_to_handler { search 'foo' }
23
- expect(handler.search_filter).to eql('foo')
24
- end
25
-
26
- it "should set tweet_handler" do
27
- apply_to_handler { replies {'bar'} }
28
- expect(handler.tweet_handler.call).to eql('bar')
29
- end
30
-
31
- it "should set favorited_handler" do
32
- apply_to_handler { favorited {'bar'} }
33
- expect(handler.favorite_handler.call).to eql('bar')
34
- end
35
-
36
- it "should set dm_handler" do
37
- apply_to_handler { direct_message {'bar'} }
38
- expect(handler.dm_handler.call).to eql('bar')
39
- end
40
-
41
- it "should set follow_handler" do
42
- apply_to_handler { followed {'bar'} }
43
- expect(handler.follow_handler.call).to eql('bar')
44
- end
45
-
46
- it "should set delete_handler" do
47
- apply_to_handler { delete {'bar'} }
48
- expect(handler.delete_handler.call).to eql('bar')
49
- end
50
-
51
- it "should set friends_handler" do
52
- apply_to_handler { friends {'bar'} }
53
- expect(handler.friends_handler.call).to eql('bar')
54
- end
55
-
56
- it "should pull config from bot" do
57
- expect(handler.bot).to receive(:config).and_return('config')
58
- expect(handler.config).to eql('config')
59
- end
60
-
61
- it "limits to one main block type" do
62
- apply_to_handler { replies {'bar'} }
63
- expect{
64
- apply_to_handler { replies {'bar'} }
65
- }.to raise_error(RuntimeError)
66
- end
67
-
68
- describe "connection_params" do
69
- it "has defaults" do
70
- expect(handler.connection_params[:stall_warnings]).to eql("false")
71
- end
72
-
73
- it "applies search filter" do
74
- apply_to_handler { search 'foo' }
75
- expect(handler.connection_params[:track]).to eql("foo")
76
- end
77
- end
78
- end