Gelişmiş Google Workspace Etkinlikleri Hizmeti

Gelişmiş Google Workspace Etkinlikleri hizmeti, Apps Komut Dosyası'nda Google Workspace Etkinlikleri API'sini kullanmanızı sağlar. Bu API, Google Workspace kaynaklarına abone olmanızı sağlar. Böylece, ilgilendiğiniz ilgili etkinlikleri alabilirsiniz. Etkinlikler, kaynaklarda yapılan değişiklikleri (ör. kaynakların oluşturulması, güncellenmesi veya silinmesi) temsil eder.

Ön koşullar

  • Apps Komut Dosyası tarafından otomatik olarak oluşturulan varsayılan proje yerine standart bir Google Cloud projesi kullanan bir Apps Komut Dosyası projesi.
  • Abonelik etkinliklerini almak için aynı Google Cloud projesinde oluşturulan bir Pub/Sub konusu. Pub/Sub konusu oluşturmak için Pub/Sub konusu oluşturma ve konuya abone olma bölümüne göz atın.
  • Chat etkinliklerine abone olmak için Google Cloud Console'daki Chat API yapılandırma sayfasında yapılandırılmış bir Google Chat uygulamanız olmalıdır. Google Chat uygulaması oluşturmak için Apps Komut Dosyası ile Google Chat uygulaması oluşturma başlıklı makaleyi inceleyin.
  • Apps Komut Dosyası projesinin appsscript.json dosyasına gerekli yetkilendirme kapsamları eklendi. Gerekli kapsamlar, aboneliklerin hedef kaynak ve etkinlik türlerine bağlıdır. Ayrıntılar için Google Workspace Etkinlikleri API kapsamlarını seçme bölümüne bakın. Örneğin:

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

Referans

Bu hizmet hakkında daha fazla bilgi edinmek için Google Workspace Etkinlikleri API'si referans belgelerine bakın. Apps Komut Dosyası'ndaki tüm gelişmiş hizmetler gibi Google Workspace Etkinlikleri de herkese açık API ile aynı nesneleri, yöntemleri ve parametreleri kullanır.

Örnek kod

Bu örnekler, gelişmiş hizmeti kullanarak sık yapılan Google Workspace Events API işlemlerini nasıl gerçekleştireceğinizi gösterir.

Abonelik oluşturma

Bir Google Workspace kaynağına abonelik oluşturmak için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Abonelikleri listeleme

Etkinlik türlerine ve hedef kaynağa göre filtrelenen abonelikleri listelemek için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Abonelik alma

Bir abonelik hakkında bilgi almak için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Abonelik güncelleme

Bir aboneliği güncellemek veya yenilemek için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Aboneliği yeniden etkinleştir

Bir aboneliği yeniden etkinleştirmek için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Abonelik silme

Bir aboneliği silmek için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

İşlem alın

Çoğu Google Workspace Etkinlikleri API yöntemi uzun süreli işlem döndürür. İşlemin durumunu belirlemek için operations.get() yöntemini kullanabilirsiniz.

Bir işlem hakkında bilgi almak için Apps Komut Dosyası projesinin koduna aşağıdaki işlevi ekleyin:

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

Bir işlemin adını almak için, subscriptions.create() veya subscriptions.patch() gibi Google Workspace Etkinlikleri API yöntemlerinden birinden döndürülen name alanındaki değeri kullanın.