chatterbot 2.0.2 → 2.2.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.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +19 -0
  3. data/Gemfile +9 -17
  4. data/README.markdown +10 -23
  5. data/Rakefile +2 -18
  6. data/chatterbot.gemspec +17 -12
  7. data/docs/Gemfile +3 -0
  8. data/docs/README.md +3 -0
  9. data/docs/_config.yml +37 -0
  10. data/docs/_includes/footer.html +3 -0
  11. data/docs/_includes/header.html +4 -0
  12. data/docs/_includes/navigation.html +23 -0
  13. data/docs/_layouts/default.html +98 -0
  14. data/docs/_layouts/page.html +11 -0
  15. data/docs/_posts/.gitkeep +0 -0
  16. data/docs/_site/Gemfile +3 -0
  17. data/docs/_site/advanced.html +465 -0
  18. data/docs/_site/configuration.html +436 -0
  19. data/docs/_site/contributing.html +414 -0
  20. data/docs/_site/css/main.css +58 -0
  21. data/docs/_site/css/syntax.css +61 -0
  22. data/docs/_site/deploying.html +451 -0
  23. data/docs/_site/examples.html +559 -0
  24. data/docs/_site/features.html +496 -0
  25. data/docs/_site/images/01-create-application.png +0 -0
  26. data/docs/_site/images/02-application-permissions.png +0 -0
  27. data/docs/_site/images/03-mobile-number.png +0 -0
  28. data/docs/_site/images/04-access-token.png +0 -0
  29. data/docs/_site/index.html +461 -0
  30. data/docs/_site/javascripts/main.js +1 -0
  31. data/docs/_site/other-tools.html +419 -0
  32. data/docs/_site/params.json +1 -0
  33. data/docs/_site/rdoc.html +409 -0
  34. data/docs/_site/setup.html +491 -0
  35. data/docs/_site/stylesheets/pygment_trac.css +68 -0
  36. data/docs/_site/stylesheets/stylesheet.css +247 -0
  37. data/docs/_site/tut.html +402 -0
  38. data/docs/_site/twitter-docs.html +409 -0
  39. data/docs/_site/walkthrough.html +447 -0
  40. data/docs/advanced.md +62 -0
  41. data/docs/basics.md +12 -0
  42. data/docs/bin/jekyll-page +109 -0
  43. data/docs/configuration.md +32 -0
  44. data/docs/contributing.md +14 -0
  45. data/docs/css/main.css +58 -0
  46. data/docs/css/syntax.css +61 -0
  47. data/docs/deploying.md +47 -0
  48. data/docs/examples.md +120 -0
  49. data/docs/features.md +88 -0
  50. data/docs/images/01-create-application.png +0 -0
  51. data/docs/images/02-application-permissions.png +0 -0
  52. data/docs/images/03-mobile-number.png +0 -0
  53. data/docs/images/04-access-token.png +0 -0
  54. data/docs/index.md +69 -0
  55. data/docs/javascripts/main.js +1 -0
  56. data/docs/other-tools.md +17 -0
  57. data/docs/params.json +1 -0
  58. data/docs/rdoc.md +6 -0
  59. data/docs/setup.md +84 -0
  60. data/docs/stylesheets/pygment_trac.css +68 -0
  61. data/docs/stylesheets/stylesheet.css +247 -0
  62. data/docs/tips.md +22 -0
  63. data/docs/tut.md +6 -0
  64. data/docs/twitter-docs.md +6 -0
  65. data/docs/walkthrough.md +46 -0
  66. data/lib/chatterbot.rb +0 -1
  67. data/lib/chatterbot/bot.rb +7 -67
  68. data/lib/chatterbot/client.rb +2 -9
  69. data/lib/chatterbot/config.rb +1 -2
  70. data/lib/chatterbot/dsl.rb +20 -47
  71. data/lib/chatterbot/home_timeline.rb +1 -1
  72. data/lib/chatterbot/search.rb +26 -3
  73. data/lib/chatterbot/skeleton.rb +0 -2
  74. data/lib/chatterbot/tweet.rb +13 -3
  75. data/lib/chatterbot/ui.rb +0 -1
  76. data/lib/chatterbot/version.rb +1 -1
  77. data/spec/bot_spec.rb +1 -76
  78. data/spec/client_spec.rb +0 -3
  79. data/spec/config_manager_spec.rb +6 -5
  80. data/spec/config_spec.rb +4 -1
  81. data/spec/dsl_spec.rb +10 -35
  82. data/spec/fixtures/update_with_media.png +0 -0
  83. data/spec/search_spec.rb +40 -1
  84. data/spec/spec_helper.rb +1 -4
  85. data/spec/tweet_spec.rb +74 -34
  86. data/templates/skeleton.txt +0 -47
  87. metadata +81 -135
  88. data/.document +0 -5
  89. data/.travis.yml +0 -11
  90. data/examples/streaming_bot.rb +0 -48
  91. data/examples/tweet_logger.rb +0 -68
  92. data/lib/chatterbot/streaming.rb +0 -62
  93. data/spec/streaming_spec.rb +0 -172
