//native
export type DynSelect_space = string
// Dropdown of the spaces, group chats and DMs the user is a member of.
export async function space(auth: RT.Gchat) {
const response = await fetch(
"https://chat.googleapis.com/v1/spaces?pageSize=1000",
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { spaces } = (await response.json()) as {
spaces?: { name: string; displayName?: string; spaceType: string }[]
}
return (spaces ?? []).map((s) => ({
value: s.name,
label: s.displayName
? `${s.displayName} (${s.name})`
: `${s.spaceType} ${s.name}`,
}))
}
/**
* Send Message
* Post a text message to a Chat space as the connected user. Supports Chat's markup (`*bold*`, `_italic_`, `<users/123>` mentions). Set `thread_key` to group messages into the same thread: the first message with a key starts the thread, later ones reply in it.
*/
export async function main(
auth: RT.Gchat,
space: DynSelect_space,
text: string,
thread_key: string | undefined
) {
const url = new URL(`https://chat.googleapis.com/v1/${space}/messages`)
const body: { [key: string]: any } = { text }
if (thread_key !== undefined && thread_key !== "") {
url.searchParams.append(
"messageReplyOption",
"REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
)
body.thread = { threadKey: thread_key }
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 4 hours ago