سرویس رویدادهای Google Workspace پیشرفته

سرویس Advanced Google Workspace Events به شما امکان می دهد از Google Workspace Events API در Apps Script استفاده کنید. این API به شما امکان می‌دهد در منابع Google Workspace مشترک شوید تا رویدادهای مرتبط مورد علاقه خود را دریافت کنید. رویدادها تغییرات منابع را نشان می‌دهند، مانند زمانی که منابع ایجاد، به‌روزرسانی یا حذف می‌شوند.

پیش نیازها

  • یک پروژه Apps Script با استفاده از یک پروژه استاندارد Google Cloud به جای پروژه پیش‌فرض ایجاد شده به‌طور خودکار توسط Apps Script.
  • موضوع Pub/Sub ایجاد شده در همان پروژه Google Cloud برای دریافت رویدادهای اشتراک. برای ایجاد یک موضوع Pub/Sub، به ایجاد و اشتراک در یک موضوع Pub/Sub مراجعه کنید.
  • برای اشتراک در رویدادهای Chat، باید یک برنامه Google Chat را در صفحه پیکربندی Chat API در کنسول Google Cloud پیکربندی کنید. برای ایجاد یک برنامه Google Chat، به ساخت برنامه Google Chat با برنامه‌های اسکریپت مراجعه کنید.
  • محدوده های مجوز لازم به فایل appsscript.json پروژه Apps Script اضافه شده است. دامنه های لازم به انواع منابع و رویدادهای هدف اشتراک ها بستگی دارد. برای جزئیات، به انتخاب حوزه‌های API رویدادهای Google Workspace مراجعه کنید. مثلا:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.messages.readonly"
    ]
    

ارجاع

برای اطلاعات بیشتر درباره این سرویس، به مستندات مرجع Google Workspace Events API مراجعه کنید. مانند همه سرویس‌های پیشرفته در Apps Script، سرویس رویدادهای Google Workspace از همان اشیا، روش‌ها و پارامترهای API عمومی استفاده می‌کند.

کد نمونه

این نمونه‌ها به شما نشان می‌دهند که چگونه با استفاده از سرویس پیشرفته، اقدامات متداول Google Workspace Events API را انجام دهید.

یک اشتراک ایجاد کنید

برای ایجاد اشتراک در یک منبع Google Workspace، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Creates a subscription to receive events about a Google Workspace resource.
 * For a list of supported resources and event types, see the
 * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).
 * For additional information, see the
 * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)
 * method reference.
 * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.
 * @param {!string|!Array<string>} eventTypes The types of events to receive about the resource.
 * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.
 */
function createSubscription(targetResource, eventTypes, pubsubTopic) {
  try {
    const operation = WorkspaceEvents.Subscriptions.create({
      targetResource: targetResource,
      eventTypes: eventTypes,
      notificationEndpoint: {
        pubsubTopic: pubsubTopic,
      },
    });
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to create subscription with error %s', err.message);
  }
}

فهرست اشتراک ها

برای فهرست کردن اشتراک‌های فیلتر شده بر اساس انواع رویداد و منبع هدف، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.
 * For additional information, see the
 * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)
 * method reference.
 * @param {!string} filter The query filter.
 */
function listSubscriptions(filter) {
  try {
    const response = WorkspaceEvents.Subscriptions.list({ filter });
    console.log(response);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to list subscriptions with error %s', err.message);
  }
}

دریافت اشتراک

برای دریافت اطلاعات در مورد اشتراک، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Gets details about a subscription.
 * For additional information, see the
 * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function getSubscription(name) {
  try {
    const subscription = WorkspaceEvents.Subscriptions.get(name);
    console.log(subscription);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get subscription with error %s', err.message);
  }
}

اشتراک را به روز کنید

برای به‌روزرسانی یا تمدید اشتراک، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Updates an existing subscription.
 * This can be used to renew a subscription that is about to expire.
 * For additional information, see the
 * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function patchSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.patch({
      // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.
      ttl: '0s',
    }, name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to update subscription with error %s', err.message);
  }
}

اشتراک را دوباره فعال کنید

برای فعال کردن مجدد اشتراک، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Reactivates a suspended subscription.
 * Before reactivating, you must resolve any errors with the subscription.
 * For additional information, see the
 * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function reactivateSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to reactivate subscription with error %s', err.message);
  }
}

حذف اشتراک

برای حذف اشتراک، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Deletes a subscription.
 * For additional information, see the
 * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function deleteSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.remove(name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to delete subscription with error %s', err.message);
  }
}

عملیات را انجام دهید

اکثر روش‌های Google Workspace Events API یک عملیات طولانی‌مدت را برمی‌گردانند. برای تعیین وضعیت عملیات، می توانید از متد operations.get() استفاده کنید.

برای دریافت اطلاعات در مورد یک عملیات، تابع زیر را به کد پروژه Apps Script اضافه کنید:

/**
 * Gets details about an operation returned by one of the methods on the subscription
 * resource of the Google Workspace Events API.
 * For additional information, see the
 * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)
 * method reference.
 * @param {!string} name The resource name of the operation.
 */
function getOperation(name) {
  try {
    const operation = WorkspaceEvents.Operations.get(name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get operation with error %s', err.message);
  }
}

برای دریافت نام یک عملیات، از مقدار فیلد name که از یکی از متدهای API رویدادهای Google Workspace، مانند subscriptions.create() یا subscriptions.patch() بازگردانده شده است استفاده کنید.