0

List Members

by
Published today

List the members of a Chat space. Requires chat.memberships.readonly on the connection.

Script gchat Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
export type DynSelect_space = string
4

5
// Dropdown of the spaces, group chats and DMs the user is a member of.
6
export async function space(auth: RT.Gchat) {
7
  const response = await fetch(
8
    "https://chat.googleapis.com/v1/spaces?pageSize=1000",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { spaces } = (await response.json()) as {
20
    spaces?: { name: string; displayName?: string; spaceType: string }[]
21
  }
22
  return (spaces ?? []).map((s) => ({
23
    value: s.name,
24
    label: s.displayName
25
      ? `${s.displayName} (${s.name})`
26
      : `${s.spaceType} ${s.name}`,
27
  }))
28
}
29

30
/**
31
 * List Members
32
 * List the members of a Chat space (human users by default; set `show_groups` to include Google Groups and `show_invited` to include pending invites). `filter` narrows by role or type, e.g. `role = "ROLE_MANAGER"` or `member.type = "HUMAN"`. Pass the returned `nextPageToken` as `page_token` for the next page. Members' `member.displayName` is only filled for the user's own entry. Requires chat.memberships.readonly on the connection (not in the default scopes).
33
 */
34
export async function main(
35
  auth: RT.Gchat,
36
  space: DynSelect_space,
37
  filter: string | undefined,
38
  show_groups: boolean = false,
39
  show_invited: boolean = false,
40
  page_size: number = 100,
41
  page_token: string | undefined
42
) {
43
  const url = new URL(`https://chat.googleapis.com/v1/${space}/members`)
44
  url.searchParams.append("pageSize", String(page_size || 100))
45
  url.searchParams.append("showGroups", String(!!show_groups))
46
  url.searchParams.append("showInvited", String(!!show_invited))
47
  if (filter != null && filter !== "")
48
    url.searchParams.append("filter", filter)
49
  if (page_token != null && page_token !== "")
50
    url.searchParams.append("pageToken", page_token)
51

52
  const response = await fetch(url, {
53
    headers: {
54
      Authorization: `Bearer ${auth.token}`,
55
      Accept: "application/json",
56
    },
57
  })
58
  if (!response.ok) {
59
    throw new Error(`${response.status} ${await response.text()}`)
60
  }
61

62
  return await response.json()
63
}
64