Get a list of all private channels with Slack API
Asked Answered
D

3

20

I've been trying to get a list of all "groups" in my Slack team. However, even with admin privs, groups.list only provides the groups that the token owner's account belongs to.

The closest solution I've seen in my research involves getting a bot to sit in the channel. The bot's membership allows it to report on the channel, but then there's the logistical problem of getting the bot into every private channel, despite the fact that we can't list them programmatically.

The code I've used to dig up private channel listings:

import requests
import json

token = '...'

r = requests.post('https://slack.com/api/groups.list', data={'token': token, 'exclude_archived': 1})
if r.ok:
  privatechannels = { c['id']: c['name'] for c in json.loads(r.text)['groups'] }
  print(privatechannels)
Deportment answered 7/6, 2016 at 22:59 Comment(7)
Welcome to Stack Overflow. Your question is premature for Stack Overflow. We expect you to have researched this independently, then tried coding, then when that code breaks ask about it. Please read "How to Ask". If you searched and searched and searched some more, and still didn't find anything, then you need to show us where you searched, explain why that didn't help, and then we can step in.Abercromby
Perhaps it's not clear, but I have tried. There's only so much that can be done with a single API call to a 3rd party service. I've dug through the documentation, written a program to dig up the info (see below), and searched SO and the internet at large.Deportment
edit: can't get multiline code formatting going in here, putting it int he questionDeportment
We appreciate that you've tried, but you need to show us where you looked and explain why it didn't help. Otherwise show us the minimal code as defined in "minimal reproducible example". It's important to understand you're helping us create a reference book for the world, to help answer programming questions for others; The question you ask isn't solely to answer your need, it's for others in the future running into the same problem. So, if you searched and searched and didn't find a solution then we'll help document the problem and fix. If you didn't do that, well... meta.https://mcmap.net/q/22495/-what-do-i-need-to-read-to-understand-how-git-works-closed/128421Abercromby
this will also avoid having ppl repeat what you already did, and will help better understand how you are trying to achieve this. imagine 50 peeps see your post and then got to api.slack.com/methods/channels.list and post an answer with code you have already tried?Dipstick
The code's in the question now...Deportment
Thanks. this question gave the solution of inviting the slack app itself to the private channel. Then the API is able to return that channel when doing a GET request to the api/conversations.listNikkinikkie
D
8

slacks privacy policy does not support this.

The most fundamental privacy principle we follow is that by default, anything you post to Slack is private to your team. That is, viewing the messages and files shared within a specific team requires authentication as a member of that team.

The company's upcoming paid Plus plan will include an optional feature called Compliance Exports, which will let administrators access their team's communications, encompassing public and private messages.

which is the closest thing to getting access to private channels from which you are not part of, but will require a written letter...here for more details

Dipstick answered 7/6, 2016 at 23:33 Comment(3)
Thanks for confirming my suspicions. We've actually signed up for compliance exports and been through all that. However, Legal wants IT to have a rolling log of who's in what channel when, which isn't something offered by compliance exports. I was trying to patch this hole up with some API scripting. Looks like we're going to have to put a bot in every channel if this is going to happen.Deportment
so... how did you get a bot in every channel, if you cant list all the channels in the first place, to know what channels it IS that you need said bot in?? I have this problem too :-(Sobersided
just FYI, I can't even list my own private channels…Remunerative
T
11

If you really need to monitor all private channels (and DMs) on your Slack workspace in real time there is another approach:

  • Ensure every user on your Slack provides a token to your app. This can be done by each user running through the OAuath installing process for your app once. That will create new token for each user, which you app can collect.
  • Iterate through all active user tokens to compile a list of all conversations (public channels, private channels, direct messages, ..) with conversations.list
  • Iterate through all existing conversations - using a token valid for that conversation - to collect all messages with conversations.history
Threadfin answered 4/11, 2018 at 16:0 Comment(5)
thank you for the answer but the app is installed once for the whole workspace, and thus you've one OAuth Access Token (which is retrieved from manage apps page). So how would you get an access token for each user?Southeastward
You must ask each user to re-install the app. Each installation will create an additional token, specific for that user.Threadfin
I've solved the issue in a different way, I guess things have been changed a little bit and your solution wouldn't work. I've followed the following solution 1. slack workspace admin installs the app and approve it to be installed for all users. 2. each user needs to authorize for that app (it's already installed for the whole workspace) by accessing OAuth step. 3. once the user authorized with the app he will be redirected into your server with some code, which you can exchange for a user token later.Southeastward
I may have worded by last comment poorly, but that was exactly what I meant.Threadfin
Thank you bro, I really appreciate that. you really a slack Guru (your answers are everywhere)Southeastward
D
8

slacks privacy policy does not support this.

The most fundamental privacy principle we follow is that by default, anything you post to Slack is private to your team. That is, viewing the messages and files shared within a specific team requires authentication as a member of that team.

The company's upcoming paid Plus plan will include an optional feature called Compliance Exports, which will let administrators access their team's communications, encompassing public and private messages.

which is the closest thing to getting access to private channels from which you are not part of, but will require a written letter...here for more details

Dipstick answered 7/6, 2016 at 23:33 Comment(3)
Thanks for confirming my suspicions. We've actually signed up for compliance exports and been through all that. However, Legal wants IT to have a rolling log of who's in what channel when, which isn't something offered by compliance exports. I was trying to patch this hole up with some API scripting. Looks like we're going to have to put a bot in every channel if this is going to happen.Deportment
so... how did you get a bot in every channel, if you cant list all the channels in the first place, to know what channels it IS that you need said bot in?? I have this problem too :-(Sobersided
just FYI, I can't even list my own private channels…Remunerative
G
0

When you have an OAuth token with the conversation.list privilege in the node API, you can call the API with the {type:'private_channel'} parameter. This will return all the private channels that belong to the OAuth token.

const web = new WebClient(token);

const privatechannellist = await web.conversations.list({ types: 'private_channel' });

Goodsized answered 2/7, 2023 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.