ব্যবহারকারীরা তাদের গুগল ক্যালেন্ডারের ইভেন্টগুলো আপডেট বা ডিলিট করতে পারেন। যদি কোনো ব্যবহারকারী একটি কনফারেন্স তৈরি করার পর সেই ইভেন্টটি আপডেট করেন, তাহলে আপনার গুগল ওয়ার্কস্পেস অ্যাড-অনটিকে কনফারেন্সের ডেটা আপডেট করার মাধ্যমে সেই পরিবর্তনের সাথে সাড়া দিতে হতে পারে। যদি আপনার থার্ড-পার্টি কনফারেন্সিং সিস্টেমটি ইভেন্টের ডেটা ট্র্যাক করার উপর নির্ভরশীল হয়, তবে ইভেন্টের কোনো পরিবর্তনে কনফারেন্সটি আপডেট করতে ব্যর্থ হলে সেটি ব্যবহার অযোগ্য হয়ে যেতে পারে।
ক্যালেন্ডার ইভেন্টের পরিবর্তনের সাথে কনফারেন্স ডেটা আপডেট রাখার প্রক্রিয়াকে সিঙ্কিং বলা হয়। একটি গুগল অ্যাপস স্ক্রিপ্ট ইনস্টলেবল ট্রিগার তৈরি করে ইভেন্টের পরিবর্তন সিঙ্ক করুন, যা একটি নির্দিষ্ট ক্যালেন্ডারে ইভেন্ট পরিবর্তিত হলেই চালু হয়। ট্রিগারটি কোন ইভেন্টগুলো পরিবর্তিত হয়েছে তা রিপোর্ট করে না এবং আপনি এটিকে শুধুমাত্র আপনার তৈরি করা কনফারেন্সযুক্ত ইভেন্টগুলোর মধ্যে সীমাবদ্ধ রাখতে পারবেন না। শেষ সিঙ্কের পর থেকে ক্যালেন্ডারে করা সমস্ত পরিবর্তনের একটি তালিকা অনুরোধ করুন, ইভেন্টের তালিকাটি ফিল্টার করুন এবং সেই অনুযায়ী আপডেট করুন।
সাধারণ সিঙ্কিং পদ্ধতিটি নিম্নরূপ:
- কোনো ব্যবহারকারী প্রথমবার কনফারেন্স তৈরি করলে, সিঙ্ক প্রক্রিয়াটি শুরু হয়।
- যখনই ব্যবহারকারী তার ক্যালেন্ডারের কোনো ইভেন্ট তৈরি, আপডেট বা মুছে ফেলেন, তখন ট্রিগারটি আপনার অ্যাড-অন প্রজেক্টের একটি ট্রিগার ফাংশন কার্যকর করে।
- ট্রিগার ফাংশনটি শেষ সিঙ্কের পর থেকে হওয়া ইভেন্ট পরিবর্তনগুলো পরীক্ষা করে এবং নির্ধারণ করে যে সেগুলোর কোনোটির জন্য সংশ্লিষ্ট তৃতীয় পক্ষের কনফারেন্স আপডেট করার প্রয়োজন আছে কিনা।
- তৃতীয় পক্ষের এপিআই অনুরোধ পাঠানোর মাধ্যমে কনফারেন্সগুলোতে প্রয়োজনীয় আপডেট করা হয়।
- একটি নতুন সিঙ্ক টোকেন সংরক্ষণ করা হয়, যাতে পরবর্তী ট্রিগার নির্বাহের সময় ক্যালেন্ডারের শুধুমাত্র সাম্প্রতিকতম পরিবর্তনগুলোই পরীক্ষা করা হয়।
সিঙ্কিং শুরু করুন
প্রারম্ভিক প্রক্রিয়াটি নিশ্চিত করে যে আপনার অ্যাড-অনটি ক্যালেন্ডারের পরিবর্তনে সাড়া দেওয়ার জন্য প্রস্তুত। অ্যাড-অনটি কোনো তৃতীয় পক্ষের সিস্টেমে সফলভাবে একটি কনফারেন্স তৈরি করার পর, যদি ট্রিগারটি আগে থেকে বিদ্যমান না থাকে, তবে এটি এই ক্যালেন্ডারের ইভেন্টের পরিবর্তনে সাড়া দেওয়ার জন্য একটি ইনস্টলযোগ্য ট্রিগার তৈরি করবে।
ট্রিগার তৈরি করার পর, প্রাথমিক সিঙ্ক টোকেন তৈরির মাধ্যমে ইনিশিয়ালাইজেশন সম্পন্ন করা উচিত। এটি সরাসরি ট্রিগার ফাংশনটি এক্সিকিউট করার মাধ্যমে করা হয়।
একটি ক্যালেন্ডার ট্রিগার তৈরি করুন
সিঙ্ক করার জন্য, আপনার অ্যাড-অনকে শনাক্ত করতে হবে কখন একটি ক্যালেন্ডার ইভেন্ট, যার সাথে একটি কনফারেন্স সংযুক্ত আছে, পরিবর্তিত হয়। এটি একটি EventUpdated ইনস্টলযোগ্য ট্রিগার তৈরি করার মাধ্যমে সম্পন্ন করা হয়। আপনার অ্যাড-অনের প্রতিটি ক্যালেন্ডারের জন্য কেবল একটি ট্রিগার প্রয়োজন, এবং এটি প্রোগ্রাম্যাটিকভাবে সেগুলো তৈরি করতে পারে।
ট্রিগার তৈরি করার জন্য প্রস্তাবিত সময় হলো যখন ব্যবহারকারী তার প্রথম কনফারেন্স তৈরি করেন, কারণ সেই মুহূর্তে ব্যবহারকারী অ্যাড-অনটি ব্যবহার করা শুরু করেন। একটি কনফারেন্স তৈরি করার পর এবং কোনো ত্রুটি নেই তা যাচাই করার পর, আপনার অ্যাড-অনটির উচিত এই ব্যবহারকারীর জন্য ট্রিগারটি বিদ্যমান আছে কিনা তা পরীক্ষা করা এবং না থাকলে তা তৈরি করে দেওয়া।
ট্রিগার তৈরি করার জন্য অ্যাড-অনটিতে https://www.googleapis.com/auth/calendar.readonly এবং https://www.googleapis.com/auth/script.scriptapp স্কোপগুলো থাকা আবশ্যক।
একটি সিঙ্ক ট্রিগার ফাংশন বাস্তবায়ন করুন
যখন অ্যাপস স্ক্রিপ্ট এমন কোনো শর্ত শনাক্ত করে যা একটি ট্রিগারকে সক্রিয় করে, তখন ট্রিগার ফাংশনগুলো কার্যকর হয়। যখন কোনো ব্যবহারকারী একটি নির্দিষ্ট ক্যালেন্ডারে কোনো ইভেন্ট তৈরি, পরিবর্তন বা মুছে ফেলেন, তখন EventUpdated ক্যালেন্ডার ট্রিগারগুলো সক্রিয় হয়।
আপনার অ্যাড-অন যে ট্রিগার ফাংশনটি ব্যবহার করে, তা বাস্তবায়ন করুন। এই ট্রিগার ফাংশনটির নিম্নলিখিত কাজগুলো করা উচিত:
শেষ সিঙ্কের পর থেকে পরিবর্তিত হওয়া ইভেন্টগুলির একটি তালিকা পেতে, একটি
syncTokenব্যবহার করে ক্যালেন্ডারের অ্যাডভান্সড সার্ভিসCalendar.Events.listকল করুন। সিঙ্ক টোকেন ব্যবহার করার মাধ্যমে, আপনার অ্যাড-অনকে যে সংখ্যক ইভেন্ট পরীক্ষা করতে হয়, তা কমে যায়।যখন ট্রিগার ফাংশনটি একটি বৈধ সিঙ্ক টোকেন ছাড়া কার্যকর হয়, তখন এটি একটি সম্পূর্ণ সিঙ্কে ফিরে যায়। সম্পূর্ণ সিঙ্ক একটি নতুন, বৈধ সিঙ্ক টোকেন তৈরি করার জন্য একটি নির্দিষ্ট সময়সীমার মধ্যে সমস্ত ইভেন্ট পুনরুদ্ধার করার চেষ্টা করে।
প্রতিটি পরিবর্তিত ইভেন্টের সাথে কোনো তৃতীয় পক্ষের সম্মেলন যুক্ত আছে কিনা তা নির্ধারণ করার জন্য পরীক্ষা করা হয়।
যদি কোনো ইভেন্টের একটি কনফারেন্স থাকে, তবে কী পরিবর্তন করা হয়েছে তা খতিয়ে দেখা হয়। পরিবর্তনের ধরনের ওপর নির্ভর করে, সংশ্লিষ্ট কনফারেন্সটির সংশোধনের প্রয়োজন হতে পারে। উদাহরণস্বরূপ, যদি কোনো ইভেন্ট মুছে ফেলা হয়, তবে অ্যাড-অনটির উচিত কনফারেন্সটিও মুছে দেওয়া।
কনফারেন্সে প্রয়োজনীয় যেকোনো পরিবর্তন তৃতীয় পক্ষের সিস্টেমে এপিআই কল করার মাধ্যমে করা হয়।
সমস্ত প্রয়োজনীয় পরিবর্তন করার পর,
Calendar.Events.listমেথড দ্বারা ফেরত আসাnextSyncTokenটি সংরক্ষণ করুন। এই সিঙ্ক টোকেনটিCalendar.Events.listকল দ্বারা ফেরত আসা ফলাফলের শেষ পৃষ্ঠায় পাওয়া যায়।
ক্যালেন্ডার ইভেন্ট আপডেট করুন
কিছু ক্ষেত্রে সিঙ্ক করার সময় আপনি ক্যালেন্ডার ইভেন্টটি আপডেট করতে চাইতে পারেন। যদি আপনি এটি করতে চান, তবে উপযুক্ত ক্যালেন্ডার অ্যাডভান্সড সার্ভিস রিকোয়েস্টের মাধ্যমে ইভেন্টটি আপডেট করুন। অবশ্যই 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. } }