data/lib/chatterbot/ui.rb CHANGED
@@ -13,7 +13,6 @@ module Chatterbot
13
13
  puts str.colorize(:red)
14
14
  end
15
15
 
16
- #:nocov:
17
16
  def green(str)
18
17
  puts str.colorize(:green)
19
18
  end
@@ -1,3 +1,3 @@
1
1
  module Chatterbot
2
- VERSION = "2.0.2"
2
+ VERSION = "2.2.0"
3
3
  end
data/spec/bot_spec.rb CHANGED
@@ -5,88 +5,13 @@ describe "Chatterbot::Bot" do
5
5
  @bot = Chatterbot::Bot.new
6
6
  end
7
7
 
8
- describe "Streaming API" do
9
- it "should call streaming_client.user" do
10
- expect(@bot.streaming_client).to receive(:user)
11
- @bot.stream!
12
- end
13
- end
14
8
 
15
9
  describe "REST API" do
16
10
  it "should work" do
17
11
  allow(@bot).to receive(:require_login).and_return(false)
18
12
  allow(@bot).to receive(:client).and_return(fake_home_timeline(3))
19
13
  @bot.register_handler(:home_timeline) {}
20
- #@bot.run!
21
- end
22
- end
23
-
24
- describe "run_or_stream" do
25
- it "should use streaming if specified" do
26
- expect(@bot).to receive(:stream!)
27
- @bot.streaming = true
28
- @bot.run_or_stream
29
- end
30
-
31
- it "should use streaming if required by handler" do
32
- expect(@bot).to receive(:stream!)
33
- @bot.register_handler(:deleted) {}
34
- @bot.run_or_stream
35
- end
36
-
37
- it "should use REST if specified" do
38
- expect(@bot).to receive(:run!)
39
- @bot.run_or_stream
40
- end
41
- end
42
-
43
-
44
-
45
- describe "stream!" do
46
- before(:each) do
47
- @bot.streaming = true
48
- @sc = double(Twitter::Client)
49
- expect(@bot).to receive(:streaming_client).and_return(@sc)
50
- end
51
-
52
- it "tweaks settings for searches" do
53
- @bot.register_handler(:search, "hello") {}
54
- expect(@sc).to receive(:filter)
55
- @bot.stream!
56
- end
57
-
58
- it "calls :user for non-searches" do
59
- @bot.register_handler(:home_timeline) {}
60
- expect(@sc).to receive(:user)
61
-
62
- @bot.stream!
14
+ @bot.run!
63
15
  end
64
16
  end
