Ottenere dettagli sullo stato di lettura dello spazio di un utente

Questa guida spiega come utilizzare il metodo getSpaceReadState nella Risorsa SpaceReadState dell'API Google Chat per ottenere dettagli sulla lettura di un utente all'interno di uno spazio. Per ottenere lo stato di lettura di un messaggio in un thread di messaggi: vedi Ottenere dettagli sullo stato di lettura dei thread di un utente.

La SpaceReadState risorsa è una risorsa singleton che rappresenta i dettagli di un l'ultimo messaggio letto dall'utente specificato in uno spazio 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 dello spazio dell'utente chiamante

Per ottenere i dettagli sullo stato di lettura di un utente all'interno di uno spazio, includi quanto segue nella tua richiesta:

  • Specifica il valore chat.users.readstate o chat.users.readstate.readonly nell'ambito dell'autorizzazione.
  • Chiama il Metodo getSpaceReadState il SpaceReadState risorsa.
  • Passa name dello stato di lettura dello spazio per ottenerlo, che include un ID utente o alias e un ID spazio. Il recupero dello stato di lettura dello spazio 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/spaceReadState.
    • L'indirizzo email Workspace dell'utente che ha effettuato la chiamata. Ad esempio: users/user@example.com/spaces/SPACE/spaceReadState.
    • L'ID utente dell'utente chiamante. Ad esempio: users/USER/spaces/SPACE/spaceReadState.

L'esempio seguente restituisce lo stato di lettura dello spazio dell'utente chiamante:

Python

  1. Nella directory di lavoro, crea un file denominato chat_spaceReadState_get.py.
  2. Includi il seguente codice in chat_spaceReadState_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 space 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().getSpaceReadState(
    
            # The space 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/spaceReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. Nel codice, sostituisci quanto segue:

    • SPACE: il nome di uno spazio, che che puoi ottenere Metodo spaces.list nell'API Chat o dall'URL di uno spazio.
  4. Nella directory di lavoro, crea ed esegui l'esempio:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. Nella directory di lavoro, crea un file denominato chat_spaceReadState_get.js.
  2. Includi il seguente codice in chat_spaceReadState_get:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the space read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getSpaceReadState() {
    
      /**
      * 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.getSpaceReadState({
    
        /**
        * The space 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/spaceReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getSpaceReadState().then(console.log);
    
  3. Nel codice, sostituisci quanto segue:

    • SPACE: il nome di uno spazio, che che puoi ottenere Metodo spaces.list nell'API Chat o dall'URL di uno spazio.
  4. Nella directory di lavoro, crea ed esegui l'esempio:

    node chat_spaceReadState_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 space read state for the calling user.
    * @param {string} spaceReadStateName The resource name of the space read state.
    */
    function getSpaceReadState(spaceReadStateName) {
      try {
        Chat.Users.Spaces.getSpaceReadState(spaceReadStateName);
      } 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 dello spazio specificato e restituisce un'istanza di SpaceReadState risorsa.