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?
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.
FYI you can now list user channels/conversations using:
https://api.slack.com/methods/users.conversations
Related
I want to send a message to any public channel that my bot gets added to. I notice there's the member_joined_channel event as documentated here, however I'm not sure how to figure out when to determine if my bot was the invited member. I know there's the user property, but I don't want to hardcode my bot's user ID.
I'm not familiar with the slack API, but i found this endpoint :
https://api.slack.com/types/conversation
In the response, you have a flag is_member which contains what you need I think :
is_member indicates whether the user, bot user or Slack app associated
with the token making the API call is itself a member of the
conversation.
EDIT:
then, you can retreive the list of public channel and have this flag on every channel accessible :
https://api.slack.com/methods/conversations.list
If you are writing a WebSocket-based "RTM" bot, you can listen for the channel_joined event, which is sent only for your own user/bot.
For the more typical Webhook-based bot, the best choice is to listen for the member_joined_channel event and compare the user field if you want an event-based implementation. Hardcoding or otherwise storing your bot's user id is a necessity.
Otherwise, as suggested in the previous answer, you can periodically query all conversations with the conversations.list method and check if you have become a member using the is_member field.
In case one of these methods does not provide the is_private field that you need to determine whether a channel is public, you can use the conversations.info method, which returns a channel object with the is_private field.
Coversation info is your friend https://api.slack.com/methods/conversations.info
I have an old slack app that relies on channel.join. I need to migrate conversations.join. The APIs have two different keys. Channels allows you to use the channel name, and conversations requires a conversation id. How do you convert them? conversations.list -> loop?
You will probably have to loop. The new API is very focused on IDs and the advice is to store the IDs of the channels you want to join not their names, because name can be changed at any time. It feels like they’re saying you can join by name if you want but we won’t make it easy for you :) It's probably good advice tbh.
Does Slack provide a method that allows me to retrieve a list of all joined channels of a user or bot?
I am unable to find a proper API method and want to avoid using either the default channel.list or group.list since it would be expensive to execute 2 api calls and crawl through all channels and match the members ids, possibly even use pagination because of the member array limits of each channel.
There is no single API method that gives you all joined channels of a user / bot with a single API call.
The quickest methods (with two API calls) is indeed calling channels.list and groups.list and matching the result against your user ID, however this is not the recommend approach due to the recently introduced member cap to max. 500 members per channel. Note that this is a hard cap (!!), so you can't get additional members by pagination.
The recommended approach is to use the newer conversations methods, which have the benefit to work for both public and private channels and will return the complete list of members (with pagination).
However, this approach will require even more API calls:
Get list of all public and private channels with conversations.list
Get members per channel with conversations.members per channel
Compile list of channels per user by comparing user ID with all
members of all channels.
If you feel that this function is missing in the API I would recommend sending a feature request to the Slack team.
i am trying to use the API to get the total number of messages posted by each user during a certain period. Ideally, I would be able to break the number of messages by the type of channel (public, private, direct messages.) Is this possible? I am looking through the API documentation but haven't found anything. I would be using it to create a script that would automatically generate weekly activity reports.
Thank you for any advice you can provide!
There is no special endpoint for this information as far as I know, but you can generate something similar yourself by looping though all channels and counting the message per user, e.g.
Get list of all channels with conversations.list
Get history of each channel with conversations.history
Count messages per user
Of course your results would not include message from channels, that your bot has no access too (e.g. some private channels, direct message channels).
Say, I have a channel named teamA, and also got a channel named subTeamB, and subTeamB is actually a sub-team of team A, e.g. all members in subTeamB included in teamA.
In this case, is that possible to have subTeamB nested inside teamA?
No, you can not have channels within a channel in Slack.
As a workaround you could name the channels in a way that they reflect sub-channels.
Examples:
#teamA-subTeamA
#teamA-subTeamB
This is also what Slack recommends on its documentation page about Organizing and naming channels.
They did say they were working on it, or something to better organize:
https://twitter.com/slackhq/status/503652089755873280
Makes sense. We're exploring better ways to organize / group large
amounts of channels. Stay tuned! ✨— Slack (#SlackHQ) January
9, 2016