Pobieranie szczegółowych informacji o stanie odczytu pokoju użytkownika

Z tego przewodnika dowiesz się, jak używać metody getSpaceReadState w zasobie SpaceReadState interfejsu Google Chat API, aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju. Informacje o stanie przeczytania wiadomości w wątku znajdziesz w artykule Sprawdzanie stanu przeczytania wątku użytkownika.

Zasób SpaceReadState to pojedynczy zasób, który reprezentuje szczegóły ostatniej przeczytanej wiadomości określonego użytkownika w pokoju 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 pokoju użytkownika dzwoniącego

Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju, umieść w swojej prośbie te dane:

  • Określ zakres autoryzacji chat.users.readstate lub chat.users.readstate.readonly.
  • Wywołaj metodę getSpaceReadState w zasobie SpaceReadState.
  • Przekaż name stanu odczytu pokoju, aby go pobrać, który obejmuje identyfikator lub alias użytkownika i identyfikator pokoju. Uzyskanie stanu odczytu dotyczącego pokoju obsługuje 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/spaceReadState.
    • Adres e-mail Workspace użytkownika dzwoniącego. Przykład: users/user@example.com/spaces/SPACE/spaceReadState.
    • Identyfikator użytkownika rozmówcy. Przykład: users/USER/spaces/SPACE/spaceReadState.

Ten przykład umożliwia pobranie stanu odczytu przestrzeni użytkownika wywołującego:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_spaceReadState_get.py.
  2. Umieść ten kod w elemencie 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. 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.
  4. W katalogu roboczym skompiluj i uruchom przykład:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. W katalogu roboczym utwórz plik o nazwie chat_spaceReadState_get.js.
  2. Umieść ten kod w elemencie 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. 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.
  4. W katalogu roboczym skompiluj i uruchom przykład:

    node chat_spaceReadState_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 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);
      }
    }
    

Google Chat API pobiera określony stan odczytu pokoju i zwraca wystąpienie zasobu SpaceReadState.