कैलेंडर कॉन्फ़्रेंस के बदलावों को सिंक किया जा रहा है

उपयोगकर्ता अपने Google Calendar इवेंट को बिना किसी परेशानी के अपडेट कर सकते हैं या उन्हें मिटा सकते हैं. अगर कोई उपयोगकर्ता कॉन्फ़्रेंस बनाने के बाद उसे अपडेट करता है, तो हो सकता है कि आपके ऐड-ऑन को कॉन्फ़्रेंस का डेटा अपडेट करके बदलाव के हिसाब से कार्रवाई करनी पड़े. अगर तीसरे पक्ष का कॉन्फ़्रेंसिंग सिस्टम, इवेंट के डेटा को ट्रैक करने पर निर्भर है, तो इवेंट में बदलाव होने पर कॉन्फ़्रेंस को अपडेट न करने पर, कॉन्फ़्रेंस बेकार हो सकती है और उपयोगकर्ता को खराब अनुभव मिल सकता है.

Google Calendar इवेंट में होने वाले बदलावों के साथ, कॉन्फ़्रेंस डेटा को अपडेट रखने की प्रोसेस को सिंक करना कहते हैं. Apps Script इंस्टॉल करने लायक ट्रिगर बनाकर, इवेंट में हुए बदलावों को सिंक किया जा सकता है. यह सुविधा किसी दिए गए कैलेंडर में इवेंट बदलने पर भी ट्रिगर होती है. माफ़ करें, ट्रिगर यह नहीं बताता कि कौनसे इवेंट में बदलाव हुए हैं और आपको सिर्फ़ अपनी बनाई गई कॉन्फ़्रेंस वाले इवेंट तक सीमित नहीं किया जा सकता. इसके बजाय, आपको पिछली बार सिंक होने के बाद से कैलेंडर में किए गए सभी बदलावों की सूची का अनुरोध करना होगा, इवेंट की सूची को फ़िल्टर करना होगा और उसके मुताबिक अपडेट करने होंगे.

सिंक करने की सामान्य प्रोसेस इस तरह है:

  1. जब कोई उपयोगकर्ता पहली बार कॉन्फ़्रेंस बनाता है, तो सिंक करने की प्रोसेस शुरू हो जाती है.
  2. जब भी उपयोगकर्ता अपना कोई कैलेंडर इवेंट बनाता है, अपडेट करता है या मिटाता है, तब ट्रिगर आपके ऐड-ऑन प्रोजेक्ट में ट्रिगर फ़ंक्शन लागू करता है.
  3. ट्रिगर फ़ंक्शन, पिछले सिंक के बाद से इवेंट में हुए बदलावों के सेट की जांच करता है और यह तय करता है कि उससे जुड़ी तीसरे-पक्ष की कॉन्फ़्रेंस को अपडेट करने की ज़रूरत है या नहीं.
  4. कॉन्फ़्रेंस में ज़रूरी अपडेट करने के लिए, तीसरे पक्ष के एपीआई अनुरोध किए जाते हैं.
  5. एक नया सिंक टोकन सेव किया जाता है, ताकि अगले ट्रिगर को एक्ज़ीक्यूट करने पर सिर्फ़ कैलेंडर में हुए सबसे हाल के बदलावों की जांच करनी हो.

सिंक की प्रोसेस शुरू करें

तीसरे पक्ष के सिस्टम पर कॉन्फ़्रेंस बनाने के बाद, ऐड-ऑन को इंस्टॉल करने लायक ट्रिगर बनाना चाहिए. यह ट्रिगर पहले से मौजूद न होने पर, इस कैलेंडर में इवेंट में हुए बदलावों के हिसाब से काम करता है.

ट्रिगर बनाने के बाद, शुरुआती सिंक टोकन बनाकर प्रोसेस शुरू किया जाना चाहिए. ऐसा सीधे ट्रिगर फ़ंक्शन को चलाकर किया जाता है.

कैलेंडर ट्रिगर बनाना

सिंक करने के लिए, आपके ऐड-ऑन को अटैच किए गए कॉन्फ़्रेंस वाले कैलेंडर इवेंट में बदलाव होने का पता लगाना होगा. इसके लिए, एक EventUpdated इंस्टॉल किया जा सकने वाला ट्रिगर बनाया जाता है. आपके ऐड-ऑन को हर कैलेंडर के लिए सिर्फ़ एक ट्रिगर की ज़रूरत होती है और उन्हें प्रोग्राम के हिसाब से बनाया जा सकता है.

