Skip to content

Commit 2783fe2

Browse files
committed
Added config.allow_bot_messages, defaults to false
1 parent be820d3 commit 2783fe2

File tree

10 files changed

+90
-7
lines changed

10 files changed

+90
-7
lines changed

.rubocop_todo.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-03-28 18:37:09 -0400 using RuboCop version 0.80.1.
3+
# on 2020-03-31 08:37:36 -0400 using RuboCop version 0.80.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -39,10 +39,11 @@ Style/AccessModifierDeclarations:
3939
Exclude:
4040
- 'lib/slack-ruby-bot/hooks/hook_support.rb'
4141

42-
# Offense count: 1
42+
# Offense count: 3
4343
Style/DoubleNegation:
4444
Exclude:
4545
- 'lib/slack-ruby-bot/commands/base.rb'
46+
- 'lib/slack-ruby-bot/config.rb'
4647

4748
# Offense count: 1
4849
# Cop supports --auto-correct.

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
### 0.13.1 (Next)
1+
### 0.14.0 (Next)
22

3+
* [#250](https://github.com/slack-ruby/slack-ruby-bot/pull/250): Added `config.allow_bot_messages`, defaults to `false` - [@dblock](https://github.com/dblock).
34
* Your contribution here.
45

56
### 0.13.0 (2020/3/28)

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,16 @@ server.hooks.add(:hello, ->(client, data) { puts "Hello!" })
472472

473473
```
474474

475+
### Bot Message Protection
476+
477+
By default bots do not respond to self or other bots. If you wish to change that behavior, set `allow_bot_messages` to `true`.
478+
479+
```ruby
480+
SlackRubyBot.configure do |config|
481+
config.allow_bot_messages = true
482+
end
483+
```
484+
475485
### Message Loop Protection
476486

477487
By default bots do not respond to their own messages. If you wish to change that behavior, set `allow_message_loops` to `true`.

UPGRADING.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Upgrading SlackRubyBot
22
======================
33

4+
### Upgrading to >= 0.14.0
5+
6+
#### Bot Messages Disabled
7+
8+
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`.
9+
10+
```ruby
11+
SlackRubyBot.configure do |config|
12+
config.allow_bot_messages = true
13+
end
14+
```
15+
16+
See [#250](https://github.com/slack-ruby/slack-ruby-bot/pull/250) for more information.
17+
418
### Upgrading to >= 0.13.0
519

620
#### Minimum Ruby Version

examples/minimal/Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
source 'http://rubygems.org'
44

5-
gem 'celluloid-io'
5+
gem 'async-websocket', '~> 0.8.0'
66
gem 'slack-ruby-bot', path: '../..'

lib/slack-ruby-bot/config.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ module SlackRubyBot
44
module Config
55
extend self
66

7-
ATTRS = %i[token url aliases user user_id team team_id allow_message_loops send_gifs logger].freeze
7+
ATTRS = %i[token url aliases user user_id team team_id allow_bot_messages allow_message_loops send_gifs logger].freeze
88
attr_accessor(*ATTRS)
99

10+
def allow_bot_messages?
11+
!!allow_bot_messages
12+
end
13+
1014
def allow_message_loops?
11-
allow_message_loops
15+
!!allow_message_loops
1216
end
1317

1418
def send_gifs?

lib/slack-ruby-bot/hooks/message.rb

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Hooks
55
class Message
66
def call(client, data)
77
return if message_to_self_not_allowed? && message_to_self?(client, data)
8+
return if bot_message_not_allowed? && bot_message?(client, data)
89

910
data.text = data.text.strip if data.text
1011
result = child_command_classes.detect { |d| d.invoke(client, data) }
@@ -23,6 +24,14 @@ def message_to_self?(client, data)
2324
client.self && client.self.id == data.user
2425
end
2526

27+
def bot_message_not_allowed?
28+
!SlackRubyBot::Config.allow_bot_messages?
29+
end
30+
31+
def bot_message?(_client, data)
32+
data.subtype == 'bot_message'
33+
end
34+
2635
#
2736
# All commands.
2837
#

lib/slack-ruby-bot/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SlackRubyBot
4-
VERSION = '0.13.1'
4+
VERSION = '0.14.0'
55
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
describe SlackRubyBot::App do
4+
def app
5+
SlackRubyBot::App.new
6+
end
7+
8+
let(:client) { subject.send(:client) }
9+
let(:message_hook) { SlackRubyBot::Hooks::Message.new }
10+
11+
context 'default' do
12+
it 'does not respond to bot messages' do
13+
expect(client).to_not receive(:message)
14+
message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} hi", subtype: 'bot_message'))
15+
end
16+
end
17+
context 'with allow_bot_messages=true' do
18+
before do
19+
SlackRubyBot::Config.allow_bot_messages = true
20+
end
21+
it 'responds to self' do
22+
expect(client).to receive(:message)
23+
message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} hi", subtype: 'bot_message'))
24+
end
25+
end
26+
end

spec/slack-ruby-bot/hooks/message_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@
5050
end
5151
end
5252
end
53+
describe '#bot_message_not_allowed?' do
54+
context 'with allow_bot_messages set to true' do
55+
before do
56+
SlackRubyBot::Config.allow_bot_messages = true
57+
end
58+
it do
59+
expect(message_hook.send(:bot_message_not_allowed?)).to be false
60+
end
61+
end
62+
context 'with allow_bot_messages set to false' do
63+
before do
64+
SlackRubyBot::Config.allow_bot_messages = false
65+
end
66+
it do
67+
expect(message_hook.send(:bot_message_not_allowed?)).to be true
68+
end
69+
end
70+
end
5371
describe '#message_to_self?' do
5472
let(:client) { Hashie::Mash.new(self: { 'id' => 'U0K8CKKT1' }) }
5573
context 'with message to self' do

0 commit comments

Comments
 (0)