बेहतर चैट सेवा

ऐडवांस चैट सेवा की मदद से, Apps Script में Google Chat API का इस्तेमाल किया जा सकता है. इस एपीआई की मदद से स्क्रिप्ट, Chat स्पेस ढूंढ सकती हैं, बना सकती हैं, और उनमें बदलाव कर सकती हैं. साथ ही, स्पेस में सदस्यों को जोड़ सकती हैं या हटा सकती हैं. इसके अलावा, टेक्स्ट, कार्ड, अटैचमेंट, और प्रतिक्रियाओं वाले मैसेज पढ़ सकती हैं या पोस्ट कर सकती हैं.

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

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानने के लिए, Chat API के रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी ऐडवांस सेवाओं की तरह, Chat सेवा में भी वही ऑब्जेक्ट, तरीके, और पैरामीटर इस्तेमाल होते हैं जो पब्लिक एपीआई में होते हैं.

नमूना कोड

इन सैंपल में, ऐडवांस सेवा का इस्तेमाल करके, Google Chat API की सामान्य कार्रवाइयां करने का तरीका बताया गया है.

उपयोगकर्ता क्रेडेंशियल का इस्तेमाल करके मैसेज पोस्ट करना

यहां दिए गए उदाहरण में, उपयोगकर्ता की ओर से Chat स्पेस में मैसेज पोस्ट करने का तरीका बताया गया है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.messages.create ऑथराइज़ेशन स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.messages.create"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का फ़ंक्शन जोड़ें:

    /**
     * Posts a new message to the specified space on behalf of the user.
     * @param {string} spaceName The resource name of the space.
     */
    function postMessageWithUserCredentials(spaceName) {
      try {
        const message = {'text': 'Hello world!'};
        Chat.Spaces.Messages.create(message, spaceName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }

ऐप्लिकेशन के क्रेडेंशियल के साथ मैसेज पोस्ट करना

यहां दिए गए उदाहरण में बताया गया है कि ऐप्लिकेशन की ओर से, Chat स्पेस में मैसेज कैसे पोस्ट किया जाता है. सेवा खाते के साथ Chat की ऐडवांस सेवा का इस्तेमाल करने के लिए, आपको appsscript.json में अनुमति के स्कोप तय करने की ज़रूरत नहीं होती. सेवा खातों से पुष्टि करने के बारे में ज़्यादा जानने के लिए, Google Chat ऐप्लिकेशन के तौर पर पुष्टि करना लेख पढ़ें.

/**
 * Posts a new message to the specified space on behalf of the app.
 * @param {string} spaceName The resource name of the space.
 */
function postMessageWithAppCredentials(spaceName) {
  try {
    // See https://developers.google.com/chat/api/guides/auth/service-accounts
    // for details on how to obtain a service account OAuth token.
    const appToken = getToken_();
    const message = {'text': 'Hello world!'};
    Chat.Spaces.Messages.create(
        message,
        spaceName,
        {},
        // Authenticate with the service account token.
        {'Authorization': 'Bearer ' + appToken});
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to create message with error %s', err.message);
  }
}

स्पेस पाना

यहां दिए गए उदाहरण में, चैट स्पेस के बारे में जानकारी पाने का तरीका बताया गया है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.spaces.readonly ऑथराइज़ेशन स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.readonly"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का फ़ंक्शन जोड़ें:

    /**
     * Gets information about a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function getSpace(spaceName) {
      try {
        const space = Chat.Spaces.get(spaceName);
        console.log('Space display name: %s', space.displayName);
        console.log('Space type: %s', space.spaceType);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get space with error %s', err.message);
      }
    }

कोई स्पेस बनाना

यहां दिए गए उदाहरण में, Chat स्पेस बनाने का तरीका बताया गया है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.spaces.create ऑथराइज़ेशन स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.create"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का फ़ंक्शन जोड़ें:

    /**
     * Creates a new Chat space.
     */
    function createSpace() {
      try {
        const space = {'displayName': 'New Space', 'spaceType': 'SPACE'};
        Chat.Spaces.create(space);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create space with error %s', err.message);
      }
    }

पैसे चुकाकर ली गई सदस्यताओं की सूची

यहां दिए गए उदाहरण में, चैट स्पेस के सभी सदस्यों की सूची बनाने का तरीका बताया गया है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.memberships.readonly ऑथराइज़ेशन स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships.readonly"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का फ़ंक्शन जोड़ें:

    /**
     * Lists all the members of a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function listMemberships(spaceName) {
      let response;
      let pageToken = null;
      try {
        do {
          response = Chat.Spaces.Members.list(spaceName, {
            pageSize: 10,
            pageToken: pageToken
          });
          if (!response.memberships || response.memberships.length === 0) {
            pageToken = response.nextPageToken;
            continue;
          }
          response.memberships.forEach((membership) => console.log(
              'Member resource name: %s (type: %s)',
              membership.name,
              membership.member.type));
          pageToken = response.nextPageToken;
        } while (pageToken);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed with error %s', err.message);
      }
    }

समस्या हल करें

अगर आपको गड़बड़ी के मैसेज Some requested scopes cannot be shown के साथ Error 400: invalid_scope दिखता है, तो इसका मतलब है कि आपने Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में कोई भी अनुमति का दायरा तय नहीं किया है. ज़्यादातर मामलों में, Apps Script अपने-आप यह तय करता है कि किसी स्क्रिप्ट को किन स्कोप की ज़रूरत है. हालांकि, Chat की ऐडवांस सेवा का इस्तेमाल करते समय, आपको उन अनुमतियों के स्कोप को मैन्युअल तरीके से जोड़ना होगा जिनका इस्तेमाल आपकी स्क्रिप्ट करती है. इन्हें आपको अपने Apps Script प्रोजेक्ट की मेनिफ़ेस्ट फ़ाइल में जोड़ना होगा. एक्सप्लिसिट स्कोप सेट करना लेख पढ़ें.

इस गड़बड़ी को ठीक करने के लिए, Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, अनुमति के सही स्कोप जोड़ें. इन्हें oauthScopes ऐरे के हिस्से के तौर पर जोड़ें. उदाहरण के लिए, spaces.messages.create तरीके को कॉल करने के लिए, यह कोड जोड़ें:

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

सीमाएं और ज़रूरी बातें

Advanced Chat की सेवा इन सुविधाओं के साथ काम नहीं करती:

मैसेज अटैचमेंट डाउनलोड करने या डेवलपर प्रीव्यू वाले तरीके को कॉल करने के लिए, UrlFetchApp का इस्तेमाल करें.