I am writing a Slack integration that can boot certain users out of public channels when certain conditions are met. I have added several OAuth scopes to the bot token, including the following:
I am writing my bot in Python using the slack-bolt library and asyncio. However when I try to invoke this code:
await app.client.conversations_kick(channel=channel_id, user=user_id)
I get the following error:
slack_sdk.errors.SlackApiError: The request to the Slack API failed. (url: https://www.slack.com/api/conversations.kick)
The server responded with: {'ok': False, 'error': 'channel_not_found'}
I know for a fact that both the channel_id
and user_id
arguments I'm passing in are valid. The channel ID I'm using is the string C01PAE3DB0A
. I know it is valid because I can use the very same value for channel_id
in the following API call:
response = await app.client.conversations_info(channel=channel_id)
And when I call conversations_info
like that I get all of the information about my channel. (The same is true for calling users_info
with the user_id
- it returns successfully.) So why is that when I pass my valid channel_id
parameter to conversations_kick
I consistently receive this channel_not_found
error? What am I missing?
So I got in touch directly with Slack support about this and they confirmed that there is a bug on their end. Specifically, the bug is that I should have received a restricted_action
error response instead of a channel_not_found
response. Apparently this is a known issue that is on their backlog.
The reason the API call would (try to) return this restricted_action
error is simply because there is a workspace setting that, by default, prevents non-admins from kicking people out of public channels. Furthermore, this setting can only be changed by the workspace owner - one tier above admins.
But assuming you are the owner of the Slack workspace, you simply have to log into the Settings & Permissions page, which should look something like this:
And then you have to change the setting labeled "People who can remove members from public channels" from "Workspace admins and owners only (default)" to "Everyone, except guests."
Once I made that change, my API calls started succeeding.