How to get current event's conference data after use schedule conference in Google Calendar

986 Views Asked by At

Background: Google Calendar > click New button > enter New Event Page > Add Conference

Click here to schedule conference

Question: When user click Add Conference to schedule a conference(3rd party service, not Hangouts), how could I get the current event's conference data? I tried to use Calendar.Events.get API but it returned 404.

my appscripts setting is here:

when user schedule a conference, it will trigger onCalendarEventUpdate function

{
  "timeZone": "America/Los_Angeles",
  "addOns": {
    "calendar": {
      "eventUpdateTrigger": {
        "runFunction": "onCalendarEventUpdate"
      },
    }
  }
}

my onCalendarEventUpdate:

function onCalendarEventUpdate(context: any) {
  // I can get calendarId, evnetId
  const {
    calendar: { calendarId, id: evnetId }
  } = context;

  // when I try to get event conferenceData, it returns 404
  let event;
  try {
    event = Calendar.Events && Calendar.Events.get(calendarId, evnetId);
    if (!event) {
      Logger.log(`[getEventsCollectionByCalendarId]event not found`);
    }
  } catch (e) {
    Logger.log(`[getEventsCollectionByCalendarId]error: ${JSON.stringify(e)}`);
  }
}

now the error message is:

{
    "message":"API call to calendar.events.get failed with error: Not Found",
    "name":"GoogleJsonResponseException",
    "lineNumber":64,
    "details":{
        "message":"Not Found",
        "code":404,
        "errors":[{
            "domain":"global",
            "reason":"notFound",
            "message":"Not Found"
        }]
    }
}
2

There are 2 best solutions below

3
On

Based on the error message, I would guess your calendarId and eventId are invalid. The event updated event does not give you the event id sadly. Because of this, you need to perform an incremental sync to get the updated event data, which means you need to do an initial sync first as described in the docs (link below).

First, run this code to perform the initial sync and get the nextSyncTokens for each calendar. You only need to run this once.

function initialSyncToSetNextSyncTokens() {
  const calendarIds = Calendar.CalendarList.list()["items"].map((item) => {
    return item["id"]
  });
  for (let calendarId of calendarIds) {
    let options = {maxResults: 2500, nextPageToken: undefined}
    let response = {}
    do {
      response = Calendar.Events.list(calendarId, options)
      options["nextPageToken"] = response["nextPageToken"]
    } while (options["nextPageToken"])
    PropertiesService.getScriptProperties().setProperty(calendarId, response["nextSyncToken"])
  }
}

Then, set your trigger to run this function and log the conference data. Notice we also update the nextSyncToken so the next execution will work correctly.

function onEventUpdated(context) {
  const calendarId = context["calendarId"]
  const nextSyncToken = PropertiesService.getScriptProperties().getProperty(calendarId)
  const response = Calendar.Events.list(calendarId, {syncToken: nextSyncToken})
  PropertiesService.getScriptProperties().setProperty(calendarId, response["nextSyncToken"])
  const event = response["items"][0] // assumes this code will run before another event is created
  const conferenceData = event["conferenceData"]
  console.log(conferenceData)
}

Link to relevant docs:

https://developers.google.com/apps-script/guides/triggers/events#google_calendar_events

0
On

I found the solution now, hope it is useful for others.

First update manifest file:

{
  "timeZone": "America/Los_Angeles",
  "oauthScopes": [
     "https://www.googleapis.com/auth/calendar.addons.current.event.read",
     "https://www.googleapis.com/auth/calendar.addons.current.event.write"
  ],
  "addOns": {
    "calendar": {
      "currentEventAccess": "READ_WRITE",
      "eventUpdateTrigger": {
        "runFunction": "onCalendarEventUpdate"
      },
    }
  }
}

Then in onCalendarEventUpdate function

function onCalendarEventUpdate(context) {
  const { conferenceData } = context;

  console.log('[onCalendarEventUpdate]conferenceData:', conferenceData);
}

You can get conference data here successfully

Reference Doc: https://developers.google.com/apps-script/manifest/calendar-addons