メッセージの一覧表示

このガイドでは、次の Message リソースで list メソッドを使用する方法について説明します。 スペース内のメッセージのリスト(ページ分け、フィルタ可能なリスト)を表示できます。

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
    

メッセージの一覧表示

メッセージを一覧表示するには、 ユーザー認証、 リクエストに以下を渡します。

次の例では、Google Chat スペース内のメッセージが、 2023 年 3 月 16 日:

Python

  1. 作業ディレクトリに、chat_messages_list.py という名前のファイルを作成します。
  2. chat_messages_list.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 lists messages in a space sent after March 16, 2023.
        '''
    
        # 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().list(
    
              # The space for which to list messages.
              parent = 'spaces/SPACE',
    
              # An optional filter that returns messages
              # created after March 16, 2023.
              filter = 'createTime > "2023-03-16T00:00:00-00:00"'
    
          ).execute()
    
        # Prints the list of messages.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで SPACE をスペース名に置き換えます。スペースには こちらの spaces.list メソッド スペースの URL から取得できます。

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

    python3 chat_messages_list.py
    

指定したスペースで送信されたメッセージのリストが Chat API から返される 2023 年 3 月 16 日以降に有効になります。リクエストからのメッセージがない場合、 Chat API のレスポンスで空のオブジェクトが返されます。「 REST/HTTP インターフェース。レスポンスには空の JSON オブジェクト {} が含まれます。