Edits history of script submission #23304 for ' Respond to Event (gcal)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Respond to Event
     * 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.
     */
    export async function main(
      auth: RT.Gcal,
      event_id: string,
      response_status: "accepted" | "declined" | "tentative",
      comment: string | undefined,
      calendar_id: string = "primary",
      send_updates: "all" | "externalOnly" | "none" = "all"
    ) {
      const base = `https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(calendar_id || "primary")}/events/${event_id}`
      const headers = {
        Authorization: `Bearer ${auth.token}`,
        Accept: "application/json",
      }
      const get = await fetch(base, { headers })
      if (!get.ok) {
        throw new Error(`${get.status} ${await get.text()}`)
      }
      const event = await get.json()
    
      const attendees: any[] = event.attendees ?? []
      const me = attendees.find((a) => a.self)
      if (!me) {
        throw new Error(
          `The connected account is not a guest of event ${event_id}, so it has no invitation to answer`
        )
      }
      me.responseStatus = response_status
      if (comment) me.comment = comment
    
      const response = await fetch(`${base}?sendUpdates=${send_updates || "all"}`, {
        method: "PATCH",
        headers: { ...headers, "Content-Type": "application/json" },
        body: JSON.stringify({ attendees }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago