高度な Google Workspace イベント サービス

Advanced Google Workspace Events サービスを使用すると、Apps Script で Google Workspace Events API を使用できます。この API を使用すると、関心のある関連イベントを受信できるように、Google Workspace リソースに登録できます。イベントは、リソースの作成、更新、削除など、リソースに対する変更を表します。

前提条件

  • Apps Script によって自動的に作成されるデフォルトのプロジェクトではなく、標準の Google Cloud プロジェクトを使用する Apps Script プロジェクト。
  • サブスクリプション イベントを受信するために同じ Google Cloud プロジェクトに作成された Pub/Sub トピック。Pub/Sub トピックを作成するには、Pub/Sub トピックを作成してサブスクライブするをご覧ください。
  • Chat イベントに登録するには、Google Cloud コンソールの Chat API 構成ページで Google Chat アプリが構成されている必要があります。Google Chat アプリを作成するには、Apps Script を使用して Google Chat アプリを作成するをご覧ください。
  • Apps Script プロジェクトの appsscript.json ファイルに追加された必要な承認スコープ。必要なスコープは、サブスクリプションのターゲット リソースとイベントのタイプによって異なります。詳しくは、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);
  }
}

オペレーションの名前を取得するには、Google Workspace Events API メソッド(subscriptions.create()subscriptions.patch() など)のいずれかから返された name フィールドの値を使用します。