0

Respond to Event

by
Published today

Accept, decline or tentatively accept an event invitation as the connected account.

Script gcal Verified

The script

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

3
/**
4
 * Respond to Event
5
 * RSVP to an event invitation as the connected account: accept, decline or tentatively accept, with an optional note to the organizer. For a recurring event, pass the series id to answer every occurrence or an instance id for one. Fails if the connected account isn't on the guest list.
6
 */
7
export async function main(
8
  auth: RT.Gcal,
9
  event_id: string,
10
  response_status: "accepted" | "declined" | "tentative",
11
  comment: string | undefined,
12
  calendar_id: string = "primary",
13
  send_updates: "all" | "externalOnly" | "none" = "all"
14
) {
15
  const base = `https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendar_id || "primary")}/events/${event_id}`
16
  const headers = {
17
    Authorization: `Bearer ${auth.token}`,
18
    Accept: "application/json",
19
  }
20
  const get = await fetch(base, { headers })
21
  if (!get.ok) {
22
    throw new Error(`${get.status} ${await get.text()}`)
23
  }
24
  const event = await get.json()
25

26
  const attendees: any[] = event.attendees ?? []
27
  const me = attendees.find((a) => a.self)
28
  if (!me) {
29
    throw new Error(
30
      `The connected account is not a guest of event ${event_id}, so it has no invitation to answer`
31
    )
32
  }
33
  me.responseStatus = response_status
34
  if (comment) me.comment = comment
35

36
  const response = await fetch(`${base}?sendUpdates=${send_updates || "all"}`, {
37
    method: "PATCH",
38
    headers: { ...headers, "Content-Type": "application/json" },
39
    body: JSON.stringify({ attendees }),
40
  })
41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  return await response.json()
46
}
47