65
-
66
- describe "streamify_search_options" do
67
- it "works with string" do
68
- expect( @bot.streamify_search_options("hello there") ).
69
- to eql({track:"hello there"})
70
-
71
-
72
- expect( @bot.streamify_search_options("hello there, friend") ).
73
- to eql({track:"hello there, friend"})
74
- end
75
-
76
- it "works with array" do
77
- expect( @bot.streamify_search_options(["hello there"]) ).
78
- to eql({track:"hello there"})
79
-
80
- expect( @bot.streamify_search_options(["hello there", "friend"]) ).
81
- to eql({track:"hello there, friend"})
82
- end
83
-
84
- it "works with hash" do
85
- opts = {filter:"hello"}
86
- expect( @bot.streamify_search_options(opts) ).
87
- to eql(opts)
88
-
89
- end
90
- end
91
-
92
17
  end
data/spec/client_spec.rb CHANGED
@@ -8,9 +8,6 @@ describe "Chatterbot::Client" do
8
8
  it "should initialize client" do
9
9
  expect(@bot.client).to be_a(Twitter::REST::Client)
10
10
  end
11
- it "should initialize streaming client" do
12
- expect(@bot.streaming_client).to be_a(Twitter::Streaming::Client)
13
- end
14
11
 
15
12
  describe "reset!" do
16
13
  it "should reset a bunch of stuff" do
@@ -1,11 +1,16 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require 'tempfile'
3
+ require 'tmpdir'
4
+
3
5
 
4
6
  describe "Chatterbot::ConfigManager" do
5
7
  before(:each) do
6
- @tmp_config_dest = "/tmp/bot.yml"
8
+ tempdir = Dir.tmpdir
9
+
10
+ @tmp_config_dest = File.join(tempdir, 'bot.yml')
7
11
  @config = Chatterbot::ConfigManager.new(@tmp_config_dest, {:consumer_key => "bar"})
8
12
  end
13
+
9
14
  after(:each) do
10
15
  if File.exist?(@tmp_config_dest)
11
16
  File.unlink(@tmp_config_dest)
@@ -17,10 +22,6 @@ describe "Chatterbot::ConfigManager" do
17
22
  expect(@config.delete(:baz)).to be_nil
18
23
  end
19
24
 
20
- it "works with missing key" do
21
-
22
- end
23
-
24
25
  it "retains read-only data" do
25
26
  expect(@config[:consumer_key]).to eql("bar")
26
27
  expect(@config.delete(:consumer_key)).to be_nil
data/spec/config_spec.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require 'tempfile'
3
+ require 'tmpdir'
3
4
 
4
5
  describe "Chatterbot::Config" do
5
6
  before(:each) do
6
7
  @bot = Chatterbot::Bot.new
7
- @tmp_config_dest = "/tmp/bot.yml"
8
+ tempdir = Dir.tmpdir
9
+ @tmp_config_dest = File.join(tempdir, 'bot.yml')
8
10
  allow(@bot).to receive(:config_file).and_return(@tmp_config_dest)
9
11
  end
12
+
10
13
  after(:each) do
11
14
  if File.exist?(@tmp_config_dest)
12
15
  File.unlink(@tmp_config_dest)
data/spec/dsl_spec.rb CHANGED
@@ -100,7 +100,7 @@ describe "Chatterbot::DSL" do
100
100
 
101
101
  describe "search" do
102
102
  it "passes along to bot object" do
103
- allow(@bot).to receive(:run_or_stream)
103
+ allow(@bot).to receive(:run!)
104
104
  expect(@bot).to receive(:register_handler).with(:search, ["foo"])
105
105
  search("foo") {}
106
106
  end
@@ -118,39 +118,7 @@ describe "Chatterbot::DSL" do
118
118
  end
119
119
  end
120
120
 
121
- describe "favorited" do
122
- it "passes along to bot object" do
123
- expect(@bot).to receive(:register_handler).with(:favorited, instance_of(Proc))
124
-
125
- favorited {}
126
- end
127
- end
128
-
129
- describe "followed" do
130
- it "passes along to bot object" do
131
- expect(@bot).to receive(:register_handler).with(:followed, instance_of(Proc))
132
-
133
- followed {}
134
- end
135
- end
136
-
137
- describe "deleted" do
138
- it "passes along to bot object" do
139
- expect(@bot).to receive(:register_handler).with(:deleted, instance_of(Proc))
140
-
141
- deleted {}
142
- end
143
- end
144
-
145
121
 
