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

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

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

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

नमूना कोड

इन सैंपल से आपको यह पता चलेगा कि ऐडवांस सेवा का इस्तेमाल करके, 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);
  }
}

किसी ऑपरेशन का नाम पाने के लिए, name फ़ील्ड से मिली वैल्यू का इस्तेमाल करें. यह वैल्यू, Google Workspace Events API के किसी तरीके से मिलती है. जैसे, subscriptions.create() या subscriptions.patch().