خدمة أحداث Google Workspace المتقدّمة

تتيح لك خدمة "أحداث Google Workspace المتقدّمة" استخدام واجهة برمجة تطبيقات أحداث Google Workspace في "برمجة تطبيقات Google". تتيح لك واجهة برمجة التطبيقات هذه الاشتراك في موارد Google Workspace لتلقّي الأحداث ذات الصلة التي تهمّك. تمثّل الأحداث التغييرات التي تطرأ على الموارد، مثل وقت إنشاء الموارد أو تعديلها أو حذفها.

المتطلبات الأساسية

  • مشروع "برمجة تطبيقات Google" يستخدم مشروعًا عاديًا على Google Cloud بدلاً من المشروع التلقائي الذي يتم إنشاؤه تلقائيًا بواسطة "برمجة تطبيقات Google".
  • موضوع Pub/Sub تم إنشاؤه في مشروع Google Cloud نفسه لتلقّي أحداث الاشتراك لإنشاء موضوع في Pub/Sub، راجِع مقالة إنشاء موضوع في Pub/Sub والاشتراك فيه.
  • للاشتراك في أحداث Chat، يجب أن يكون لديك تطبيق Google Chat تم إعداده في صفحة إعدادات Chat API في Google Cloud Console. لإنشاء تطبيق Google Chat، يُرجى الاطّلاع على المقالة إنشاء تطبيق Google Chat باستخدام "برمجة تطبيقات Google".
  • نطاقات التفويض اللازمة التي تمت إضافتها إلى ملف appsscript.json في مشروع Apps Script تعتمد النطاقات اللازمة على أنواع الموارد والأحداث المستهدَفة للاشتراكات. لمعرفة التفاصيل، يُرجى الاطّلاع على اختيار نطاقات "واجهة برمجة تطبيقات أحداث Google Workspace". على سبيل المثال:

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

مراجع

لمزيد من المعلومات حول هذه الخدمة، يُرجى الاطّلاع على مستندات مرجع Google Workspace Events API. مثل جميع الخدمات المتقدّمة في "برمجة التطبيقات"، تستخدم خدمة Google Workspace Events الكائنات والطُرق والمَعلمات نفسها المستخدَمة في واجهة برمجة التطبيقات العامة.

نموذج التعليمات البرمجية

توضّح لك هذه النماذج كيفية تنفيذ الإجراءات الشائعة في Google Workspace Events API باستخدام الخدمة المتقدّمة.

إنشاء اشتراك

لإنشاء اشتراك في أحد موارد Google Workspace، أضِف الدالة التالية إلى رمز مشروع Apps Script:

advanced/events.gs
/**
 * 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:

advanced/events.gs
/**
 * 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);
  }
}

الحصول على اشتراك

للحصول على معلومات حول اشتراك، أضِف الدالة التالية إلى رمز مشروع "برمجة التطبيقات":

advanced/events.gs
/**
 * 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:

advanced/events.gs
/**
 * 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:

advanced/events.gs
/**
 * 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:

advanced/events.gs
/**
 * 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" عملية تستغرق وقتًا طويلاً. لتحديد حالة العملية، يمكنك استخدام طريقة operations.get().

للحصول على معلومات حول عملية ما، أضِف الدالة التالية إلى رمز مشروع &quot;برمجة التطبيقات&quot;:

advanced/events.gs
/**
 * 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 الذي تم عرضه من إحدى طرق Google Workspace Events API، مثل subscriptions.create() أو subscriptions.patch().