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

Apps Script để đăng ký nhận tài nguyên Google Workspace và nhận thông báo về sự kiện

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

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

  • Một dự án Apps Script sử dụng dự án trên đám mây Google 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 trên đám mây của Google để nhận các sự kiện đăng ký. Để tạo một chủ đề Pub/Sub, hãy xem bài viết Tạo và đăng ký nhận 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 định cấu hình API Chat trong bảng điều khiển Cloud của Google. Để 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 loại tài nguyên và sự kiện mục tiêu của gói thuê bao. Để biết thông tin chi tiết, hãy xem bài viết Chọn phạm vi API Sự kiện Google Workspace. Ví dụ:

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

Đây là một dịch vụ nâng cao mà bạn phải bật trước khi sử dụng.

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 API Sự kiện Google Workspace. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Sự kiện Google Workspace 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 API Sự kiện Google Workspace phổ biến 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 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);
  }
}

Nhận gói thuê bao

Để nhận 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 gói thuê bao

Để 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á 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 Google Workspace đều trả về một thao tác chạy 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 operations.get() phương thức.

Để nhận 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);
  }
}

Để nhận 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 Google Workspace, chẳng hạn như subscriptions.create() hoặc subscriptions.patch().