Skip to content

Commit

Permalink
Do not respond to messages from self, override with allow_message_loo…
Browse files Browse the repository at this point in the history
…ps, closes slack-ruby#17.
  • Loading branch information
dblock committed Oct 16, 2015
1 parent 798d130 commit 3a54456
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
19 changes: 10 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
9 changes: 6 additions & 3 deletions examples/minimal/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -60,3 +60,6 @@ PLATFORMS

DEPENDENCIES
slack-ruby-bot!

BUNDLED WITH
1.10.6
1 change: 1 addition & 0 deletions lib/slack-ruby-bot/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 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 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) }
Expand Down
34 changes: 34 additions & 0 deletions spec/slack-ruby-bot/commands/message_loop_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3a54456

Please sign in to comment.