Sprawdzanie szczegółów wiadomości

Z tego przewodnika dowiesz się, jak używać metody get w zasobie Message interfejsu Google Chat API do zwracania szczegółów wiadomości tekstowej lub karty.

Zasób Message reprezentuje wiadomość w Google Chat lub tekst lub kartę. Możesz create, get, update lub delete wysłać wiadomość w Google Chat API, wywołując odpowiednie metody. Więcej informacji o SMS-ach i kartach znajdziesz w artykule Omówienie 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. Otrzymanie wiadomości obsługuje obie metody uwierzytelniania:

Otrzymywanie wiadomości po uwierzytelnieniu użytkownika

Aby uzyskać szczegóły wiadomości z uwierzytelnianiem użytkownika, przekaż w żądaniu te informacje:

  • Określ zakres autoryzacji chat.messages.readonly lub chat.messages.
  • Wywołaj metodę get w zasobie Message.
  • Ustaw name na nazwę zasobu wiadomości, którą chcesz pobrać.

Poniższy przykład otrzymuje komunikat z uwierzytelnianiem użytkownika:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_message_get_user.py.
  2. Umieść ten kod w elemencie chat_message_get_user.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.messages.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets a message.
        '''
    
        # 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.spaces().messages().get(
    
            # The message to get.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. Zastąp w nim ten fragment kodu:

    • SPACE: nazwa pokoju, którą możesz uzyskać za pomocą metody spaces.list w interfejsie Chat API lub z adresu URL pokoju.
    • MESSAGE: nazwa wiadomości, którą można uzyskać z treści odpowiedzi zwróconej po asynchronicznym utworzeniu wiadomości za pomocą interfejsu Chat API lub z niestandardową nazwą przypisaną do wiadomości w momencie jej utworzenia.
  4. W katalogu roboczym skompiluj i uruchom przykład:

    python3 chat_message_get_user.py
    

Chat API zwraca instancję Message, która zawiera szczegóły określonej wiadomości.

Otrzymywanie wiadomości przez uwierzytelnienie w aplikacji

Aby uzyskać szczegóły wiadomości z uwierzytelnianiem aplikacji, przekaż w żądaniu te informacje:

  • Określ zakres autoryzacji chat.bot.
  • Wywołaj metodę get w zasobie Message.
  • Ustaw name na nazwę zasobu wiadomości, którą chcesz pobrać.

Poniższy przykład otrzymuje komunikat z uwierzytelnianiem aplikacji:

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Get a Chat message.
    result = chat.spaces().messages().get(
    
        # The message to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        #
        # Replace MESSAGE with a message name.
        # Obtain the message name from the response body returned
        # after creating a message asynchronously with Chat REST API.
        name='spaces/SPACE/messages/MESSAGE'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. Zastąp w nim ten fragment kodu:

    • SPACE: name pokoju, w którym została opublikowana wiadomość, który można uzyskać za pomocą metody spaces.list w interfejsie Chat API lub z adresu URL pokoju.

    • MESSAGE: nazwa wiadomości, którą można uzyskać z treści odpowiedzi zwróconej po asynchronicznym utworzeniu wiadomości za pomocą interfejsu Chat API lub z niestandardową nazwą przypisaną do wiadomości w momencie jej utworzenia.

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

    python3 chat_get_message_app.py
    

Chat API zwraca instancję Message, która zawiera szczegóły określonej wiadomości.