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

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

Closed
4 of 10 tasks
Levelleor opened this issue Apr 7, 2021 · 5 comments
Closed
4 of 10 tasks

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

Levelleor opened this issue Apr 7, 2021 · 5 comments
Labels
question M-T: User needs support to use the project

Comments

@Levelleor
Copy link

Levelleor commented Apr 7, 2021

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 [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

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

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

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'
@gitwave gitwave bot added the untriaged label Apr 7, 2021
@misscoded misscoded added question M-T: User needs support to use the project and removed untriaged labels Apr 8, 2021
@misscoded
Copy link
Contributor

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

@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
Copy link
Member

seratch commented Apr 19, 2021

@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:

  • Use an admin user's user token, not yours (you may be an admin but using a different user can be easier to understand for the members in the workspace)
  • Change the workspace settings to allow bots to do this operation (probably, this is not feasible though)

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

@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
Copy link
Member

seratch commented Apr 20, 2021

Thank you for your understanding.

@seratch seratch closed this as completed Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

3 participants