ट्रिगर बनाने का सबसे अच्छा समय तब होता है, जब उपयोगकर्ता अपनी पहली कॉन्फ़्रेंस बनाता है. ऐसा तब होता है, जब उपयोगकर्ता ऐड-ऑन का इस्तेमाल शुरू करने लगता है. कॉन्फ़्रेंस बनाने और इसमें कोई गड़बड़ी न होने की पुष्टि करने के बाद, आपके ऐड-ऑन को इसकी जांच करनी चाहिए कि क्या इस उपयोगकर्ता के लिए ट्रिगर मौजूद है. साथ ही, देखें कि इस उपयोगकर्ता के लिए ट्रिगर मौजूद है या नहीं.

सिंक ट्रिगर फ़ंक्शन लागू करना

ट्रिगर फ़ंक्शन तब चलाए जाते हैं, जब Apps Script को किसी ऐसी स्थिति का पता चलता है जिसकी वजह से ट्रिगर ट्रिगर होता है. EventUpdated कैलेंडर ट्रिगर यह तब सक्रिय होता है, जब कोई उपयोगकर्ता किसी खास कैलेंडर में कोई इवेंट बनाता है, उसमें बदलाव करता है या उसे मिटाता है.

आपको वह ट्रिगर फ़ंक्शन लागू करना होगा जिसका इस्तेमाल आपका ऐड-ऑन करता है. इस ट्रिगर फ़ंक्शन को ये काम करना चाहिए:

  1. पिछली बार सिंक होने के बाद हुए इवेंट की सूची फिर से पाने के लिए, syncToken का इस्तेमाल करके Calendar की बेहतर सेवा के लिए Calendar.Events.list() कॉल करें. सिंक टोकन का इस्तेमाल करके, उन इवेंट की संख्या कम की जा सकती है जिनकी जांच आपके ऐड-ऑन को करनी होगी.

    जब ट्रिगर फ़ंक्शन किसी मान्य सिंक टोकन के बिना काम करता है, तो यह पूरी तरह सिंक पर बैक अप हो जाता है. फ़ुल सिंक का इस्तेमाल करके सभी इवेंट को एक तय विंडो में वापस पाया जा सकता है, ताकि नया और मान्य सिंक टोकन जनरेट किया जा सके.

  2. बदलाव किए गए हर इवेंट की जांच यह तय करने के लिए की जाती है कि उसमें किसी तीसरे पक्ष का कोई कॉन्फ़्रेंस जुड़ा है या नहीं.
  3. अगर किसी इवेंट में कॉन्फ़्रेंस है, तो इसकी जांच की जाती है. इससे यह पता चलता है कि क्या बदलाव हुए हैं. बदलाव के आधार पर, इससे जुड़ी कॉन्फ़्रेंस में बदलाव करने की ज़रूरत पड़ सकती है. उदाहरण के लिए, अगर कोई इवेंट मिटा दिया गया था, तो ऐड-ऑन को कॉन्फ़्रेंस भी मिटा दिया जाना चाहिए.
  4. कॉन्फ़्रेंस में ज़रूरी बदलाव करने के लिए, तीसरे पक्ष के सिस्टम को एपीआई कॉल किए जाते हैं.
  5. सभी ज़रूरी बदलाव करने के बाद, Calendar.Events.list() तरीके से लौटाए गए nextSyncToken को सेव करें. यह सिंक टोकन, Calendar.Events.list() कॉल के नतीजों के आखिरी पेज पर दिखता है.

Google Calendar इवेंट को अपडेट किया जा रहा है

कुछ मामलों में हो सकता है कि आप सिंक करते समय Google Calendar इवेंट को अपडेट करना चाहें. अगर आपको ऐसा करना है, तो Google Calendar की बेहतर सेवा का सही अनुरोध करके, इवेंट अपडेट करें. If-Match हेडर के साथ शर्तों के मुताबिक अपडेट करना का इस्तेमाल करें. इससे उपयोगकर्ता की ओर से किसी दूसरे क्लाइंट में एक साथ किए गए बदलावों को अनजाने में ओवरराइट होने से रोका जा सकता है.

उदाहरण

नीचे दिए गए उदाहरण में बताया गया है कि कैलेंडर इवेंट और उनसे जुड़ी कॉन्फ़्रेंस के लिए, सिंक करने की सुविधा कैसे सेट अप की जा सकती है.

