บริการกิจกรรมขั้นสูงของ Google Workspace

บริการเหตุการณ์ขั้นสูงของ Google Workspace ช่วยให้คุณใช้ 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 Console หากต้องการสร้างแอป Google Chat โปรดดูสร้างแอป Google Chat ด้วย Apps Script
  • เพิ่มขอบเขตการให้สิทธิ์ที่จำเป็นลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script ขอบเขตที่จำเป็นจะขึ้นอยู่กับประเภทของ ทรัพยากรเป้าหมายและเหตุการณ์ของการสมัครใช้บริการ โปรดดูรายละเอียดที่หัวข้อ เลือกขอบเขต Google Workspace Events API เช่น

    "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

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

รับการสมัครใช้บริการ

หากต้องการดูข้อมูลเกี่ยวกับการสมัครใช้บริการ ให้เพิ่มฟังก์ชันต่อไปนี้ลงในโค้ดของโปรเจ็กต์ 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);
  }
}

อัปเดตการสมัครใช้บริการ

หากต้องการอัปเดตหรือต่ออายุการสมัครรับข้อมูล ให้เพิ่มฟังก์ชันต่อไปนี้ลงในโค้ดของโปรเจ็กต์ 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 Events API จะแสดงผลการดำเนินการที่ใช้เวลานาน หากต้องการตรวจสอบสถานะของการดำเนินการ ให้ใช้วิธี operations.get()

หากต้องการดูข้อมูลเกี่ยวกับการดำเนินการ ให้เพิ่มฟังก์ชันต่อไปนี้ ลงในโค้ดของโปรเจ็กต์ 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);
  }
}

หากต้องการดูชื่อของการดำเนินการ ให้ใช้ค่าจากฟิลด์ name ที่ส่งคืน จากเมธอด Google Workspace Events API อย่างใดอย่างหนึ่ง เช่น subscriptions.create() หรือ subscriptions.patch()