146
- describe "streaming" do
147
- it "passes along to bot object" do
148
- expect(@bot).to receive(:streaming=).with(true)
149
- use_streaming
150
- end
151
- end
152
-
153
-
154
122
  it "#retweet passes along to bot object" do
155
123
  expect(@bot).to receive(:retweet).with(1234)
156
124
  retweet(1234)
@@ -187,10 +155,17 @@ describe "Chatterbot::DSL" do
187
155
  end
188
156
 
189
157
  it "#reply passes along to bot object" do
190
- expect(@bot).to receive(:reply).with("hello sailor!", { :source => "source "})
191
- reply "hello sailor!", { :source => "source "}
158
+ source = double(Twitter::Tweet)
159
+ expect(@bot).to receive(:reply).with("hello sailor!", source, {})
160
+ reply "hello sailor!", source
192
161
  end
193
162
 
163
+ it "#reply passes along to bot object with media" do
164
+ source = double(Twitter::Tweet)
165
+ expect(@bot).to receive(:reply).with("hello sailor!", source, {media:"/tmp/foo.jpg"})
166
+ reply "hello sailor!", source, media:"/tmp/foo.jpg"
167
+ end
168
+
194
169
  describe "#direct_message" do
195
170
  it "passes along to bot object" do
196
171
  expect(@bot).to receive(:direct_message).with("hello sailor!", nil)
Binary file
data/spec/search_spec.rb CHANGED
@@ -7,7 +7,6 @@ describe "Chatterbot::Search" do
7
7
  bot.search("foo")
8
8
  end
9
9
 
10
-
11
10
  it "calls update_since_id" do
12
11
  bot = test_bot
13
12
 
@@ -32,6 +31,46 @@ describe "Chatterbot::Search" do
32
31
  bot.search(["foo", "bar"])
33
32
  end
34
33
 
34
+ it "encloses search queries in quotes" do
35
+ bot = test_bot
36
+
37
+ allow(bot).to receive(:client).and_return(fake_search(100, 1))
38
+ expect(bot.client).to receive(:search).
39
+ with("\"foo bar baz\"", {
40
+ :result_type=>"recent",
41
+ :since_id => 1,
42
+ :since_id_reply => 1
43
+ })
44
+
45
+ bot.search("foo bar baz")
46
+ end
47
+
48
+ it "doesn't enclose search queries in quotes if not exact" do
49
+ bot = test_bot
50
+
51
+ allow(bot).to receive(:client).and_return(fake_search(100, 1))
52
+ expect(bot.client).to receive(:search).
53
+ with("foo bar baz", {
54
+ :result_type=>"recent",
55
+ :since_id => 1,
56
+ :since_id_reply => 1
57
+ })
58
+
59
+ bot.search("foo bar baz", exact:false)
60
+ end
61
+
62
+ it "passes along since_id" do
63
+ bot = test_bot
64
+ allow(bot).to receive(:since_id).and_return(123)
65
+ allow(bot).to receive(:since_id_reply).and_return(456)
66
+
67
+ allow(bot).to receive(:client).and_return(fake_search(100, 1))
68
+ expect(bot.client).to receive(:search).with("foo", {:since_id => 123, :result_type => "recent", :since_id_reply => 456})
69
+
70
+ bot.search("foo")
71
+ end
72
+
73
+
35
74
  it "accepts extra params" do
36
75
  bot = test_bot
37
76
 
data/spec/spec_helper.rb CHANGED
@@ -2,10 +2,7 @@
2
2
  require 'simplecov'
3
3
  SimpleCov.start
4
4
 
5
- require 'bundler/setup'
6
- Bundler.require
7
-
8
- require "twitter"
5
+ require "chatterbot"
9
6
 
10
7
  # Requires supporting files with custom matchers and macros, etc,
11
8
  # in ./support/ and its subdirectories.
data/spec/tweet_spec.rb CHANGED
@@ -11,29 +11,48 @@ describe "Chatterbot::Tweet" do
11
11
  @bot.tweet "tweet test!"
