diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 02542db..5655602 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -20,6 +20,10 @@ Set SLACK_API_TOKEN from the Bot integration settings on Slack. heroku config:add SLACK_API_TOKEN=... ``` +#### GIPHY_API_KEY + +Slack-Gamebot replies with animated GIFs. While it's currently not necessary, uyou may need to set GIPHY_API_KEY in the future, see [github.com/Giphy/GiphyAPI](https://github.com/Giphy/GiphyAPI) for details. + ### Heroku Idling -Heroku free tier applications will idle. Use [UptimeRobot](http://uptimerobot.com) or similar to prevent your instance from sleeping or pay for a production dyno. +Heroku free tier applications will idle. Either pay 7$ a month for the hobby dyno or use [UptimeRobot](http://uptimerobot.com) or similar to prevent your instance from sleeping or pay for a production dyno. diff --git a/Gemfile b/Gemfile index c419b7b..0268d81 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'puma' gem 'sinatra' gem 'activesupport' gem 'dentaku' +gem 'giphy', '~> 2.0.2' group :development, :test do gem 'rake', '~> 10.4' diff --git a/Gemfile.lock b/Gemfile.lock index 425fe85..4775c92 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,17 +20,29 @@ GEM multipart-post (>= 1.2, < 3) faraday_middleware (0.9.1) faraday (>= 0.7.4, < 0.10) + faraday_middleware-parse_oj (0.3.0) + faraday (~> 0.9.0) + faraday_middleware (~> 0.9.1) + oj (~> 2.0) faye-websocket (0.9.2) eventmachine (>= 0.12.0) websocket-driver (>= 0.5.1) foreman (0.78.0) thor (~> 0.19.1) + giphy (2.0.2) + faraday (~> 0.9) + faraday_middleware (~> 0.9) + faraday_middleware-parse_oj (~> 0.3) + launchy (~> 2.4) hashie (3.4.1) i18n (0.7.0) json (1.8.2) + launchy (2.4.3) + addressable (~> 2.3) minitest (5.6.1) multi_json (1.11.0) multipart-post (2.0.0) + oj (2.12.9) parser (2.3.0.pre.2) ast (>= 1.1, < 3.0) powerpack (0.1.1) @@ -93,6 +105,7 @@ DEPENDENCIES activesupport dentaku foreman + giphy (~> 2.0.2) hashie puma rack-test (~> 0.6.2) diff --git a/README.md b/README.md index 44773b7..5a14816 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ On the next screen, note the API token. Run `SLACK_API_TOKEN= foreman start` +While it's currently not necessary, uyou may need to set _GIPHY_API_KEY_ in the future, see [github.com/Giphy/GiphyAPI](https://github.com/Giphy/GiphyAPI) for details. + ## Production Deployment See [DEPLOYMENT](DEPLOYMENT.md). diff --git a/app/slack-mathbot/commands/about.rb b/app/slack-mathbot/commands/about.rb index 8dd9328..260b28b 100644 --- a/app/slack-mathbot/commands/about.rb +++ b/app/slack-mathbot/commands/about.rb @@ -2,7 +2,7 @@ module SlackMathbot module Commands class Default < Base def self.call(data, _command, _arguments) - send_message data.channel, SlackMathbot::ABOUT + send_message_with_gif data.channel, SlackMathbot::ABOUT, 'math' end end end diff --git a/app/slack-mathbot/commands/base.rb b/app/slack-mathbot/commands/base.rb index f89caa1..51f6e46 100644 --- a/app/slack-mathbot/commands/base.rb +++ b/app/slack-mathbot/commands/base.rb @@ -5,6 +5,17 @@ module SlackMathbot Slack.chat_postMessage(channel: channel, text: text) end + def self.send_message_with_gif(channel, text, keywords) + gif = begin + Giphy.random(keywords) + rescue StandardError => e + logger.warn "Giphy.random: #{e.message}" + nil + end + text = text + "\n" + gif.image_url.to_s if gif + send_message channel, text + end + def self.logger @logger ||= begin $stdout.sync = true diff --git a/app/slack-mathbot/commands/calculate.rb b/app/slack-mathbot/commands/calculate.rb index a554d61..a1f6901 100644 --- a/app/slack-mathbot/commands/calculate.rb +++ b/app/slack-mathbot/commands/calculate.rb @@ -2,8 +2,15 @@ module SlackMathbot module Commands class Calculate < Base def self.call(data, _command, arguments) - result = Dentaku::Calculator.new.evaluate(arguments.join) || 'Got nothing.' - send_message data.channel, result.to_s + result = Dentaku::Calculator.new.evaluate(arguments.join) + result = result.to_s if result + if result && result.length > 0 + send_message data.channel, result + else + send_message_with_gif data.channel, 'Got nothing.', 'nothing' + end + rescue StandardError => e + send_message_with_gif data.channel, "Sorry, #{e.message}.", 'idiot' end end end diff --git a/app/slack-mathbot/commands/help.rb b/app/slack-mathbot/commands/help.rb index b9de666..420c616 100644 --- a/app/slack-mathbot/commands/help.rb +++ b/app/slack-mathbot/commands/help.rb @@ -2,7 +2,7 @@ module SlackMathbot module Commands class Help < Base def self.call(data, _command, _arguments) - send_message data.channel, 'See https://github.com/dblock/slack-mathbot, please.' + send_message_with_gif data.channel, 'See https://github.com/dblock/slack-mathbot, please.', 'help' end end end diff --git a/app/slack-mathbot/commands/hi.rb b/app/slack-mathbot/commands/hi.rb index 85711d3..0f7fe3c 100644 --- a/app/slack-mathbot/commands/hi.rb +++ b/app/slack-mathbot/commands/hi.rb @@ -2,7 +2,7 @@ module SlackMathbot module Commands class Hi < Base def self.call(data, _command, _arguments) - send_message data.channel, "Hi <@#{data.user}>!" + send_message_with_gif data.channel, "Hi <@#{data.user}>!", 'hi' end end end diff --git a/app/slack-mathbot/commands/unknown.rb b/app/slack-mathbot/commands/unknown.rb index ef43ec5..dfbbb50 100644 --- a/app/slack-mathbot/commands/unknown.rb +++ b/app/slack-mathbot/commands/unknown.rb @@ -2,7 +2,7 @@ module SlackMathbot module Commands class Unknown < Base def self.call(data, _command, _arguments) - send_message data.channel, "Sorry <@#{data.user}>, I don't understand that command!" + send_message_with_gif data.channel, "Sorry <@#{data.user}>, I don't understand that command!", 'idiot' end end end diff --git a/config/initializers/giphy.rb b/config/initializers/giphy.rb new file mode 100644 index 0000000..8d0e328 --- /dev/null +++ b/config/initializers/giphy.rb @@ -0,0 +1,3 @@ +Giphy::Configuration.configure do |config| + config.api_key = ENV['GIPHY_API_KEY'] || 'dc6zaTOxFJmzC' # from https://github.com/Giphy/GiphyAPI +end diff --git a/spec/slack-mathbot/commands/calculate_spec.rb b/spec/slack-mathbot/commands/calculate_spec.rb index 5e8c5d4..ab2277f 100644 --- a/spec/slack-mathbot/commands/calculate_spec.rb +++ b/spec/slack-mathbot/commands/calculate_spec.rb @@ -13,4 +13,7 @@ describe SlackMathbot::Commands::Calculate, vcr: { cassette_name: 'user_info' } it 'sends something without an answer' do expect(message: 'mathbot calculate pi', channel: 'channel').to respond_with_slack_message('Got nothing.') end + it 'reports division by zero' do + expect(message: 'mathbot calculate 1/0', channel: 'channel').to respond_with_slack_message('Sorry, divided by 0.') + end end diff --git a/spec/support/slack-mathbot/respond_with_error.rb b/spec/support/slack-mathbot/respond_with_error.rb index e35787c..31556ac 100644 --- a/spec/support/slack-mathbot/respond_with_error.rb +++ b/spec/support/slack-mathbot/respond_with_error.rb @@ -5,6 +5,7 @@ RSpec::Matchers.define :respond_with_error do |expected| channel, user, message = parse(actual) app = SlackMathbot::App.new SlackMathbot.config.user = 'mathbot' + allow(Giphy).to receive(:random) begin expect do app.send(:message, text: message, channel: channel, user: user) diff --git a/spec/support/slack-mathbot/respond_with_slack_message.rb b/spec/support/slack-mathbot/respond_with_slack_message.rb index cbfb68b..cea2765 100644 --- a/spec/support/slack-mathbot/respond_with_slack_message.rb +++ b/spec/support/slack-mathbot/respond_with_slack_message.rb @@ -5,6 +5,7 @@ RSpec::Matchers.define :respond_with_slack_message do |expected| channel, user, message = parse(actual) app = SlackMathbot::App.new SlackMathbot.config.user = 'mathbot' + allow(Giphy).to receive(:random) expect(SlackMathbot::Commands::Base).to receive(:send_message).with(channel, expected) app.send(:message, text: message, channel: channel, user: user) true