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/advanced.md CHANGED
@@ -60,8 +60,3 @@ end
60
60
  See `Chatterbot::Logging` for details on this.
61
61
 
62
62
 
63
- Streaming
64
- ---------
65
-
66
- Chatterbot has basic support for Twitter's Streaming API. You can read
67
- more about it [here](streaming.html)
data/docs/deploying.md CHANGED
@@ -45,9 +45,3 @@ end
45
45
  has processed -- if you don't have this call, you will get duplicate
46
46
  tweets.
47
47
 
48
- Streaming
49
- ---------
50
-
51
- Chatterbot has rough support for the Streaming API. If your bot can
52
- use it, it's a great option, because you get your data immediately.
53
- You can read more about setting up a bot to use [streaming](streaming.html).
data/docs/examples.md CHANGED
@@ -118,51 +118,3 @@ tweet result
118
118
  ```
119
119
 
120
120
 
121
- Streaming Bot
122
- --------------
123
-
124
-
125
- ```
126
- #!/usr/bin/env ruby
127
-
128
- ## This is a very simple working example of a bot using the streaming
129
- ## API. It's basically a copy of echoes_bot.rb, just using streaming.
130
-
131
- #
132
- # require the dsl lib to include all the methods you see below.
133
- #
134
- require 'rubygems'
135
- require 'chatterbot/dsl'
136
-
137
- consumer_secret 'foo'
138
- secret 'bar'
139
- token 'biz'
140
- consumer_key 'bam'
141
-
142
-
143
- puts "Loading echoes_bot.rb using the streaming API"
144
-
145
- exclude "http://", "https://"
146
-
147
- blacklist "mean_user, private_user"
148
-
149
- streaming do
150
- favorited do |user, tweet|
151
- reply "@#{user.screen_name} thanks for the fave!", tweet
152
- end
153
-
154
- followed do |user|
155
- tweet "@#{user.screen_name} just followed me!"
156
- follow user
157
- end
158
-
159
- replies do |tweet|
160
- favorite tweet
161
-
162
- puts "It's a tweet!"
163
- src = tweet.text.gsub(/@echoes_bot/, "#USER#")
164
- reply src, tweet
165
- end
166
- end
167
-
168
- ```
data/docs/index.md CHANGED
@@ -49,7 +49,6 @@ Features
49
49
  * Simple blacklistling system to limit your annoyance of users
50
50
  * Avoid your bot making a fool of itself by ignoring tweets with
51
51
  certain bad words
52
- * Basic Streaming API support
53
52
  * Optionally log tweets to the database for metrics and tracking purposes
54
53
 
55
54
 
@@ -62,9 +61,9 @@ calls. Any calls to the search/reply methods will return
62
61
  Copyright/License
63
62
  -----------------
64
63
 
65
- Copyright (c) 2014 Colin Mitchell. Chatterbot is distributed under the
66
- WTFPL license.
67
64
 
65
+ Copyright (c) 2018 Colin Mitchell. Chatterbot is distributed under the
66
+ MIT licence -- Please see LICENSE.txt for further details.
68
67
 
69
68
  http://muffinlabs.com
70
69
 
data/lib/chatterbot.rb CHANGED
@@ -45,7 +45,6 @@ module Chatterbot
45
45
  require "chatterbot/followers"
46
46
  require "chatterbot/helpers"
47
47
  require "chatterbot/utils"
48
- require "chatterbot/streaming"
49
48
 
50
49
  require "chatterbot/bot"
51
50
  end
@@ -5,7 +5,6 @@ module Chatterbot
5
5
  # primary Bot object, includes all the other modules
6
6
  class Bot
7
7
  include Utils
8
- include Streaming
9
8
  include Blocklist
10
9
  include Safelist
11
10
  include Config
@@ -26,9 +25,6 @@ module Chatterbot
26
25
  # handlers that can use the REST API
27
26
  HANDLER_CALLS = [:direct_messages, :home_timeline, :replies, :search]
28
27
 
29
- # handlers that require the Streaming API
30
- STREAMING_ONLY_HANDLERS = [:favorited, :followed, :deleted]
31
-
32
28
  #
33
29
  # Create a new bot. No options for now.
34
30
  def initialize(params={})
@@ -46,7 +42,7 @@ module Chatterbot
46
42
 
47
43
  at_exit do
48
44
  if !@handlers.empty? && @run_count <= 0 && skip_run? != true
49
- run_or_stream
45
+ run!
50
46
  end
51
47
 
52
48
  raise $! if $!
@@ -58,58 +54,6 @@ module Chatterbot
58
54
  @screen_name ||= client.settings.screen_name
59
55
  end
60
56
 
61
-
62
- #
63
- # determine the right API to use and run the bot
64
- #
65
- def run_or_stream
66
- @run_count += 1
67
- if streaming?
68
- stream!
69
- else
70
- run!
71
- end
72
- end
73
-
74
- #
75
- # run the bot with the Streaming API
76
- #
77
- def stream!
78
- before_run
79
-
80
- #
81
- # figure out how we want to call streaming client
82
- #
83
- if @handlers[:search]
84
- method = :filter
85
- args = streamify_search_options(@handlers[:search].opts)
86
- else
87
- method = :user
88
- args = {
89
- stall_warnings: "true"
90
- }
91
- end
92
-
93
- streaming_client.send(method, args) do |object|
94
- handle_streaming_object(object)
95
- end
96
- after_run
97
- end
98
-
99
- #
100
- # the REST API and Streaming API have a slightly different format.
101
- # tweak our search handler to switch from REST to Streaming
102
- #
103
- def streamify_search_options(opts)
104
- if opts.is_a?(String)
105
- { track: opts }
106
- elsif opts.is_a?(Array)
107
- { track: opts.join(", ") }
108
- else
109
- opts
110
- end
111
- end
112
-
113
57
  #
114
58
  # run the bot with the REST API
115
59
  #
@@ -137,24 +81,17 @@ module Chatterbot
137
81
  end
138
82
 
139
83
  def call_api_immediately?
140
- !streaming?
84
+ true
141
85
  end
142
86
 
143
87
  def register_handler(method, opts = nil, &block)
144
88
  # @todo raise error if method already defined
145
89
  @handlers[method] = Handler.new(opts, &block)
146
90
 
147
- if STREAMING_ONLY_HANDLERS.include?(method)
148
- puts "Forcing usage of Streaming API to support #{method} calls"
149
- self.streaming = true
150
- elsif call_api_immediately?
151
- @run_count += 1
152
- h = @handlers[method]
153
- self.send(method, *(h.opts)) do |obj|
154
- h.call(obj)
155
- end
91
+ h = @handlers[method]
92
+ self.send(method, *(h.opts)) do |obj|
93
+ h.call(obj)
156
94
  end
157
-
158
95
  end
159
96
  end
160
97
  end
@@ -6,7 +6,7 @@ module Chatterbot
6
6
  # routines for connecting to Twitter and validating the bot
7
7
  #
8
8
  module Client
9
- attr_accessor :screen_name, :client, :streaming_client
9
+ attr_accessor :screen_name, :client
10
10
 
11
11
  #
12
12
  # the main interface to the Twitter API
@@ -14,14 +14,7 @@ module Chatterbot
14
14
  def client
15
15
  @client ||= Twitter::REST::Client.new(client_params)
16
16
  end
17
-
18
- #
19
- # interace to the Streaming API
20
- #
21
- def streaming_client
22
- @streaming_client ||= Twitter::Streaming::Client.new(client_params)
23
- end
24
-
17
+
25
18
  #
26
19
  # return the currently authenticated User
27
20
  #
@@ -55,7 +55,6 @@ module Chatterbot
55
55
 
56
56
  attr_boolean :debug_mode, false
57
57
  attr_boolean :verbose, false
58
- attr_boolean :streaming, false
59
58
  attr_boolean :skip_run, false
60
59
  attr_boolean :only_interact_with_followers, false
61
60
 
@@ -70,49 +70,6 @@ module Chatterbot
70
70
  bot.register_handler(:direct_messages, block)
71
71
  end
72
72
 
73
-
74
- #
75
- # handle notifications of bot tweets favorited by other users.
76
- # Using this block will require usage of the Streaming API.
77
- #
78
- # @example
79
- # favorited do |tweet|
80
- # puts tweet.text # this is the actual tweeted text
81
- # reply "@#{user.screen_name} thanks for the fave!", tweet
82
- # end
83
- def favorited(&block)
84
- bot.register_handler(:favorited, block)
85
- end
86
-
87
- #
88
- # handle notifications that the bot has a new follower.
89
- # Using this block will require usage of the Streaming API.
90
- #
91
- # @example
92
- # followed do |user|
93
- # follow user
94
- # end
95
- def followed(&block)
96
- bot.register_handler(:followed, block)
97
- end
98
-
99
- #
100
- # handle notifications of tweets on the bot's timeline that were deleted.
101
- # Using this block will require usage of the Streaming API.
102
- def deleted(&block)
103
- bot.register_handler(:deleted, block)
104
- end
105
-
106
-
107
- #
108
- # enable or disable usage of the Streaming API
109
- #
110
- def use_streaming(s=nil)
111
- s = true if s.nil?
112
- bot.streaming = s
113
- end
114
-
115
-
116
73
  #
117
74
  # send a tweet
118
75
  #
@@ -18,8 +18,6 @@ module Chatterbot
18
18
  :timestamp => Time.now
19
19
  })
20
20
 
21
- puts opts.inspect
22
-
23
21
  src % opts
24
22
  end
25
23
  end
@@ -1,3 +1,3 @@
1
1
  module Chatterbot
2
- VERSION = "2.0.5"
2
+ VERSION = "2.1.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