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

Dịch vụ Sự kiện nâng cao trên Google Workspace cho phép bạn sử dụng API Sự kiện của Google Workspace trong Apps Script. API này cho phép bạn đăng ký tài nguyên trên Google Workspace để nhận các sự kiện liên quan mà mình quan tâm. Sự kiện biểu thị các 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 bị xoá.

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

  • Một dự án Apps Script dùng một dự án Google Cloud chuẩn thay vì dự án mặc định do Apps Script tạo tự động.
  • Một chủ đề Pub/Sub được tạo trong cùng một dự án Google Cloud để nhận các sự kiện liên quan đến gói thuê bao. Để tạo chủ đề Pub/Sub, hãy xem phần Tạo và đăng ký chủ đề Pub/Sub.
  • Để đăng ký nhận các sự kiện trên Chat, bạn phải thiết lập ứng dụng Google Chat trên trang cấu hình API Chat trong bảng điều khiển Google Cloud. Để tạo ứng dụng Google Chat, hãy xem phần 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. Phạm vi cần thiết phụ thuộc vào loại sự kiện và tài nguyên mục tiêu của gói thuê bao. Để biết thông tin chi tiết, hãy xem phần Chọn phạm vi API Sự kiện 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ề API Sự kiện của Google Workspace. Giống như tất cả dịch vụ nâng cao trong Apps Script, dịch vụ Sự kiện của Google Workspace cũng sử dụng các đối tượng, phương thức và tham số giống như API công khai.

Mã mẫu

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

Tạo gói thuê bao

Để tạo gói thuê bao cho 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);
  }
}

Liệt kê gói thuê bao

Để liệt kê các gói thuê bao được lọc theo loại sự kiện và tài nguyên mục tiêu, 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);
  }
}

Mua 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 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 API Sự kiện của Google Workspace đều trả về một hoạt động diễn ra trong thời gian dài. Để xác định trạng thái của hoạt động, bạn có thể sử dụng phương thức operations.get().

Để lấy 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ị từ trường name được trả về từ một trong các phương thức API Sự kiện của Google Workspace, chẳng hạn như subscriptions.create() hoặc subscriptions.patch().