Writing a slack bot and I would like to be able to get a list of all the channels my bot is a member of. One way to do this is to call https://slack.com/api/channels.list
, get a (potentially large) list of all channels and then search for the channels that the current (bot) user is a member of. This works fine, but seems very heavy handed.
Is there a better way? To get just the channels that a given user is a member of?
One way to do this is to call https://slack.com/api/channels.list , get a (potentially large) list of all channels and then search for the channels that the current (bot) user is a member of.
I think users.conversations
is what you're looking for. Without additional params it will return all public channels the calling user is a member of.
No, there is no shorter way to get this information.
Actually, Slack recommends to use the new conversations
methods for this task, since the members
property in all other methods, e.g. channels.list has recently been changed to only return a truncated user list. See here for details.
With conversations
you have to make an additional call per channel to get all channels a user is a member of. However it will work with all types of channels (e.g. public channels, private channels) at the same time.
The basic approach is:
Get the list of all conversations from conversations.list
Get the list of members per conversation form conversations.members
.
So if you want your Slack app to be future proof and also work with large number of users better use the conversations
methods for your task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With