Browse Source

Now with animated GIFs.

master
dblock 9 years ago
parent
commit
4acc53bcbb
14 changed files with 53 additions and 7 deletions
  1. +5
    -1
      DEPLOYMENT.md
  2. +1
    -0
      Gemfile
  3. +13
    -0
      Gemfile.lock
  4. +2
    -0
      README.md
  5. +1
    -1
      app/slack-mathbot/commands/about.rb
  6. +11
    -0
      app/slack-mathbot/commands/base.rb
  7. +9
    -2
      app/slack-mathbot/commands/calculate.rb
  8. +1
    -1
      app/slack-mathbot/commands/help.rb
  9. +1
    -1
      app/slack-mathbot/commands/hi.rb
  10. +1
    -1
      app/slack-mathbot/commands/unknown.rb
  11. +3
    -0
      config/initializers/giphy.rb
  12. +3
    -0
      spec/slack-mathbot/commands/calculate_spec.rb
  13. +1
    -0
      spec/support/slack-mathbot/respond_with_error.rb
  14. +1
    -0
      spec/support/slack-mathbot/respond_with_slack_message.rb

+ 5
- 1
DEPLOYMENT.md View File

@ -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.

+ 1
- 0
Gemfile View File

@ -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'

+ 13
- 0
Gemfile.lock View File

@ -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)

+ 2
- 0
README.md View File

@ -17,6 +17,8 @@ On the next screen, note the API token.
Run `SLACK_API_TOKEN=<your 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).

+ 1
- 1
app/slack-mathbot/commands/about.rb View File

@ -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

+ 11
- 0
app/slack-mathbot/commands/base.rb View File

@ -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

+ 9
- 2
app/slack-mathbot/commands/calculate.rb View File

@ -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

+ 1
- 1
app/slack-mathbot/commands/help.rb View File

@ -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

+ 1
- 1
app/slack-mathbot/commands/hi.rb View File

@ -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

+ 1
- 1
app/slack-mathbot/commands/unknown.rb View File

@ -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

+ 3
- 0
config/initializers/giphy.rb View File

@ -0,0 +1,3 @@
Giphy::Configuration.configure do |config|
config.api_key = ENV['GIPHY_API_KEY'] || 'dc6zaTOxFJmzC' # from https://github.com/Giphy/GiphyAPI
end

+ 3
- 0
spec/slack-mathbot/commands/calculate_spec.rb View File

@ -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

+ 1
- 0
spec/support/slack-mathbot/respond_with_error.rb View File

@ -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)

+ 1
- 0
spec/support/slack-mathbot/respond_with_slack_message.rb View File

@ -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

Loading…
Cancel
Save