0

Get Channel Details

by
Published today

Get a conversation's details (name, topic, purpose, membership) by ID.

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
 * Get Channel Details
26
 * Get a conversation's details — name, topic, purpose, creator, archived flag — by ID. Set `include_num_members` for the member count. 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
  include_num_members?: boolean
32
) {
33
  const client = new WebClient(auth.token)
34
  return await client.conversations.info(
35
    dropEmpty({ channel, include_num_members })
36
  )
37
}
38

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