उपयोगकर्ता की थ्रेड पढ़े जाने की स्थिति के बारे में जानकारी पाएं

इस गाइड में बताया गया है कि मैसेज थ्रेड में किसी उपयोगकर्ता के पढ़े गए स्टेटस की जानकारी पाने के लिए, Google Chat API के ThreadReadState संसाधन पर getThreadReadState तरीके का इस्तेमाल कैसे करें. स्पेस में किसी मैसेज को पढ़े जाने की स्थिति जानने के लिए, स्पेस के पढ़े जाने की स्थिति के बारे में जानकारी पाना लेख देखें.

ThreadReadState रिसॉर्स एक सिंगलटन रिसॉर्स है, जो Google Chat के मैसेज थ्रेड में, किसी खास उपयोगकर्ता के आखिरी बार पढ़े गए मैसेज की जानकारी दिखाता है.

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

Python

  • Python 3.6 या इससे नया वर्शन
  • pip पैकेज मैनेजमेंट टूल
  • Python के लिए नई Google क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल या अपडेट करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में नीचे दिया गया कमांड चलाएं:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • ऐसा Google Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
  • Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. स्पेस में, उपयोगकर्ता की रीड स्टेट के बारे में जानकारी पाने के लिए, chat.users.readstate या chat.users.readstate.readonly की अनुमति के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है.

Node.js

  • Node.js और npm
  • Node.js के लिए नई Google क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में इस कमांड को चलाएं:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • ऐसा Google Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
  • Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. स्पेस में, उपयोगकर्ता की रीड स्टेट के बारे में जानकारी पाने के लिए, chat.users.readstate या chat.users.readstate.readonly की अनुमति के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है.

Apps Script

  • Google Workspace खाता, जिसमें Google Chat का ऐक्सेस हो.
  • पब्लिश किया गया Chat ऐप्लिकेशन. Chat ऐप्लिकेशन बनाने के लिए, इस quickstart को अपनाएं.
  • Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. स्पेस में, उपयोगकर्ता की रीड स्टेट के बारे में जानकारी पाने के लिए, chat.users.readstate या chat.users.readstate.readonly की अनुमति के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है.

कॉल करने वाले उपयोगकर्ता के थ्रेड को पढ़ने की स्थिति पाएं

किसी मैसेज थ्रेड में उपयोगकर्ता के पढ़े जाने की स्थिति के बारे में जानकारी पाने के लिए, अपने अनुरोध में ये चीज़ें शामिल करें:

  • chat.users.readstate या chat.users.readstate.readonly की अनुमति का दायरा बताएं.
  • ThreadReadState संसाधन पर getThreadReadState वाला तरीका कॉल करें.
  • पाने के लिए, थ्रेड रीड स्टेट का name पास करें. इसमें यूज़र आईडी या उपनाम और स्पेस आईडी शामिल है. थ्रेड रीड की स्थिति पाने से, सिर्फ़ कॉल करने वाले उपयोगकर्ता की रीड की स्थिति मिल सकती है. यह स्थिति, इनमें से किसी एक को सेट करके तय की जा सकती है:
    • me उपनाम. उदाहरण के लिए, users/me/spaces/SPACE/threads/THREAD/threadReadState.
    • कॉल करने वाले उपयोगकर्ता का Workspace ईमेल पता. उदाहरण के लिए, users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState.
    • कॉल करने वाले उपयोगकर्ता का यूज़र आईडी. उदाहरण के लिए, users/USER/spaces/SPACE/threads/THREAD/threadReadState.

नीचे दिए गए उदाहरण में, कॉल करने वाले उपयोगकर्ता की थ्रेड रीड की स्थिति का पता लगाया जाता है:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_threadReadState_get.py नाम की फ़ाइल बनाएं.
  2. chat_threadReadState_get.py में यह कोड शामिल करें:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.users.readstate.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets the thread read state for the calling user.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.users().spaces().threads().getThreadReadState(
    
            # The thread read state to get.
            #
            # Replace USER with the calling user's ID, Workspace email,
            # or the alias me.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace THREAD with a thread name.
            # Obtain the thread name from the messages resource of Chat API.
            name='users/me/spaces/SPACE/threads/THREAD/threadReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • THREAD: थ्रेड का नाम, जिसे Chat API में spaces.messages.get तरीके से पाया जा सकता है.

  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_threadReadState_get.py
    

Node.js

  1. अपनी वर्किंग डायरेक्ट्री में, chat_threadReadState_get.js नाम की फ़ाइल बनाएं.
  2. chat_threadReadState_get में यह कोड शामिल करें:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the thread read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getThreadReadState() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.users.readstate.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.users.spaces.threads.getThreadReadState({
    
        /**
        * The thread read state to get.
        *
        * Replace USER with the calling user's ID, Workspace email,
        * or the alias me.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        */
        name: 'users/me/spaces/SPACE/threads/THREADS/threadReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getThreadReadState().then(console.log);
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • THREAD: थ्रेड का नाम, जिसे Chat API में spaces.messages.get तरीके से पाया जा सकता है.

  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    node chat_threadReadState_get.js
    

Apps Script

इस उदाहरण में, बेहतर चैट सेवा का इस्तेमाल करके Chat API को कॉल किया जाता है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.users.readstate.readonly अनुमति का स्कोप जोड़ें:

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

    /**
    * Authenticates with Chat API via user credentials,
    * then gets the thread read state for the calling user.
    * @param {string} threadReadStateName The resource name of the thread read state.
    */
    function getThreadReadState(threadReadStateName) {
      try {
        Chat.Users.Spaces.Threads.getThreadReadState(threadReadStateName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get read state with error %s', err.message);
      }
    }
    

Google Chat API को थ्रेड रीड की स्थिति की जानकारी मिलती है और यह ThreadReadState रिसॉर्स का इंस्टेंस दिखाता है.