slack-ruby / slack-ruby-bot-server-events

Slack commands, interactive buttons, and events extension for slack-ruby-bot-server.
MIT License
71 stars 10 forks source link

Error when processing team_join event. #5

Closed ghost closed 3 years ago

ghost commented 3 years ago

I am trying to create a simple bot for greetings. According to the instructions in the Slack API, I need to process the "team_join" event. But while processing it, I get the error:

12:49:21 web.1  | E, [2020-12-28T12:49:21.032375 #2829] ERROR -- : Slack::Web::Api::Errors::SlackError: invalid_array_arg
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/slack-ruby-client-0.15.1/lib/slack/web/faraday/response/raise_error.rb:16:in `on_complete'
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/faraday-1.1.0/lib/faraday/response.rb:12:in `block in call'
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/faraday-1.1.0/lib/faraday/response.rb:65:in `on_complete'
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/faraday-1.1.0/lib/faraday/response.rb:11:in `call'
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/faraday-1.1.0/lib/faraday/response.rb:11:in `call'
12:49:21 web.1  |   /home/deadalice/.rvm/gems/ruby-2.7.2/gems/faraday-1.1.0/lib/faraday/request/url_encoded.rb:25:in `call'

This is the code I use to process event:

SlackRubyBotServer::Events.configure do |config|
  config.on :event, ['event_callback', 'team_join'] do |event|
    client = Slack::Web::Client.new(token: team_token(event[:event][:team]))
    user = client.users_info(user: event[:event][:user])
    unless user[:user][:is_bot] || team.users.exists?(user_id: event[:event][:user])
      team.users.create(user_id: event[:event][:user], user_name: user[:user][:name])
      #event.answer(blocks: WelcomeMessage.blocks) unless debug?
      event.logger.info "team_join #{event[:event][:user]} #{user[:user][:name]}"
    end
    nil
  end
end

Please ignore the commented line: this is my simple wrapper to quickly respond in events and in both cases it was commented out.

It is interesting that by copying the same code into the "member_join_channel" handler, everything works good.

dblock commented 3 years ago

What's the rest of that stack trace? I bet this isn't coming from the library and the data in event is different. Do this:

SlackRubyBotServer::Events.configure do |config|
  config.on :event, ['event_callback', 'team_join'] do |event|
   begin
    client = Slack::Web::Client.new(token: team_token(event[:event][:team]))
    user = client.users_info(user: event[:event][:user])
    unless user[:user][:is_bot] || team.users.exists?(user_id: event[:event][:user])
      team.users.create(user_id: event[:event][:user], user_name: user[:user][:name])
      #event.answer(blocks: WelcomeMessage.blocks) unless debug?
      event.logger.info "team_join #{event[:event][:user]} #{user[:user][:name]}"
    end
    nil
  rescue Slack::Web::Api::Errors::SlackError => e
    p e
    p e.backtrace
    raise
  end
end

If this shows a better exception with the line that actually raised it, we need to do something in the code that invokes callbacks to help the caller find the culprit.

dblock commented 3 years ago

According to the documentation, event[:event][:user] is a hash, probably containing id, so users_info(user: event[:event][:user]) is incorrect and should be something like users_info(user: event[:event][:user][:id])?

ghost commented 3 years ago

I just added a rescue section, after the holidays I will test and report the result.

The problem is that, as I said, users_info(user: event[:event][:user]) works correctly for other events except only this one, but I'll try to check that too.

ghost commented 3 years ago

Error caused by line user = client.users_info(user: event[:event][:user]). As I can see, I need to use event[:event][:user][:id] there? If that so, it's strange, because I don't need to add [:id] in other events. Anyway, I will check it later and inform you.

dblock commented 3 years ago

Well, these are different APIs.

member_joined_channel sends

{
    "type": "member_joined_channel",
    "user": "W06GH7XHN",
    "channel": "C0698JE0H",
    "channel_type": "C",
    "team": "T024BE7LD",
    "inviter": "U123456789"
}

team_join sends

{
    "type": "team_join",
    "user": {
        …
    }
}

So really just apples and oranges.

I'll close this since we found the problem, but feel free to ask more questions here if you would like.

dblock commented 1 year ago

There was a comment that was edited to remove the key, I deleted it. The user seems to be deleted from GitHub unfortunately so we'll never know who it was.

joeleonjr commented 1 year ago

Great. I'm going to delete my original comment now too!