メッセージの添付ファイルに関するメタデータを取得する

このガイドでは、次のリソースの Media リソースで get メソッドを使用する方法について説明します。 メッセージの添付ファイルに関するメタデータを取得するための Google Chat API。レスポンスは Pod の Attachment リソース

ユーザーがアプリにメッセージを送信すると、Google Chat は MESSAGE インタラクション イベント。 アプリが受け取るインタラクション イベントには、リクエスト本文が含まれます。これは、 操作イベント(添付ファイルを含む)を表す JSON ペイロード。「 添付ファイルに含まれるデータは、その添付ファイルが アップロード済みコンテンツ(ローカル ファイル)、ドライブに保存されているファイルなどです。「 Media リソース とは、Google Chat にアップロードされた、画像、動画、ドキュメントなどのファイルを表します。 「 Attachment リソース メッセージに添付されたメディア(ファイル)のインスタンスを表します。Attachment リソースには、次のような添付ファイルに関するメタデータが含まれます。 保存されます。

前提条件

Python

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

メッセージの添付ファイルを取得する

Google Chat でメッセージの添付ファイルに関するメタデータを非同期で取得するには、 次のコードを追加します。

メッセージの添付ファイルに関するメタデータを取得する方法は次のとおりです。

Python

  1. 作業ディレクトリに、先ほど作成した chat_get_message_attachment.py
  2. chat_get_message_attachment.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().attachments().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/attachments/ATTACHMENT'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. コード内の spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENT メールの添付ファイル名が付いています

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

    python3 chat_get_message_attachment.py
    

Chat API は、メッセージに対して Attachment 指定したメッセージの添付ファイルに関するメタデータの詳細を記述します。