slack-ruby / slack-ruby-client

A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
MIT License
1.19k stars 214 forks source link

examples/hi_real_time_and_web/hi.rb does not work #500

Open svoop opened 9 months ago

svoop commented 9 months ago

Quite possibly, I'm doing something wrong, but after countless tries, I can't pinpoint where the real time example falls short.

To reproduce, create the app and bot OAuth token:

Now open IRB and follow the hi example:

require 'slack-ruby-client'

Slack.configure do |config|
  config.token = 'xoxb-...'   # OAuth token created above
end

client = Slack::RealTime::Client.new

client.on :hello do
  puts(
    "Successfully connected, welcome '#{client.self.name}' " \
    "to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
  )
end

client.start!

The last line raises:

/Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/web/faraday/response/raise_error.rb:19:in `on_complete': not_allowed_token_type (Slack::Web::Api::Errors::NotAllowedTokenType)
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/middleware.rb:18:in `block in call'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/response.rb:42:in `on_complete'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/middleware.rb:17:in `call'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/request/url_encoded.rb:25:in `call'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-multipart-1.0.4/lib/faraday/multipart/middleware.rb:28:in `call'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/rack_builder.rb:153:in `build_response'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/connection.rb:444:in `run_request'
  from /Users/me/.gem/ruby/3.2.0/gems/faraday-2.7.11/lib/faraday/connection.rb:280:in `post'
  from /Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/web/faraday/request.rb:25:in `request'
  from /Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/web/faraday/request.rb:11:in `post'
  from /Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/web/api/endpoints/rtm.rb:19:in `rtm_connect'
  from /Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/real_time/client.rb:170:in `build_socket'
  from /Users/me/.gem/ruby/3.2.0/gems/slack-ruby-client-2.2.0/lib/slack/real_time/client.rb:42:in `start!'
  from (irb):12:in `<main>'
  from /Users/me/.gem/ruby/3.2.0/gems/irb-1.8.1/exe/irb:9:in `<top (required)>'
  from /Users/me/Development/wemakeit/bot/.direnv/bin/irb:27:in `load'
  ... 1 levels...

Any idea where the example (or my interpretation thereof) is wrong?

dblock commented 9 months ago

As mentioned in the document: https://slack.dev/node-slack-sdk/rtm-api

Note: RTM isn’t available for modern scoped apps anymore. We recommend using the Events API and Web API instead. If you need to use RTM (possibly due to corporate firewall limitations), you can do so by creating a legacy scoped app. If you have an existing RTM app, do not update its scopes as it will be updated to a modern scoped app and stop working with RTM.

You need to create a classic Slack app for using RTM API. You can create a classic app from the following URL: https://api.slack.com/apps?new_classic_app=1

We possibly need to update the documentation, or maybe add a comment to the samples? Care to help?

svoop commented 9 months ago

@dblock Thanks for your quick reply. Sure thing, I'll help with these docs to make things easier for people like myself. However, I have to understand it myself first. The RTM API note is present in the auth basiscs docs as well, but a little hidden under "Scopes".

Here's how I got posting as the app to work:

  1. Create the classic app
  2. Add a legacy bot user to it
  3. Install the app
  4. Use the OAuth token with slack-ruby-client.

(I still have to figure out why "on message" doesn't work yet, but that's probably an issue on my end.)

It's a little worrying though having to deal with "classic apps" and "legacy bot users", sounds somewhat like these will go away at some point.

If I understand correctly, the RTM API is superseded by the Events API which sends events as callbacks to wherever you subscribe them. Is it planned or even feasible to add support for the Events API to slack-ruby-client?

dblock commented 9 months ago

Yes, the client fully supports events. But since events just require a web server, it's a bit different. Start with https://github.com/slack-ruby/slack-ruby-bot-server-events. I've been migrating existing apps, and wrote up https://code.dblock.org/2020/11/30/migrating-classic-slack-ruby-bots-to-granular-permissions.html.

svoop commented 8 months ago

@dblock I'll try to get the RTM approach working for now, then migrate later when I have a little more time. This should produce some HOWTO material which might be useful to others. Might take a moment though. Thanks for the hints!