Skip to content

Commit

Permalink
Added config.allow_bot_messages, defaults to false
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Mar 31, 2020
1 parent be820d3 commit 13fccdb
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
14 changes: 14 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

source 'http://rubygems.org'

gem 'celluloid-io'
gem 'async-websocket', '~> 0.8.0'
gem 'slack-ruby-bot', path: '../..'
8 changes: 6 additions & 2 deletions lib/slack-ruby-bot/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
9 changes: 9 additions & 0 deletions lib/slack-ruby-bot/hooks/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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.
#
Expand Down
26 changes: 26 additions & 0 deletions spec/slack-ruby-bot/commands/bot_messages_spec.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions spec/slack-ruby-bot/hooks/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 13fccdb

Please sign in to comment.