diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d062067..2b885ac 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-03-28 18:37:09 -0400 using RuboCop version 0.80.1. +# on 2020-03-31 08:37:36 -0400 using RuboCop version 0.80.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -39,10 +39,11 @@ Style/AccessModifierDeclarations: Exclude: - 'lib/slack-ruby-bot/hooks/hook_support.rb' -# Offense count: 1 +# Offense count: 3 Style/DoubleNegation: Exclude: - 'lib/slack-ruby-bot/commands/base.rb' + - 'lib/slack-ruby-bot/config.rb' # Offense count: 1 # Cop supports --auto-correct. diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c5be6..243f757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ -### 0.13.1 (Next) +### 0.14.0 (Next) +* [#250](https://github.com/slack-ruby/slack-ruby-bot/pull/250): Added `config.allow_bot_messages`, defaults to `false` - [@dblock](https://github.com/dblock). * Your contribution here. ### 0.13.0 (2020/3/28) diff --git a/README.md b/README.md index c6c14f6..a9fdd3e 100644 --- a/README.md +++ b/README.md @@ -472,6 +472,16 @@ server.hooks.add(:hello, ->(client, data) { puts "Hello!" }) ``` +### Bot Message Protection + +By default bots do not respond to self or other bots. If you wish to change that behavior, set `allow_bot_messages` to `true`. + +```ruby +SlackRubyBot.configure do |config| + config.allow_bot_messages = true +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`. diff --git a/UPGRADING.md b/UPGRADING.md index 3d61769..fcd1e13 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,20 @@ Upgrading SlackRubyBot ====================== +### Upgrading to >= 0.14.0 + +#### Bot Messages Disabled + +By default bots will no longer respond to other bots. This caused confusing "I don't understand this command." errors when DMing the bot and rendering URLs that were being sent back as DMs. If you wish to restore the old behavior, set `allow_bot_messages` to `true`. + +```ruby +SlackRubyBot.configure do |config| + config.allow_bot_messages = true +end +``` + +See [#250](https://github.com/slack-ruby/slack-ruby-bot/pull/250) for more information. + ### Upgrading to >= 0.13.0 #### Minimum Ruby Version diff --git a/examples/minimal/Gemfile b/examples/minimal/Gemfile index 596fd80..3e65beb 100644 --- a/examples/minimal/Gemfile +++ b/examples/minimal/Gemfile @@ -2,5 +2,5 @@ source 'http://rubygems.org' -gem 'celluloid-io' +gem 'async-websocket', '~> 0.8.0' gem 'slack-ruby-bot', path: '../..' diff --git a/lib/slack-ruby-bot/config.rb b/lib/slack-ruby-bot/config.rb index b81e9c1..77ff29f 100644 --- a/lib/slack-ruby-bot/config.rb +++ b/lib/slack-ruby-bot/config.rb @@ -4,11 +4,15 @@ module SlackRubyBot module Config extend self - ATTRS = %i[token url aliases user user_id team team_id allow_message_loops send_gifs logger].freeze + ATTRS = %i[token url aliases user user_id team team_id allow_bot_messages allow_message_loops send_gifs logger].freeze attr_accessor(*ATTRS) + def allow_bot_messages? + !!allow_bot_messages + end + def allow_message_loops? - allow_message_loops + !!allow_message_loops end def send_gifs? diff --git a/lib/slack-ruby-bot/hooks/message.rb b/lib/slack-ruby-bot/hooks/message.rb index 16a1456..fb7362d 100644 --- a/lib/slack-ruby-bot/hooks/message.rb +++ b/lib/slack-ruby-bot/hooks/message.rb @@ -5,6 +5,7 @@ module Hooks class Message def call(client, data) return if message_to_self_not_allowed? && message_to_self?(client, data) + return if bot_message_not_allowed? && bot_message?(client, data) data.text = data.text.strip if data.text result = child_command_classes.detect { |d| d.invoke(client, data) } @@ -23,6 +24,14 @@ def message_to_self?(client, data) client.self && client.self.id == data.user end + def bot_message_not_allowed? + !SlackRubyBot::Config.allow_bot_messages? + end + + def bot_message?(_client, data) + data.subtype == 'bot_message' + end + # # All commands. # diff --git a/spec/slack-ruby-bot/commands/bot_messages_spec.rb b/spec/slack-ruby-bot/commands/bot_messages_spec.rb new file mode 100644 index 0000000..5a00b13 --- /dev/null +++ b/spec/slack-ruby-bot/commands/bot_messages_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +describe SlackRubyBot::App do + def app + SlackRubyBot::App.new + end + + let(:client) { subject.send(:client) } + let(:message_hook) { SlackRubyBot::Hooks::Message.new } + + context 'default' do + it 'does not respond to bot messages' do + expect(client).to_not receive(:message) + message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} hi", subtype: 'bot_message')) + end + end + context 'with allow_bot_messages=true' do + before do + SlackRubyBot::Config.allow_bot_messages = true + end + it 'responds to self' do + expect(client).to receive(:message) + message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} hi", subtype: 'bot_message')) + end + end +end diff --git a/spec/slack-ruby-bot/hooks/message_spec.rb b/spec/slack-ruby-bot/hooks/message_spec.rb index 8fea110..fe32f0a 100644 --- a/spec/slack-ruby-bot/hooks/message_spec.rb +++ b/spec/slack-ruby-bot/hooks/message_spec.rb @@ -50,6 +50,24 @@ end end end + describe '#bot_message_not_allowed?' do + context 'with allow_bot_messages set to true' do + before do + SlackRubyBot::Config.allow_bot_messages = true + end + it do + expect(message_hook.send(:bot_message_not_allowed?)).to be false + end + end + context 'with allow_bot_messages set to false' do + before do + SlackRubyBot::Config.allow_bot_messages = false + end + it do + expect(message_hook.send(:bot_message_not_allowed?)).to be true + end + end + end describe '#message_to_self?' do let(:client) { Hashie::Mash.new(self: { 'id' => 'U0K8CKKT1' }) } context 'with message to self' do