Dịch vụ sự kiện nâng cao của Google Workspace

Dịch vụ Sự kiện nâng cao của Google Workspace cho phép bạn sử dụng Google Workspace Events API trong Apps Script. API này cho phép bạn đăng ký nhận các tài nguyên trên Google Workspace để nhận được các sự kiện liên quan mà bạn quan tâm. Sự kiện biểu thị những thay đổi đối với tài nguyên, chẳng hạn như khi tài nguyên được tạo, cập nhật hoặc xoá.

Điều kiện tiên quyết

  • Một dự án Apps Script sử dụng dự án Google Cloud tiêu chuẩn thay vì dự án mặc định do Apps Script tự động tạo.
  • Một chủ đề Pub/Sub được tạo trong cùng một dự án Google Cloud để nhận các sự kiện đăng ký. Để tạo một chủ đề Pub/Sub, hãy xem phần Tạo và đăng ký theo dõi một chủ đề Pub/Sub.
  • Để đăng ký nhận sự kiện Chat, bạn phải có một ứng dụng Google Chat được định cấu hình trên trang cấu hình Chat API trong Google Cloud Console. Để tạo một ứng dụng Google Chat, hãy xem bài viết Tạo ứng dụng Google Chat bằng Apps Script.
  • Các phạm vi uỷ quyền cần thiết được thêm vào tệp appsscript.json của dự án Apps Script. Các phạm vi cần thiết phụ thuộc vào các loại tài nguyên và sự kiện mục tiêu của các sự kiện đăng ký. Để biết thông tin chi tiết, hãy xem bài viết Chọn phạm vi của Events API trong Google Workspace. Ví dụ:

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

Tài liệu tham khảo

Để biết thêm thông tin về dịch vụ này, hãy xem Tài liệu tham khảo về Google Workspace Events API. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Google Workspace Events sử dụng cùng các đối tượng, phương thức và tham số như API công khai.

Mã mẫu

Các mẫu này cho thấy cách thực hiện các thao tác phổ biến của Google Workspace Events API bằng dịch vụ nâng cao.

Tạo gói thuê bao

Để tạo một lượt đăng ký nhận thông báo về một tài nguyên trên Google Workspace, hãy thêm hàm sau vào mã của dự án 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);
  }
}

Danh sách gói thuê bao

Để liệt kê các sự kiện được lọc theo loại sự kiện và tài nguyên đích, hãy thêm hàm sau vào mã của dự án 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);
  }
}

Nhận gói thuê bao

Để lấy thông tin về một gói thuê bao, hãy thêm hàm sau vào mã của dự án Apps Script:

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);
  }
}

Cập nhật đăng ký

Để cập nhật hoặc gia hạn gói thuê bao, hãy thêm hàm sau vào mã của dự án 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);
  }
}

Kích hoạt lại gói thuê bao

Để kích hoạt lại một gói thuê bao, hãy thêm hàm sau vào mã của dự án 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);
  }
}

Xoá gói thuê bao

Để xoá một gói thuê bao, hãy thêm hàm sau vào mã của dự án 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);
  }
}

Nhận thao tác

Hầu hết các phương thức của Google Workspace Events API đều trả về một thao tác diễn ra trong thời gian dài. Để xác định trạng thái của thao tác, bạn có thể sử dụng phương thức operations.get().

Để biết thông tin về một thao tác, hãy thêm hàm sau vào mã của dự án Apps Script:

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);
  }
}

Để lấy tên của một thao tác, hãy sử dụng giá trị trong trường name do một trong các phương thức của Google Workspace Events API trả về, chẳng hạn như subscriptions.create() hoặc subscriptions.patch().