slackapi / bolt-js

A framework to build Slack apps using JavaScript
https://tools.slack.dev/bolt-js/
MIT License
2.74k stars 393 forks source link

Unable to kick user from a channel | Missing Property botId #869

Closed Levelleor closed 3 years ago

Levelleor commented 3 years ago

Description

I can't find any info on this and still new to Bolt and Slack which both have quite difficult structure to understand. Currently I am trying to add then kick a user to/from a channel. While adding a user seems to be working fine I cannot remove any because of the error: error: 'restricted_action'.

I found a couple of issues on this repository similar to what I have but seems like they're outdated, since they recommend using as_user, which is now deprecated. It appears that Bot doesn't have sufficient permissions to kick anyone with a regular Bot token so I went ahead and replaced it with a User token. I hoped that fix it and provided it access to admin scope.

Now I am getting the following error message when trying to kick user from channel:

code: 'slack_bolt_context_missing_property_error',
missingProperty: 'botId'

I searched it up, there are exactly 2 results on Google. I also found there's a way for an OAuth authorization, but then bot acts directly from my user account, which is not something I want to happen.

The code looks like this:

const app = new App({
  token: tokenUser,
  signingSecret: signinsecret,
  appToken: appToken,
  socketMode: true,
  developerMode: true
});

app.message('bye', async ({ message, say, client }) => {
  try {
    await client.conversations.kick({
      channel: CHANNELID,
      user: message.user
    });
    await say(`Bye <@${message.user}>, you've been successfully kicked!`);
  }
  catch(e) {
    console.error(e);
  }
});

(async () => {
  await app.start(process.env.PORT || 3000);
  console.log('Bolt app is running!');
})();

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])


Reproducible in:

package version: 3.3.0

node version: 14.16.0

OS version(s): Windows 10

Steps to reproduce:

  1. Run the code above
  2. Add bot to the any channel
  3. Type "bye" in that channel

Expected result:

Bot to successfully remove a non-admin user from a channel.

Actual result:

Error:

code: 'slack_bolt_context_missing_property_error',
missingProperty: 'botId'
misscoded commented 3 years ago

Hey @Levelleor! Thanks so much for raising this issue. 🙂

After a little digging, it turns out this is in fact a bug. We've made note of it and will hopefully get a fix out shortly.

In the meantime, to workaround this issue, you can introduce a temporary authorize function that returns both the botId and the userToken, as seen in the example below:

// cached auth.test response
let botAuthTestResult = undefined;

const authorize = async () => {
  if (botAuthTestResult === undefined) {
    // if the cached result does not exist yet
    botAuthTestResult = await app.client.auth.test({token: process.env.SLACK_BOT_TOKEN});
  }
  return {
    // for ignoreSelf middleware compatibility as of bolt-js v3.3
    botId: botAuthTestResult.bot_id,
    // for ignoreSelf middleware compatibility as of bolt-js v3.3
    botUserId: botAuthTestResult.user_id,
    // if the token is a user token, you can use userToken for the key in context
    userToken: process.env.SLACK_USER_TOKEN,
  };
}

const app = new App({
  logLevel: 'debug',
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
  authorize,
});

Thanks again for bringing this to our attention. Do let us know if the above resolves the problem for the time being!

Levelleor commented 3 years ago

@misscoded Thank you. It is finally working. Though I am a little lost. It works under my personal Slack profile now. Is it possible to make it act on its own instead of using my name? I assume it is because of usertoken, though, it makes no sense. Bot is unable to kick with the bot permissions, but is able to do so when logged in to my account?

seratch commented 3 years ago

@Levelleor

It works under my personal Slack profile now. Is it possible to make it act on its own instead of using my name?

Unfortunately, as the app is working on behalf of you (using your user token), it's not possible to let your app behave as a bot user. All the options I can suggest are:

Perhaps, both options are not desirable but I hope this was helpful to you. As we've already provided an answer to your original question here, would you mind closing this issue?

Levelleor commented 3 years ago

@seratch

Got you.

This is weird, weird how Slack's API doesn't allow for some simple things. Hopefully they make it more similar to Discord in future.

Thank you, close this issue if you need to, or leave it open until the bug is fixed. I guess my question is answered.

seratch commented 3 years ago

Thank you for your understanding.