메시지 첨부파일에 관한 메타데이터 가져오기

이 가이드에서는 다음 API의 Media 리소스에서 get 메서드를 사용하는 방법을 설명합니다. Google Chat API를 사용하여 메시지 첨부파일에 대한 메타데이터를 가져옵니다. 응답은 인스턴스의 Attachment 리소스.

사용자가 앱에 메시지를 보내면 Google Chat에서 MESSAGE 상호작용 이벤트. 앱에서 수신한 상호작용 이벤트에는 요청 본문인 첨부파일을 포함하여 상호작용 이벤트를 나타내는 JSON 페이로드입니다. 이 첨부파일의 데이터는 첨부파일이 올바른지 여부에 따라 Drive에 저장된 파일 (로컬 파일)일 수 있습니다. 이 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 - 지정된 메시지 첨부파일에 대한 메타데이터를 자세히 설명합니다.