Ottenere dettagli sullo stato di lettura del thread di un utente

Questa guida spiega come utilizzare il metodo getThreadReadState nella ThreadReadState risorsa dell'API Google Chat per ottenere i dettagli sulla in un thread di messaggi. Per ottenere lo stato di lettura di un messaggio in nello spazio, vedi Ottenere dettagli sullo stato di lettura dello spazio di un utente.

La ThreadReadState risorsa è una risorsa singleton che rappresenta i dettagli di un l'ultimo messaggio letto da un utente specificato in un thread di messaggi di Google Chat.

Prerequisiti

Python

  • Python 3.6 o versioni successive
  • Lo strumento di gestione dei pacchetti pip
  • Le librerie client di Google più recenti. Per installarle o aggiornarle, esegui questo comando nell'interfaccia a riga di comando:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 o versioni successive
  • npm strumento di gestione dei pacchetti
  • Le librerie client di Google più recenti. Per installarle o aggiornarle, esegui questo comando nell'interfaccia a riga di comando:
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

Ottieni lo stato di lettura del thread dell'utente chiamante

Per ottenere dettagli sullo stato di lettura di un utente all'interno di un thread di messaggi, includi il comando a seguire nella tua richiesta:

  • Specifica il valore chat.users.readstate o chat.users.readstate.readonly nell'ambito dell'autorizzazione.
  • Chiama il Metodo getThreadReadState il ThreadReadState risorsa.
  • Passa name dello stato di lettura del thread per ottenerlo, che include un ID utente o alias e un ID spazio. L'ottenimento dello stato di lettura del thread supporta solo il recupero lo stato dell'utente chiamante, che può essere specificato impostando uno dei seguenti:
    • L'alias me. Ad esempio: users/me/spaces/SPACE/threads/THREAD/threadReadState.
    • L'indirizzo email Workspace dell'utente che ha effettuato la chiamata. Ad esempio: users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState.
    • L'ID utente dell'utente chiamante. Ad esempio: users/USER/spaces/SPACE/threads/THREAD/threadReadState.

L'esempio seguente restituisce lo stato di lettura del thread dell'utente chiamante:

Python

  1. Nella directory di lavoro, crea un file denominato chat_threadReadState_get.py.
  2. Includi il seguente codice in 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. Nel codice, sostituisci quanto segue:

  4. Nella directory di lavoro, crea ed esegui l'esempio:

    python3 chat_threadReadState_get.py
    

Node.js

  1. Nella directory di lavoro, crea un file denominato chat_threadReadState_get.js.
  2. Includi il seguente codice in 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. Nel codice, sostituisci quanto segue:

  4. Nella directory di lavoro, crea ed esegui l'esempio:

    node chat_threadReadState_get.js
    

Apps Script

In questo esempio viene chiamata l'API Chat utilizzando Servizio Chat avanzato.

  1. Aggiungi l'ambito dell'autorizzazione chat.users.readstate.readonly al File appsscript.json del progetto Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Aggiungi una funzione come questa all'interfaccia codice:

    /**
    * 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);
      }
    }
    

L'API Google Chat ottiene lo stato di lettura del thread specificato e restituisce un'istanza di ThreadReadState risorsa.