0

Get Space

by
Published today

Get a Chat space's details (name, type, settings, member count).

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
 * Get Space
32
 * Get a Chat space, group chat or DM: display name, type, threading state, history setting, member counts and URL.
33
 */
34
export async function main(auth: RT.Gchat, space: DynSelect_space) {
35
  const response = await fetch(`https://chat.googleapis.com/v1/${space}`, {
36
    headers: {
37
      Authorization: `Bearer ${auth.token}`,
38
      Accept: "application/json",
39
    },
40
  })
41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  return await response.json()
46
}
47