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

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

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

सिंक करने की सामान्य प्रोसेस नीचे दी गई है:

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

सिंक करना शुरू करें

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

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

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

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

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

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

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

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

  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.

  }
}