diff --git a/CHANGELOG.md b/CHANGELOG.md index ed3c1ca..5f9bf78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,31 +1,32 @@ ### 0.4.5 (Next) +* [#17](https://github.com/dblock/slack-ruby-client/issues/17): Do not respond to messages from self, override with `allow_message_loops` - [@dblock](https://github.com/dblock). * Your contribution here. ### 0.4.4 (10/5/2015) -* [#17](https://github.com/dblock/slack-ruby-bot/issues/17): Address bot by `name:` - [@dblock](https://githubcom/dblock). -* [#19](https://github.com/dblock/slack-ruby-bot/issues/19): Retry on `Faraday::Error::TimeoutError`, `TimeoutError` and `SSLError` - [@dblock](https://githubcom/dblock). -* [#3](https://github.com/dblock/slack-ruby-bot/issues/3): Retry on `migration_in_progress` errors during `rtm.start` - [@dblock](https://githubcom/dblock). -* Respond to direct messages without being addressed by name - [@dblock](https://githubcom/dblock). +* [#17](https://github.com/dblock/slack-ruby-bot/issues/17): Address bot by `name:` - [@dblock](https://github.com/dblock). +* [#19](https://github.com/dblock/slack-ruby-bot/issues/19): Retry on `Faraday::Error::TimeoutError`, `TimeoutError` and `SSLError` - [@dblock](https://github.com/dblock). +* [#3](https://github.com/dblock/slack-ruby-bot/issues/3): Retry on `migration_in_progress` errors during `rtm.start` - [@dblock](https://github.com/dblock). +* Respond to direct messages without being addressed by name - [@dblock](https://github.com/dblock). * Added `send_gif`, to allow GIFs to be sent without text - [@maclover7](https://github.com/maclover7). ### 0.4.3 (8/21/2015) -* [#13](https://github.com/dblock/slack-ruby-bot/issues/13): You can now address the bot by its Slack @id - [@dblock](https://githubcom/dblock). +* [#13](https://github.com/dblock/slack-ruby-bot/issues/13): You can now address the bot by its Slack @id - [@dblock](https://github.com/dblock). ### 0.4.2 (8/20/2015) -* [#12](https://github.com/dblock/slack-ruby-bot/issues/12): Added support for bot aliases - [@dblock](https://githubcom/dblock). +* [#12](https://github.com/dblock/slack-ruby-bot/issues/12): Added support for bot aliases - [@dblock](https://github.com/dblock). ### 0.4.1 (7/25/2015) -* Use a real client in `respond_with_slack_message` expectaions - [@dblock](https://githubcom/dblock). +* Use a real client in `respond_with_slack_message` expectaions - [@dblock](https://github.com/dblock). ### 0.4.0 (7/25/2015) -* Using [slack-ruby-client](https://github.com/dblock/slack-ruby-client) - [@dblock](https://githubcom/dblock). -* Use RealTime API to post messages - [@dblock](https://githubcom/dblock). +* Using [slack-ruby-client](https://github.com/dblock/slack-ruby-client) - [@dblock](https://github.com/dblock). +* Use RealTime API to post messages - [@dblock](https://github.com/dblock). ### 0.3.1 (7/21/2015) diff --git a/README.md b/README.md index 4fb18d5..eb9ea85 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,16 @@ module MyBot end ``` +### Message Loop Protection + +By default bots do not respond to their own messages. If you wish to change that behavior, set `allow_message_loops` to `true`. + +```ruby +SlackRubyBot.configure do |config| + config.allow_message_loops = true +end +``` + ### RSpec Shared Behaviors Slack-ruby-bot ships with a number of shared RSpec behaviors that can be used in your RSpec tests. Require 'slack-ruby-bot/rspec' in your `spec_helper.rb`. diff --git a/examples/minimal/Gemfile.lock b/examples/minimal/Gemfile.lock index 3863263..81640b0 100644 --- a/examples/minimal/Gemfile.lock +++ b/examples/minimal/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - slack-ruby-bot (0.4.4) + slack-ruby-bot (0.4.5) activesupport giphy (~> 2.0.2) hashie @@ -11,7 +11,7 @@ PATH GEM remote: http://rubygems.org/ specs: - activesupport (4.2.3) + activesupport (4.2.4) i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) @@ -40,7 +40,7 @@ GEM json (1.8.3) launchy (2.4.3) addressable (~> 2.3) - minitest (5.8.0) + minitest (5.8.1) multipart-post (2.0.0) oj (2.12.12) slack-ruby-client (0.2.1) @@ -60,3 +60,6 @@ PLATFORMS DEPENDENCIES slack-ruby-bot! + +BUNDLED WITH + 1.10.6 diff --git a/lib/slack-ruby-bot/config.rb b/lib/slack-ruby-bot/config.rb index f8ca182..9bcb37e 100644 --- a/lib/slack-ruby-bot/config.rb +++ b/lib/slack-ruby-bot/config.rb @@ -9,6 +9,7 @@ module Config attr_accessor :user_id attr_accessor :team attr_accessor :team_id + attr_accessor :allow_message_loops def names [user, aliases, "<@#{user_id.downcase}>", "<@#{user_id.downcase}>:", "#{user}:"].compact.flatten diff --git a/lib/slack-ruby-bot/hooks/message.rb b/lib/slack-ruby-bot/hooks/message.rb index 3f9b76c..82164de 100644 --- a/lib/slack-ruby-bot/hooks/message.rb +++ b/lib/slack-ruby-bot/hooks/message.rb @@ -5,6 +5,7 @@ module Message def message(client, data) data = Hashie::Mash.new(data) + return if !SlackRubyBot::Config.allow_message_loops && (client.self && client.self['id'] == data.user) data.text.strip! if data.text result = child_command_classes.detect { |d| d.invoke(client, data) } result ||= built_in_command_classes.detect { |d| d.invoke(client, data) } diff --git a/spec/slack-ruby-bot/commands/message_loop_spec.rb b/spec/slack-ruby-bot/commands/message_loop_spec.rb new file mode 100644 index 0000000..fe32807 --- /dev/null +++ b/spec/slack-ruby-bot/commands/message_loop_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe SlackRubyBot::App do + def app + SlackRubyBot::App.new + end + let(:client) { subject.send(:client) } + before do + allow(client).to receive(:self).and_return('id' => 'UDEADBEEF') + allow(Giphy).to receive(:random) + end + context 'default' do + it 'does not respond to self' do + expect(SlackRubyBot::Commands::Base).to_not receive(:send_client_message) + subject.send(:message, client, text: "#{SlackRubyBot.config.user} hi", channel: 'channel', user: 'UDEADBEEF') + end + end + context 'with allow_message_loops=true' do + before do + SlackRubyBot.configure do |config| + config.allow_message_loops = true + end + end + after do + SlackRubyBot.configure do |config| + config.allow_message_loops = nil + end + end + it 'responds to self' do + expect(SlackRubyBot::Commands::Base).to receive(:send_client_message) + subject.send(:message, client, text: "#{SlackRubyBot.config.user} hi", channel: 'channel', user: 'UDEADBEEF') + end + end +end