メッセージの詳細を確認する

このガイドでは、次の Message リソースで get メソッドを使用する方法について説明します。 テキスト メッセージやカード メッセージの詳細を返すこともできます。

Message リソース は、 テキスト または カード Google Chat で管理できます。Google Chat では Google Chat API でメッセージを creategetupdatedelete のいずれかに 対応するメソッドがあります。テキスト メッセージとカード メッセージについて詳しくは、以下をご覧ください。 Google Chat メッセージの概要

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

ユーザー認証を使用してメッセージを取得する

メッセージの詳細を確認するには、 ユーザー認証、 リクエストに以下を渡します。

  • 認可スコープ chat.messages.readonly または chat.messages を指定します。
  • 呼び出し get メソッド 日付 Message リソース
  • name を、取得するメッセージのリソース名に設定します。

次の例では、メッセージに ユーザー認証:

Python

  1. 作業ディレクトリに、chat_message_get_user.py という名前のファイルを作成します。
  2. 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 message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードの次のように置き換えます。

    • SPACE: スペース名。 spaces.list メソッド スペースの URL から取得できます。
    • MESSAGE: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_message_get_user.py
    

Chat API は、メッセージに対して Message メッセージの詳細です。

アプリの認証を使用してメッセージを取得する

メッセージの詳細を確認するには、 アプリの認証、 リクエストに以下を渡します。

  • chat.bot 認可スコープを指定します。
  • 呼び出し get メソッド 日付 Message リソース
  • name を、取得するメッセージのリソース名に設定します。

次の例では、メッセージに アプリの認証:

Python

  1. 作業ディレクトリに、chat_get_message_app.py という名前のファイルを作成します。
  2. 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. コードの次のように置き換えます。

    • SPACE: スペースの name 表示されます。この情報は、 spaces.list メソッド スペースの URL から取得できます。

    • MESSAGE: メッセージ名(取得可能) 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_get_message_app.py
    

Chat API は、メッセージに対して Message メッセージの詳細です。