12
12
  end
13
13
 
14
- it "calls client.update with the right values" do
15
- bot = test_bot
14
+ context "when authenticated" do
15
+ let(:bot) { test_bot }
16
+ before(:each) do
17
+ expect(bot).to receive(:require_login).and_return(true)
18
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
19
+ end
20
+
21
+ it "calls client.update with the right values" do
22
+ allow(bot).to receive(:debug_mode?).and_return(false)
16
23
 
17
- expect(bot).to receive(:require_login).and_return(true)
18
- allow(bot).to receive(:client).and_return(double(Twitter::Client))
24
+ test_str = "test!"
25
+ expect(bot.client).to receive(:update).with(test_str, {})
26
+ bot.tweet test_str
27
+ end
19
28
 
20
- allow(bot).to receive(:debug_mode?).and_return(false)
29
+ it "calls client.update_with_media if media file is specified" do
30
+ allow(bot).to receive(:debug_mode?).and_return(false)
21
31
 
22
- test_str = "test!"
23
- expect(bot.client).to receive(:update).with(test_str, {})
24
- bot.tweet test_str
25
- end
32
+ test_str = "test!"
33
+ path = File.join(File.dirname(__FILE__), "fixtures", "update_with_media.png")
34
+ media = File.new(path)
35
+ expect(bot.client).to receive(:update_with_media).with(test_str, media, {})
36
+ bot.tweet test_str, media:media
37
+ end
26
38
 
27
- it "doesn't tweet when debug_mode? is set" do
28
- bot = test_bot
29
-
30
- expect(bot).to receive(:require_login).and_return(true)
31
- allow(bot).to receive(:client).and_return(double(Twitter::Client))
39
+ it "calls client.update_with_media if media file path is specified" do
40
+ allow(bot).to receive(:debug_mode?).and_return(false)
32
41
 
33
- allow(bot).to receive(:debug_mode?).and_return(true)
42
+ test_str = "test!"
43
+ path = File.join(File.dirname(__FILE__), "fixtures", "update_with_media.png")
34
44
 
35
- expect(bot.client).not_to receive(:update)
36
- bot.tweet "no tweet!"
45
+ expect(bot.client).to receive(:update_with_media).with(test_str, an_instance_of(File), {})
46
+ bot.tweet test_str, media:path
47
+ end
48
+
49
+
50
+ it "doesn't tweet when debug_mode? is set" do
51
+ allow(bot).to receive(:debug_mode?).and_return(true)
52
+
53
+ expect(bot.client).not_to receive(:update)
54
+ bot.tweet "no tweet!"
55
+ end
37
56
  end
38
57
  end
39
58
 
@@ -44,30 +63,51 @@ describe "Chatterbot::Tweet" do
44
63
  bot.reply "reply test!", fake_tweet(100)
45
64
  end
46
65
 
47
- it "calls client.update with the right values" do
48
- bot = test_bot
49
- expect(bot).to receive(:require_login).and_return(true)
50
- allow(bot).to receive(:client).and_return(double(Twitter::Client))
66
+ context "when authenticated" do
67
+ let(:bot) { test_bot }
68
+ before(:each) do
69
+ expect(bot).to receive(:require_login).and_return(true)
70
+ allow(bot).to receive(:client).and_return(double(Twitter::Client))
71
+ end
51
72
 
52
- allow(bot).to receive(:debug_mode?).and_return(false)
73
+
74
+ it "calls client.update with the right values" do
75
+ allow(bot).to receive(:debug_mode?).and_return(false)
53
76
 
54
- test_str = "test!"
77
+ test_str = "test!"
55
78
 
56
- expect(bot.client).to receive(:update).with(test_str, {:in_reply_to_status_id => 100})
57
- bot.reply test_str, fake_tweet(100, 100)
58
- end
79
+ expect(bot.client).to receive(:update).with(test_str, {:in_reply_to_status_id => 100})
80
+ bot.reply test_str, fake_tweet(100, 100)
81
+ end
59
82
 
83
+ it "calls client.update_with_media if media file is specified" do
84
+ allow(bot).to receive(:debug_mode?).and_return(false)
60
85
 
61
- it "doesn't reply when debug_mode? is set" do
62
- bot = test_bot
63
- expect(bot).to receive(:require_login).and_return(true)
64
- allow(bot).to receive(:client).and_return(double(Twitter::Client))
86
+ test_str = "test!"
87
+ path = File.join(File.dirname(__FILE__), "fixtures", "update_with_media.png")
88
+ media = File.new(path)
89
+
90
+ expect(bot.client).to receive(:update_with_media).with(test_str, media, {:in_reply_to_status_id => 100})
91
+ bot.reply test_str, fake_tweet(100, 100), media:media
92
+ end
65
93
 
66
- allow(bot).to receive(:debug_mode?).and_return(true)
94
+ it "calls client.update_with_media if media file path is specified" do
95
+ allow(bot).to receive(:debug_mode?).and_return(false)
67
96
 
68
- expect(bot.client).not_to receive(:update)
69
- bot.reply "no reply test!", fake_tweet(100)
97
+ test_str = "test!"
98
+ path = File.join(File.dirname(__FILE__), "fixtures", "update_with_media.png")
99
+
100
+ expect(bot.client).to receive(:update_with_media).with(test_str, an_instance_of(File), {:in_reply_to_status_id => 100})
101
+ bot.reply test_str, fake_tweet(100, 100), media:path
102
+ end
103
+
104
+ it "doesn't reply when debug_mode? is set" do
105
+ allow(bot).to receive(:debug_mode?).and_return(true)
106
+
107
+ expect(bot.client).not_to receive(:update_with_media)
108
+ bot.reply "no reply test!", fake_tweet(100)
109
+ end
70
110
  end
71
111
  end
72
-
112
+
73
113
  end
@@ -94,14 +94,6 @@ exclude bad_words
94
94
  # will be rejected
95
95
  only_interact_with_followers
96
96
 
97
- #
98
- # Specifying 'use_streaming' will cause Chatterbot to use Twitter's
99
- # Streaming API. Your bot will run constantly, listening for tweets.
100
- # Alternatively, you can run your bot as a cron/scheduled job. In that
101
- # case, do not use this line. Every time you run your bot, it will
102
- # execute once, and then exit.
103
- #
104
- use_streaming
105
97
 
106
98
  #
107
99
  # Here's the fun stuff!
@@ -148,43 +140,4 @@ use_streaming
148
140
  # home_timeline do |tweet|
149
141
  # puts tweet.inspect
150
142
  # end
151
-
152
- #
153
- # Use this block if you want to be notified about new followers of
154
- # your bot. You might do this to follow the user back.
155
- #
156
- # NOTE: This block only works with the Streaming API. If you use it,
157
- # chatterbot will assume you want to use streaming and will
158
- # automatically activate it for you.
159
- #
160
- # followed do |user|
161
- # puts user.inspect
162
- # end
163
-
164
- #
165
- # Use this block if you want to be notified when one of your tweets is
166
- # favorited. The object passed in will be a Twitter::Streaming::Event
167
- # @see http://www.rubydoc.info/gems/twitter/Twitter/Streaming/Event
168
- #
169
- # NOTE: This block only works with the Streaming API. If you use it,
170
- # chatterbot will assume you want to use streaming and will
171
- # automatically activate it for you.
172
- #
173
- # favorited do |event|
174
- # puts event.inspect
175
- # end
176
-
177
- #
178
- # Use this block if you want to be notified of deleted tweets from
179
- # your bots home timeline. The object passed in will be a
180
- # Twitter::Streaming::DeletedTweet
181
- # @see http://www.rubydoc.info/gems/twitter/Twitter/Streaming/DeletedTweet
182
- #
183
- # NOTE: This block only works with the Streaming API. If you use it,
184
- # chatterbot will assume you want to use streaming and will
185
- # automatically activate it for you.
186
- #
187
- #deleted do |tweet|
188
- #
189
- #end
190
143