0

List Members in Channel

by
Published today

List the user IDs of a conversation's members, 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
export type DynSelect_channel = string
4

5
// Dropdown of channels the token can see; private channels need groups:read.
6
export async function channel(auth: RT.Slack) {
7
  if (!auth?.token) return []
8
  const client = new WebClient(auth.token)
9
  const res = await client.conversations
10
    .list({
11
      types: "public_channel,private_channel",
12
      exclude_archived: true,
13
      limit: 1000,
14
    })
15
    .catch(() =>
16
      client.conversations.list({ exclude_archived: true, limit: 1000 })
17
    )
18
  return (res.channels ?? []).map((c) => ({
19
    value: c.id!,
20
    label: `#${c.name}`,
21
  }))
22
}
23

24
/**
25
 * List Members in Channel
26
 * List the user IDs in a conversation (resolve them with Get User). Page with `cursor` = the previous `response_metadata.next_cursor`. Needs channels:read (groups:read for private channels, im:read / mpim:read for DMs).
27
 */
28
export async function main(
29
  auth: RT.Slack,
30
  channel: DynSelect_channel,
31
  limit?: number,
32
  cursor?: string
33
) {
34
  const client = new WebClient(auth.token)
35
  return await client.conversations.members(
36
    dropEmpty({ channel, limit, cursor })
37
  )
38
}
39

40
function dropEmpty<T extends { [key: string]: any }>(params: T) {
41
  return Object.fromEntries(
42
    Object.entries(params).filter(
43
      ([, v]) => v !== undefined && v !== null && v !== ""
44
    )
45
  ) as T
46
}
47