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

Chat स्पेस, सदस्यों, और मैसेज को मैनेज करना.

ऐडवांस चैट सेवा की मदद से, Google 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;
          }
          for (const membership of response.memberships) {
            console.log(
              "Member: %s, Role: %s",
              membership.member.displayName,
              membership.role,
            );
          }
          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"
]

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

ऐडवांस चैट की सेवा इन सुविधाओं के साथ काम नहीं करती:

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