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

Z tego przewodnika dowiesz się, jak używać metody getSpaceReadState w SpaceReadState zasób interfejsu Google Chat API, który zawiera szczegółowe informacje o odczytaniu przez użytkownika stanu w pokoju. Aby uzyskać stan odczytania wiadomości w wątku: zobacz Uzyskiwanie szczegółowych informacji o stanie odczytu wątków użytkownika

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

Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w pokoju, uwzględnij te informacje: w Twoim żądaniu:

  • Określ chat.users.readstate lub chat.users.readstate.readonly zakresu autoryzacji.
  • Wywołaj funkcję Metoda getSpaceReadState w SpaceReadState zasób.
  • Aby uzyskać wartość, przekaż name stanu odczytu pokoju, który zawiera identyfikator użytkownika lub alias i identyfikator pokoju. Uzyskiwanie stanu odczytu pokoju 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/spaceReadState
    • Adres e-mail użytkownika nawiązującego połączenie z Workspace. Przykład: users/user@example.com/spaces/SPACE/spaceReadState
    • Identyfikator użytkownika wywołującego. Przykład: users/USER/spaces/SPACE/spaceReadState

Ten przykład ilustruje stan odczytu miejsca użytkownika dzwoniącego:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_spaceReadState_get.py
  2. Umieść w pliku chat_spaceReadState_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 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 kodzie następujące elementy:

    • SPACE: nazwa pokoju, która który znajdziesz w Metoda 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ść w pliku chat_spaceReadState_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 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 kodzie następujące elementy:

    • SPACE: nazwa pokoju, która który znajdziesz w Metoda 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ł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 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);
      }
    }
    

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