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

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

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

前提条件

Python

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

メディアをアップロードしてメッセージに添付するには、リクエストで次の情報を渡します。

次の例では、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(  
                  'credentials.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()  
  1. コードで、SPACE を添付ファイルをアップロードするスペースの名前に置き換えます。スペースの名前は、Chat API の spaces.list メソッドまたはスペースの URL から取得できます。
  2. 作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_media_and_attachment_upload.py  

Chat API は、アップロードされたファイルの詳細を含む attachmentDataRef を含むレスポンス本文を返します。

制限事項と考慮事項

ファイルをアップロードしてメッセージに添付する際は、次の制限事項と考慮事項に注意してください。