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

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

사용자가 앱에 메시지를 보내면 Google Chat에서 MESSAGE 상호작용 이벤트를 전달합니다. 앱에서 수신한 상호작용 이벤트에는 모든 첨부파일을 포함한 상호작용 이벤트를 나타내는 JSON 페이로드인 요청 본문이 포함됩니다. 첨부파일의 데이터는 첨부파일이 콘텐츠 (로컬 파일) 업로드되었는지 아니면 드라이브에 저장된 파일인지에 따라 다릅니다. Media 리소스는 이미지, 동영상, 문서처럼 Google Chat에 업로드된 파일을 나타냅니다. Attachment 리소스는 메시지에 첨부된 미디어(파일)의 인스턴스를 나타냅니다. Attachment 리소스에는 첨부파일에 대한 메타데이터(예: 저장된 위치)가 포함됩니다.

기본 요건

Python

  • Python 3.6 이상
  • pip 패키지 관리 도구
  • 최신 Python용 Google 클라이언트 라이브러리입니다. 이를 설치하거나 업데이트하려면 명령줄 인터페이스에서 다음 명령어를 실행합니다.

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • Google Chat API가 사용 설정 및 구성된 Google Cloud 프로젝트 단계는 Google Chat 앱 빌드를 참고하세요.
  • 채팅 앱에 구성된 승인. 메시지를 가져오려면 chat.bot 승인 범위의 앱 인증이 필요합니다.

메시지 첨부파일 받기

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 인스턴스를 반환합니다.