diff --git a/CHANGELOG.md b/CHANGELOG.md index 36cfadb..260d731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/README.md b/README.md index f6b1365..ea0fac5 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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`. diff --git a/lib/slack-ruby-bot/commands/base.rb b/lib/slack-ruby-bot/commands/base.rb index a08b4b7..605d9fc 100644 --- a/lib/slack-ruby-bot/commands/base.rb +++ b/lib/slack-ruby-bot/commands/base.rb @@ -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") diff --git a/lib/slack-ruby-bot/config.rb b/lib/slack-ruby-bot/config.rb index 221e396..a30acbf 100644 --- a/lib/slack-ruby-bot/config.rb +++ b/lib/slack-ruby-bot/config.rb @@ -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 diff --git a/lib/slack-ruby-bot/hooks/message.rb b/lib/slack-ruby-bot/hooks/message.rb index 82164de..0316f1a 100644 --- a/lib/slack-ruby-bot/hooks/message.rb +++ b/lib/slack-ruby-bot/hooks/message.rb @@ -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) } diff --git a/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb b/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb index eb9d321..05cc9bc 100644 --- a/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb +++ b/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb @@ -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 diff --git a/spec/slack-ruby-bot/config_spec.rb b/spec/slack-ruby-bot/config_spec.rb new file mode 100644 index 0000000..ed7489d --- /dev/null +++ b/spec/slack-ruby-bot/config_spec.rb @@ -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