Skip to content

Commit

Permalink
Added SlackRubyBot::Config.send_gifs, set via code or ENV['SLACK_RUBY…
Browse files Browse the repository at this point in the history
…_BOT_SEND_GIFS'].

Closes dblock/slack-gamebot#40.
  • Loading branch information
dblock committed Dec 7, 2015
1 parent ba13b6e commit 0b9bba3
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.5.0 (Next)

* [#40](https://github.com/dblock/slack-gamebot/issues/40): Disable animated GIFs via `SlackRubyBot::Config.send_gifs` or ENV['SLACK_RUBY_BOT_SEND_GIFS'] - [@dblock](https://github.com/dblock).
* `SlackRubyBot::Server` supports `restart!` with retry - [@dblock](https://github.com/dblock).
* `SlackRubyBot::Server` publicly supports `auth!`, `start!` and `start_async` that make up a `run` loop - [@dblock](https://github.com/dblock).
* Extracted `SlackRubyBot::Server` from `SlackRubyBot::App` - [@dblock](https://github.com/dblock).
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ A typical production Slack bot is a combination of a vanilla web server and a we

The following examples of bots based on slack-ruby-bot are listed in growing order of complexity.

* [slack-bot-on-rails](https://github.com/dblock/slack-bot-on-rails): A bot running on Rails and using React to display Slack messages on a website.
* [slack-bot-on-rails](https://github.com/dblock/slack-bot-on-rails): A bot running on Rails and using React to display Slack messages on a website.
* [slack-mathbot](https://github.com/dblock/slack-mathbot): Slack integration with math.
* [slack-google-bot](https://github.com/dblock/slack-google-bot): A Slack bot that searches Google, including CSE.
* [slack-aws](https://github.com/dblock/slack-aws): Slack integration with Amazon Web Services.
Expand Down Expand Up @@ -201,6 +201,16 @@ module MyBot
end
```

### Disable Animated GIFs

By default bots send animated GIFs in default commands and errors. To disable animated GIFs set `send_gifs` or `ENV['SLACK_RUBY_BOT_SEND_GIFS']` to `false`.

```ruby
SlackRubyBot.configure do |config|
config.send_gifs = false
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
4 changes: 2 additions & 2 deletions lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ def self.finalize_routes!

def self.get_gif_and_send(options = {})
options = options.dup
keywords = options.delete(:keywords)
gif = begin
keywords = options.delete(:keywords)
Giphy.random(keywords)
rescue StandardError => e
logger.warn "Giphy.random: #{e.message}"
nil
end
end if SlackRubyBot::Config.send_gifs?
client = options.delete(:client)
text = options.delete(:text)
text = [text, gif && gif.image_url.to_s].compact.join("\n")
Expand Down
26 changes: 26 additions & 0 deletions lib/slack-ruby-bot/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,31 @@ module Config
attr_accessor :team
attr_accessor :team_id
attr_accessor :allow_message_loops
attr_accessor :send_gifs

def allow_message_loops?
allow_message_loops
end

def send_gifs?
v = boolean_from_env('SLACK_RUBY_BOT_SEND_GIFS')
v.nil? ? (send_gifs.nil? || send_gifs) : v
end

private

def boolean_from_env(key)
value = ENV[key]
case value
when nil
nil
when 0, 'false', 'no'
false
when 1, 'true', 'yes'
true
else
fail ArgumentError, "Invalid value for #{key}: #{value}."
end
end
end
end
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/hooks/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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)
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
28 changes: 28 additions & 0 deletions spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,32 @@ def app
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message')
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user')
end
context 'send_gifs' do
context 'set to false' do
before do
SlackRubyBot::Config.send_gifs = false
end
it 'does not send a gif' do
expect(Giphy).to_not receive(:random)
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message')
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user')
end
after do
SlackRubyBot::Config.send_gifs = nil
end
end
context 'set to false via SLACK_RUBY_BOT_SEND_GIFS' do
before do
ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'false'
end
it 'does not send a gif' do
expect(Giphy).to_not receive(:random)
expect(SlackRubyBot::Commands::Base).to receive(:send_client_message).with(client, channel: 'channel', text: 'message')
app.send(:message, client, text: "#{SlackRubyBot.config.user} send_message_with_gif_spec message", channel: 'channel', user: 'user')
end
after do
ENV.delete 'SLACK_RUBY_BOT_SEND_GIFS'
end
end
end
end
55 changes: 55 additions & 0 deletions spec/slack-ruby-bot/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

describe SlackRubyBot::Config do
context 'send_gifs?' do
context 'default' do
it 'is true' do
expect(SlackRubyBot::Config.send_gifs?).to be true
end
end
context 'set to false' do
before do
SlackRubyBot::Config.send_gifs = false
end
it 'is false' do
expect(SlackRubyBot::Config.send_gifs?).to be false
end
after do
SlackRubyBot::Config.send_gifs = nil
end
end
context 'set to false via SLACK_RUBY_BOT_SEND_GIFS' do
before do
ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'false'
end
it 'is false' do
expect(SlackRubyBot::Config.send_gifs?).to be false
end
after do
ENV.delete 'SLACK_RUBY_BOT_SEND_GIFS'
end
end
context 'set to true' do
before do
SlackRubyBot::Config.send_gifs = true
end
it 'is false' do
expect(SlackRubyBot::Config.send_gifs?).to be true
end
after do
SlackRubyBot::Config.send_gifs = nil
end
end
context 'set to true via SLACK_RUBY_BOT_SEND_GIFS' do
before do
ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'true'
end
it 'is false' do
expect(SlackRubyBot::Config.send_gifs?).to be true
end
after do
ENV.delete 'SLACK_RUBY_BOT_SEND_GIFS'
end
end
end
end

0 comments on commit 0b9bba3

Please sign in to comment.