Google Workspace के इवेंट के लिए बेहतर सेवा

बेहतर Google Workspace इवेंट सेवा की मदद से, Apps Script में Google Workspace Events API का इस्तेमाल किया जा सकता है. इस एपीआई की मदद से, Google Workspace के संसाधनों की सदस्यता ली जा सकती है, ताकि आपको अपनी पसंद के इवेंट मिल सकें. इवेंट, संसाधनों में होने वाले बदलावों को दिखाते हैं, जैसे कि संसाधन कब बनाए जाते हैं, अपडेट किए जाते हैं या मिटाए जाते हैं.

ज़रूरी शर्तें

  • ऐसा Apps Script प्रोजेक्ट, जो Apps Script की मदद से अपने-आप बनाए गए डिफ़ॉल्ट प्रोजेक्ट के बजाय, स्टैंडर्ड Google Cloud प्रोजेक्ट का इस्तेमाल करता है.
  • सदस्यता से जुड़े इवेंट की जानकारी पाने के लिए, उसी Google Cloud प्रोजेक्ट में Pub/Sub विषय बनाया गया हो. कोई Pub/Sub विषय बनाने के लिए, Pub/Sub के लिए विषय बनाना और उसकी सदस्यता लेना देखें.
  • Chat इवेंट की सदस्यता लेने के लिए, ज़रूरी है कि आपने Google Cloud Console में, 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 इवेंट एपीआई का रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी बेहतर सेवाओं की तरह, Google Workspace इवेंट सेवा भी उन ही ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है जिन्हें सार्वजनिक एपीआई में किया जाता है.

नमूना कोड

इन सैंपल से पता चलता है कि बेहतर सेवा का इस्तेमाल करके, 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 इवेंट एपीआई के ज़्यादातर तरीकों में, लंबे समय तक चलने वाली कार्रवाई दिखती है. कार्रवाई की स्थिति जानने के लिए, 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 इवेंट एपीआई के किसी एक तरीके, जैसे कि subscriptions.create() या subscriptions.patch() से मिली है.