I have a Slack channel created with a Slack app. In the channel, I can enter /leave and I leave the channel, but I want to have an interactive button in my app that will do the same thing as /leave so a user just needs to click the button instead of entering the command. What's the best way to do that?
-
Hi and welcome to Stack Overflow. Franky, I don't think your idea would work very well, because there is no way to permanently place a button in a Slack channel. So the user will always need to first enter a slash command to get the button. So it would be easier to user `/leave` in the first place. – Erik Kalkoken Oct 10 '18 at 11:53
-
We're using the Slack API to programmatically create channels for users in an app. https://api.slack.com/methods/channels.create – Rizzo95 Oct 11 '18 at 13:27
-
I understand. But that does not change the fact that you can not create permanent buttons for a channel. Buttons are always part of a message, and messages posted to a channel will disappear from view once more messages are posted. – Erik Kalkoken Oct 11 '18 at 13:45
-
Yes, that's true @ErikKalkoken I meant a button in a message that will initiate the command /leave – Rizzo95 Oct 12 '18 at 14:09
1 Answers
Here is how you can implement a button on Slack that will allows a user to leave his/her current channel:
Add an interactive button to a message and send it to the channel. e.g. as response to a slash command.
Once a user clicks the button it will send are request to your app containing its user ID and channel ID
You app can then use
conversations.kick
to remove the user from the channel, assuming your app has the correct scopes (e.g.channels.write
for public channels). Note that you need to use the access token for this method. A bot token won't work.
However, there are some potential stumbling blocks to consider:
The admins of a Slack workspace can restrict the right to kick somebody from a channel. You app will get the corresponding error message if that happens:
restricted_action
. Similar for the "general" channel:cant_kick_from_general
This approach also works in private channel, but with one caveats: Your app needs to be a member of a private channel in order to be able to kick a user from it. Which means that the user that installed your app needs to be a member.

- 30,467
- 8
- 79
- 114