Zaawansowana usługa wydarzeń Google Workspace

Usługa zaawansowanych zdarzeń Google Workspace umożliwia korzystanie z interfejsu Google Workspace Events API w Google Apps Script. Ten interfejs API pozwala subskrybować zasoby Google Workspace, aby otrzymywać odpowiednie zdarzenia, które Cię interesują. Zdarzenia reprezentują zmiany w zasobach, np. ich utworzenie, zaktualizowanie lub usunięcie.

Wymagania wstępne

  • Projekt Apps Script, który korzysta ze standardowego projektu Google Cloud zamiast domyślnego projektu utworzonego automatycznie przez Apps Script.
  • Temat Pub/Sub utworzony w tym samym projekcie w chmurze Google Cloud, aby otrzymywać zdarzenia subskrypcji. Aby utworzyć temat Pub/Sub, zapoznaj się z artykułem Tworzenie tematu Pub/Sub i subskrybowanie go.
  • Aby subskrybować zdarzenia w Google Chat, musisz mieć aplikację do obsługi czatu skonfigurowaną na stronie konfiguracji interfejsu Chat API w konsoli Google Cloud. Aby utworzyć aplikację Google Chat, zapoznaj się z artykułem Tworzenie aplikacji Google Chat za pomocą Apps Script.
  • Niezbędne zakresy autoryzacji dodane do pliku appsscript.json projektu Apps Script. Wymagane zakresy zależą od typów zasobów docelowych i zdarzeń subskrypcji. Więcej informacji znajdziesz w artykule Wybieranie zakresów interfejsu Google Workspace Events API. Na przykład:

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

Jest to usługa zaawansowana, którą musisz włączyć przed użyciem.

Dokumentacja

Więcej informacji o tej usłudze znajdziesz w dokumentacji API Google Workspace Events API. Podobnie jak wszystkie usługi zaawansowane w Apps Script, usługa Google Workspace Events używa tych samych obiektów, metod i parametrów co publiczny interfejs API.

Przykładowy kod

Te przykłady pokazują, jak wykonywać typowe działania interfejsu Google Workspace Events API za pomocą usługi zaawansowanej.

Tworzenie subskrypcji

Aby utworzyć subskrypcję zasobu Google Workspace, dodaj tę funkcję do kodu projektu 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);
  }
}

Wyświetlanie listy subskrypcji

Aby wyświetlić listę subskrypcji przefiltrowaną według typów zdarzeń i zasobu docelowego, dodaj tę funkcję do kodu projektu 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);
  }
}

Pobieranie subskrypcji

Aby uzyskać informacje o subskrypcji, dodaj tę funkcję do kodu projektu 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);
  }
}

Aktualizowanie subskrypcji

Aby zaktualizować lub odnowić subskrypcję, dodaj tę funkcję do kodu projektu 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);
  }
}

Wznawianie subskrypcji

Aby ponownie aktywować subskrypcję, dodaj tę funkcję do kodu projektu 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);
  }
}

Usuwanie subskrypcji

Aby usunąć subskrypcję, dodaj tę funkcję do kodu projektu 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);
  }
}

Pobranie operacji

Większość metod interfejsu Google Workspace Events API zwraca a długo trwającą operację. Aby określić stan operacji, możesz użyć operations.get() metody.

Aby uzyskać informacje o operacji, dodaj tę funkcję do kodu projektu 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);
  }
}

Aby uzyskać nazwę operacji, użyj wartości z pola name zwróconej przez jedną z metod interfejsu Google Workspace Events API, np. subscriptions.create() lub subscriptions.patch().