0

List Channels

by
Published today

List conversations in the workspace, filtered by type, with cursor pagination.

Script slack Verified

The script

Submitted by hugo989 Bun
Verified 1 hour ago
1
import { WebClient } from "@slack/web-api"
2

3
/**
4
 * List Channels
5
 * List conversations by type — comma-separated public_channel, private_channel, mpim, im (default public_channel). Page with `cursor` = the previous `response_metadata.next_cursor`. Needs channels:read; private channels need groups:read, DMs im:read and group DMs mpim:read (user-token defaults; for a bot token tick them in the connect dialog). A bot sees only private channels and DMs it is in.
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  types?: string,
10
  exclude_archived?: boolean,
11
  limit?: number,
12
  cursor?: string
13
) {
14
  const client = new WebClient(auth.token)
15
  return await client.conversations.list(
16
    dropEmpty({ types, exclude_archived, limit, cursor })
17
  )
18
}
19

20
function dropEmpty<T extends { [key: string]: any }>(params: T) {
21
  return Object.fromEntries(
22
    Object.entries(params).filter(
23
      ([, v]) => v !== undefined && v !== null && v !== ""
24
    )
25
  ) as T
26
}
27