メディアを添付ファイルとしてアップロード

このガイドでは、次の Media リソースで upload メソッドを使用する方法について説明します。 Google Chat API を使用してメディア(ファイル)を Google Chat にアップロードし、 クリックします。

ユーザーがアプリにメッセージを送信すると、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
    

添付ファイルとしてアップロード

メディアをアップロードしてメッセージに添付するには、 request::

  • 認可スコープ chat.messages.create または chat.messages を指定します。
  • 次の Google Chat メソッドを呼び出します。
    1. ファイルをアップロードするには、 upload メソッド Media リソースに対する権限。
      • parent を、ファイルをホストするスペースのスペース名に設定します。
      • body(リクエスト本文)で、filename をアップロードしたファイルの名前に設定します。 表示されます。
      • media_body を、アップロードするファイルのインスタンスとして設定します。
    2. アップロードしたファイルを添付したメッセージを作成するには、 create メソッド 日付 Messages リソース
      • 呼び出しからのレスポンスとして attachment を設定します。 upload メソッド 日付 Media リソースattachment フィールドにはリストを指定できます。

次の例では、PNG イメージファイルをアップロードし、メッセージに添付しています。

Python

  1. 作業ディレクトリに、先ほど作成した chat_media_and_attachment_upload.py
  2. chat_media_and_attachment_upload.py に次のコードを含めます。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.http import MediaFileUpload
    
    # 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.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then uploads a file as media, creates a message, and
        attaches the file to the 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.
        service = build('chat', 'v1', credentials=creds)
    
        # Upload a file to Google Chat.
        media = MediaFileUpload('test_image.png', mimetype='image/png')
    
        # Create a message and attach the uploaded file to it.
        attachment_uploaded = service.media().upload(
    
            # The space to upload the attachment in.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The filename of the attachment, including the file extension.
            body={'filename': 'test_image.png'},
    
            # Media resource of the attachment.
            media_body=media
    
        ).execute()
    
        print(attachment_uploaded)
    
        # Create a Chat message with attachment.
        result = service.spaces().messages().create(
    
            # The space to create the message in.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Must match the space name that the attachment is uploaded to.
            parent='spaces/SPACE',
    
            # The message to create.
            body={
                'text': 'Hello, world!',
                'attachment': [attachment_uploaded]
            }
    
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで、SPACE をスペース名に置き換えて、 添付ファイルをアップロードします。 spaces.list メソッド スペースの URL から取得できます。

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

    python3 chat_media_and_attachment_upload.py
    

Chat API から返されるレスポンスの本文には attachmentDataRef は、アップロードされたファイルの詳細に置き換えます。

制限事項と考慮事項

ファイルをアップロードしてメールに添付する準備ができたら、 制限事項と考慮事項: