slack-ruby / slack-ruby-bot

The easiest way to write a Slack bot in Ruby.
MIT License
1.12k stars 187 forks source link

SlackRubyBot.config.token does not work? #240

Closed jrab89 closed 4 years ago

jrab89 commented 4 years ago

I'm working on a Slack bot where I have one subclass of SlackRubyBot::Commands::Base per file. I also have my logger and Slack token that gets set within a Rails initializer file. What I have is equivalent to:

require 'slack-ruby-bot'
require 'logger'

SlackRubyBot.configure do |config|
  config.token = ENV['MY_SLACK_TOKEN']
  config.logger = Logger.new(STDOUT, level: Logger::INFO)
end

class FirstCommand < SlackRubyBot::Commands::Base
  command 'first' do |client, data, match|
    client.say(text: '1!', channel: data.channel)
  end
end

class SecondCommand < SlackRubyBot::Commands::Base
  command 'second' do |client, data, match|
    client.say(text: '2!', channel: data.channel)
  end
end

class MySlackBot < SlackRubyBot::Bot
end

MySlackBot.run
  1. Is this a reasonable structure? I didn't see anything in the README or examples to suggest otherwise
  2. config.token doesn't seem to work. I get the following stack trace when I run my code:

    $ MY_SLACK_TOKEN=my-token-here bundle exec ruby my_slack_bot.rb
    Traceback (most recent call last):
            7: from my_slack_bot.rb:25:in `<main>'
            6: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/bot.rb:6:in `run'
            5: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/bot.rb:10:in `instance'
            4: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:19:in `instance'
            3: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:19:in `new'
            2: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:4:in `initialize'
            1: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot.rb:12:in `configure'
    /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:5:in `block in initialize': Missing ENV['SLACK_API_TOKEN']. (RuntimeError)

    But when I run my code like SLACK_API_TOKEN=my-token-here bundle exec ruby my_slack_bot.rb instead, it works as expected

  3. It seems odd to have an empty subclass of SlackRubyBot::Bot. Is that the right way to combine and run multiple subclasses of SlackRubyBot::Commands::Base?

Thanks for your help

dblock commented 4 years ago
  1. Is this a reasonable structure? I didn't see anything in the README or examples to suggest otherwise

Looks reasonable to me.

  1. config.token doesn't seem to work. I get the following stack trace when I run my code:
    $ MY_SLACK_TOKEN=my-token-here bundle exec ruby my_slack_bot.rb
    Traceback (most recent call last):
           7: from my_slack_bot.rb:25:in `<main>'
           6: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/bot.rb:6:in `run'
           5: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/bot.rb:10:in `instance'
           4: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:19:in `instance'
           3: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:19:in `new'
           2: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:4:in `initialize'
           1: from /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot.rb:12:in `configure'
    /Users/jeff.rabovsky/.gem/ruby/2.6.5/gems/slack-ruby-bot-0.12.0/lib/slack-ruby-bot/app.rb:5:in `block in initialize': Missing ENV['SLACK_API_TOKEN']. (RuntimeError)

It should. Would be a bug.

But when I run my code like SLACK_API_TOKEN=my-token-here bundle exec ruby my_slack_bot.rb instead, it works as expected

Try to debug it, write a spec?

  1. It seems odd to have an empty subclass of SlackRubyBot::Bot. Is that the right way to combine and run multiple subclasses of SlackRubyBot::Commands::Base?

It's not necessary, just gives you a place to put your own code. You can instantiate an instance of SlackRubyBot::Bot and it should just work.

Note that subclassing from base and auto-loading of commands could cause some potential problems with ordering of things, like #225.

wasabigeek commented 4 years ago

config.token doesn't seem to work

~@jrab89 could I clarify on the above - did you mean you were trying to hardcode the API Token in the SlackRubyBot.configure block?~ It looks like the code only supports defining the token via an ENV variable.

dblock commented 4 years ago

Duh, thanks for digging this up @wasabigeek. Care to PR support for config.token and config.aliases?