Erweiterter Google Workspace-Ereignisdienst

Mit dem erweiterten Google Workspace-Ereignisdienst können Sie die Google Workspace Events API in Apps Script verwenden. Mit dieser API können Sie Google Workspace-Ressourcen abonnieren, um relevante Ereignisse zu erhalten, die Sie interessieren. Ereignisse stellen Änderungen an Ressourcen dar, z. B. beim Erstellen, Aktualisieren oder Löschen von Ressourcen.

Voraussetzungen

  • Ein Apps Script-Projekt, das ein Standard-Google Cloud-Projekt anstelle des von Apps Script automatisch erstellten Standardprojekts verwendet
  • Ein im selben Google Cloud-Projekt erstelltes Pub/Sub-Thema, um Aboereignisse zu empfangen. Informationen zum Erstellen eines Pub/Sub-Themas finden Sie unter Pub/Sub-Thema erstellen und abonnieren.
  • Wenn Sie Chat-Ereignisse abonnieren möchten, muss in der Google Cloud Console auf der Konfigurationsseite der Chat API eine Google Chat-App konfiguriert sein. Informationen zum Erstellen einer Google Chat-App finden Sie unter Google Chat-App mit Apps Script erstellen.
  • Die erforderlichen Autorisierungsbereiche, die der Datei appsscript.json des Apps Script-Projekts hinzugefügt wurden. Die erforderlichen Bereiche hängen von den Typen der Zielressourcen und -ereignisse der Abos ab. Weitere Informationen finden Sie unter Google Workspace Events API-Bereiche auswählen. Beispiel:

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

Referenz

Weitere Informationen zu diesem Dienst finden Sie in der Referenzdokumentation zur Google Workspace Events API. Wie alle erweiterten Dienste in Apps Script verwendet der Google Workspace-Ereignisdienst dieselben Objekte, Methoden und Parameter wie die öffentliche API.

Beispielcode

Diese Beispiele zeigen, wie Sie gängige Aktionen der Google Workspace Events API mit dem erweiterten Dienst ausführen.

Abo erstellen

Fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu, um ein Abo für eine Google Workspace-Ressource zu erstellen:

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

Abos auflisten

Wenn Sie Abos auflisten möchten, die nach Ereignistypen und Zielressource gefiltert sind, fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu:

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

Abo abrufen

Wenn Sie Informationen zu einem Abo erhalten möchten, fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu:

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

Abo aktualisieren

Fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu, um ein Abo zu aktualisieren oder zu verlängern:

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

Abo reaktivieren

Fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu, um ein Abo wieder zu aktivieren:

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

Abo löschen

Fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu, um ein Abo zu löschen:

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

Vorgang abrufen

Die meisten Google Workspace Events API-Methoden geben einen Vorgang mit langer Ausführungszeit zurück. Sie können den Status des Vorgangs mit der Methode operations.get() ermitteln.

Wenn Sie Informationen zu einem Vorgang abrufen möchten, fügen Sie dem Code des Apps Script-Projekts die folgende Funktion hinzu:

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

Verwenden Sie zum Abrufen des Namens eines Vorgangs den Wert aus dem Feld name, der von einer der Google Workspace Events API-Methoden wie subscriptions.create() oder subscriptions.patch() zurückgegeben wird.