Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlackRubyBot.config.token does not work? #240

Closed
jrab89 opened this issue Jan 13, 2020 · 3 comments · Fixed by #254
Closed

SlackRubyBot.config.token does not work? #240

jrab89 opened this issue Jan 13, 2020 · 3 comments · Fixed by #254
Labels

Comments

@jrab89
Copy link

jrab89 commented Jan 13, 2020

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
Copy link
Collaborator

dblock commented Jan 14, 2020

  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.

@dblock dblock changed the title Project structure and configuration using SlackRubyBot::Commands::Base SlackRubyBot.config.token does not work? Jan 14, 2020
@dblock dblock added the bug? label Jan 14, 2020
@wasabigeek
Copy link
Contributor

wasabigeek commented Apr 5, 2020

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
Copy link
Collaborator

dblock commented Apr 5, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants