Pobieranie informacji o stanie odczytu wątku użytkownika

Z tego przewodnika dowiesz się, jak używać metody getThreadReadState w zasobie ThreadReadState interfejsu Google Chat API, aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w wątku wiadomości. Aby uzyskać informacje o stanie przeczytania wiadomości w pokoju, zapoznaj się z informacjami o stanie odczytu pokoju użytkownika.

Zasób ThreadReadState to pojedynczy zasób, który reprezentuje szczegóły ostatniej przeczytanej wiadomości określonego użytkownika w wątku wiadomości w Google Chat.

Wymagania wstępne

Python

  • Python 3.6 lub nowszy
  • Narzędzie do zarządzania pakietami pip
  • Najnowsze biblioteki klienta Google dla języka Python. Aby je zainstalować lub zaktualizować, uruchom w interfejsie wiersza poleceń to polecenie:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Projekt Google Cloud z włączonym i skonfigurowanym interfejsem Google Chat API. Instrukcje znajdziesz w artykule Tworzenie aplikacji Google Chat.
  • Skonfigurowano autoryzację dla aplikacji Google Chat. Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju, wymagane jest uwierzytelnianie użytkownika z zakresem autoryzacji chat.users.readstate lub chat.users.readstate.readonly.

Node.js

  • Node.js i npm
  • Najnowsze biblioteki klienta Google dla środowiska Node.js. Aby je zainstalować, uruchom w interfejsie wiersza poleceń to polecenie:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • Projekt Google Cloud z włączonym i skonfigurowanym interfejsem Google Chat API. Instrukcje znajdziesz w artykule Tworzenie aplikacji Google Chat.
  • Skonfigurowano autoryzację dla aplikacji Google Chat. Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju, wymagane jest uwierzytelnianie użytkownika z zakresem autoryzacji chat.users.readstate lub chat.users.readstate.readonly.

Google Apps Script

  • konto Google Workspace z dostępem do Google Chat.
  • Opublikowana aplikacja Google Chat. Aby utworzyć aplikację do obsługi Google Chat, postępuj zgodnie z tym quickstart.
  • Skonfigurowano autoryzację dla aplikacji Google Chat. Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju, wymagane jest uwierzytelnianie użytkownika z zakresem autoryzacji chat.users.readstate lub chat.users.readstate.readonly.

Pobieranie stanu odczytu wątku użytkownika dzwoniącego

Aby uzyskać szczegółowe informacje o stanie przeczytania wiadomości przez użytkownika w wątku, umieść w prośbie te dane:

  • Określ zakres autoryzacji chat.users.readstate lub chat.users.readstate.readonly.
  • Wywołaj metodę getThreadReadState w zasobie ThreadReadState.
  • Przekaż name stanu odczytu wątku do pobrania, który obejmuje identyfikator lub alias użytkownika i identyfikator pokoju. Uzyskanie stanu odczytu wątku umożliwia tylko pobieranie stanu odczytu użytkownika wywołującego, który można określić, ustawiając jedno z tych ustawień:
    • Alias me. Przykład: users/me/spaces/SPACE/threads/THREAD/threadReadState.
    • Adres e-mail Workspace użytkownika dzwoniącego. Przykład: users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState.
    • Identyfikator użytkownika rozmówcy. Przykład: users/USER/spaces/SPACE/threads/THREAD/threadReadState.

W tym przykładzie pobierany jest stan odczytu wątku użytkownika wywołującego:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_threadReadState_get.py.
  2. Umieść ten kod w elemencie 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. Zastąp w nim ten fragment kodu:

    • SPACE: nazwa pokoju, którą można uzyskać za pomocą metody spaces.list w interfejsie Chat API lub z adresu URL pokoju.

    • THREAD: nazwa wątku, którą można uzyskać za pomocą metody spaces.messages.get w interfejsie Chat API.

  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ść ten kod w elemencie 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. Zastąp w nim ten fragment kodu:

    • SPACE: nazwa pokoju, którą można uzyskać za pomocą metody spaces.list w interfejsie Chat API lub z adresu URL pokoju.

    • THREAD: nazwa wątku, którą można uzyskać za pomocą metody spaces.messages.get w interfejsie Chat API.

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

    node chat_threadReadState_get.js
    

Google Apps Script

W tym przykładzie wywołujesz interfejs Chat API za pomocą zaawansowanej usługi czatu.

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

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Dodaj funkcję podobną do tej do kodu projektu 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 pobiera określony stan odczytu wątku i zwraca instancję zasobu ThreadReadState.