Pobieranie informacji o stanie odczytu wątku użytkownika

Z tego przewodnika dowiesz się, jak używać metody getThreadReadState w ThreadReadState zasób interfejsu Google Chat API, który zawiera szczegółowe informacje o użytkowniku stanu odczytu w wątku. Aby poznać stan przeczytania wiadomości w kosmos, zobacz Uzyskiwanie informacji o stanie odczytu pokoju użytkownika

ThreadReadState zasób to pojedynczy zasób, który przedstawia szczegółowe informacje ostatniej przeczytanej wiadomości użytkownika w wątku wiadomości Google Chat.

Wymagania wstępne

Python

  • Python w wersji 3.6 lub nowszej
  • narzędzie do zarządzania pakietami pip;
  • Najnowsze biblioteki klienta Google. Aby je zainstalować lub zaktualizować: uruchom następujące polecenie w interfejsie wiersza poleceń:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js w wersji 14 lub nowszej
  • npm narzędzie do zarządzania pakietami
  • Najnowsze biblioteki klienta Google. Aby je zainstalować lub zaktualizować: uruchom następujące polecenie w interfejsie wiersza poleceń:
    npm install @google-cloud/local-auth @googleapis/chat
    

Google Apps Script

Pobieranie stanu odczytu wątku użytkownika wywołującego

Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w wątku, umieść w nim parametr :

  • Określ chat.users.readstate lub chat.users.readstate.readonly zakresu autoryzacji.
  • Wywołaj funkcję Metoda getThreadReadState w ThreadReadState zasób.
  • Przekazuj wartość name stanu odczytu wątku, która zawiera identyfikator użytkownika lub alias i identyfikator pokoju. Uzyskiwanie stanu odczytu wątku obsługuje tylko pobieranie odczytu stanu użytkownika wywołującego, który można określić, ustawiając jedną z :
    • Alias me. Przykład: users/me/spaces/SPACE/threads/THREAD/threadReadState
    • Adres e-mail użytkownika nawiązującego połączenie z Workspace. Przykład: users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
    • Identyfikator użytkownika wywołującego. Przykład: users/USER/spaces/SPACE/threads/THREAD/threadReadState

Ten przykład pokazuje stan odczytania wątku przez użytkownika wywołującego:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_threadReadState_get.py
  2. Umieść w pliku chat_threadReadState_get.py ten kod:

    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. Zastąp w kodzie następujące elementy:

  4. W katalogu roboczym skompiluj i uruchom przykład:

    python3 chat_threadReadState_get.py
    

Node.js

  1. W katalogu roboczym utwórz plik o nazwie chat_threadReadState_get.js
  2. Umieść w pliku chat_threadReadState_get ten kod:

    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. Zastąp w kodzie następujące elementy:

  4. W katalogu roboczym skompiluj i uruchom przykład:

    node chat_threadReadState_get.js
    

Google Apps Script

W tym przykładzie wywołujemy interfejs Chat API za pomocą parametru Zaawansowana usługa czatu.

  1. Dodaj zakres autoryzacji chat.users.readstate.readonly do Plik appsscript.json projektu Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Dodaj funkcję taką jak ta do kod:

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

Interfejs Google Chat API pobiera określony stan odczytu wątku i zwraca go wystąpienie ThreadReadState zasób.