/**
 *  Initializes syncing of conference data by creating a sync trigger and
 *  sync token if either does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function initializeSyncing(calendarId) {
  // Create a syncing trigger if it doesn't exist yet.
  createSyncTrigger(calendarId);

  // Perform an event sync to create the initial sync token.
  syncEvents({'calendarId': calendarId});
}

/**
 *  Creates a sync trigger if it does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function createSyncTrigger(calendarId) {
  // Check to see if the trigger already exists; if does, return.
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    var trigger = allTriggers[i];
    if (trigger.getTriggerSourceId() == calendarId) {
      return;
    }
  }

  // Trigger does not exist, so create it. The trigger calls the
  // 'syncEvents()' trigger function when it fires.
  var trigger = ScriptApp.newTrigger('syncEvents')
      .forUserCalendar(calendarId)
      .onEventUpdated()
      .create();
}

/**
 *  Sync events for the given calendar; this is the syncing trigger
 *  function. If a sync token already exists, this retrieves all events
 *  that have been modified since the last sync, then checks each to see
 *  if an associated conference needs to be updated and makes any required
 *  changes. If the sync token does not exist or is invalid, this
 *  retrieves future events modified in the last 24 hours instead. In
 *  either case, a new sync token is created and stored.
 *
 *  @param {Object} e If called by a event updated trigger, this object
 *      contains the Google Calendar ID, authorization mode, and
 *      calling trigger ID. Only the calendar ID is actually used here,
 *      however.
 */
function syncEvents(e) {
  var calendarId = e.calendarId;
  var properties = PropertiesService.getUserProperties();
  var syncToken = properties.getProperty('syncToken');

  var options;
  if (syncToken) {
    // There's an existing sync token, so configure the following event
    // retrieval request to only get events that have been modified
    // since the last sync.
    options = {
      syncToken: syncToken
    };
  } else {
    // No sync token, so configure to do a 'full' sync instead. In this
    // example only recently updated events are retrieved in a full sync.
    // A larger time window can be examined during a full sync, but this
    // slows down the script execution. Consider the trade-offs while
    // designing your add-on.
    var now = new Date();
    var yesterday = new Date();
    yesterday.setDate(now.getDate() - 1);
    options = {
      timeMin: now.toISOString(),          // Events that start after now...
      updatedMin: yesterday.toISOString(), // ...and were modified recently
      maxResults: 50,   // Max. number of results per page of responses
      orderBy: 'updated'
    }
  }

  // Examine the list of updated events since last sync (or all events
  // modified after yesterday if the sync token is missing or invalid), and
  // update any associated conferences as required.
  var events;
  var pageToken;
  do {
    try {
      options.pageToken = pageToken;
      events = Calendar.Events.list(calendarId, options);
    } catch (err) {
      // Check to see if the sync token was invalidated by the server;
      // if so, perform a full sync instead.
      if (err.message ===
            "Sync token is no longer valid, a full sync is required.") {
        properties.deleteProperty('syncToken');
        syncEvents(e);
        return;
      } else {
        throw new Error(err.message);
      }
    }

    // Read through the list of returned events looking for conferences
    // to update.
    if (events.items && events.items.length > 0) {
      for (var i = 0; i < events.items.length; i++) {
         var calEvent = events.items[i];
         // Check to see if there is a record of this event has a
         // conference that needs updating.
         if (eventHasConference(calEvent)) {
           updateConference(calEvent, calEvent.conferenceData.conferenceId);
         }
      }
    }

    pageToken = events.nextPageToken;
  } while (pageToken);

  // Record the new sync token.
  if (events.nextSyncToken) {
    properties.setProperty('syncToken', events.nextSyncToken);
  }
}

/**
 *  Returns true if the specified event has an associated conference
 *  of the type managed by this add-on; retuns false otherwise.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @return {boolean}
 */
function eventHasConference(calEvent) {
  var name = calEvent.conferenceData.conferenceSolution.name || null;

  // This version checks if the conference data solution name matches the
  // one of the solution names used by the add-on. Alternatively you could
  // check the solution's entry point URIs or other solution-specific
  // information.
  if (name) {
    if (name === "My Web Conference" ||
        name === "My Recorded Web Conference") {
      return true;
    }
  }
  return false;
}

/**
 *  Update a conference based on new Google Calendar event information.
 *  The exact implementation of this function is highly dependant on the
 *  details of the third-party conferencing system, so only a rough outline
 *  is shown here.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @param {String} conferenceId The ID used to identify the conference on
 *      the third-party conferencing system.
 */
function updateConference(calEvent, conferenceId) {
  // Check edge case: the event was cancelled
  if (calEvent.status === 'cancelled' || eventHasConference(calEvent)) {
    // Use the third-party API to delete the conference too.


  } else {
    // Extract any necessary information from the event object, then
    // make the appropriate third-party API requests to update the
    // conference with that information.

  }
}