메시지 삭제

이 가이드에서는 다음의 Message 리소스에서 delete 메서드를 사용하는 방법을 설명합니다. Google Chat API를 사용하여 문자 또는 카드 메시지를 삭제할 수 있습니다.

Chat API에서 Chat 메시지는 Message 리소스. Chat 사용자는 텍스트가 포함된 메시지만 보낼 수 있지만 채팅 앱에서는 다음과 같은 다양한 메시지 기능을 사용할 수 있습니다. 정적 또는 대화형 사용자 인터페이스를 표시하여 메시지를 비공개로 전달할 수 있습니다 메시지 기능 자세히 알아보기 Chat API에서 사용할 수 있는 기능에 대한 자세한 내용은 Google Chat 메시지 개요

앱 인증을 사용하면 이 메서드를 사용하여 채팅 앱이 전송되었습니다. 다음으로 바꿉니다. 사용자 인증이 필요한 경우 인증된 사용자가 보낸 메일을 삭제할 수 있습니다. 만약 사용자가 스페이스의 스페이스 관리자인 경우 메시지가 표시됩니다. 자세한 내용은 자세히 알아보기 스페이스 관리자 역할을 참고하시기 바랍니다.

기본 요건

Python

사용자 인증이 포함된 메일 삭제

사용자 인증이 포함된 메일을 삭제하려면 다음 안내를 따르세요. 요청에 다음을 전달합니다.

  • chat.messages 승인 범위를 지정합니다.
  • 먼저 delete 메서드Message 리소스.
  • name을 삭제할 메시지의 리소스 이름으로 설정합니다.

다음 예는 사용자 인증:

Python

  1. 작업 디렉터리에서 chat_message_delete_user.py
  2. chat_message_delete_user.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # 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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a 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.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().delete(
    
            # The message to delete.
            #
            # 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'
    
        ).execute()
    
        # Prints response to the Chat API call.
        # When deleting a message, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름으로, 다음에서 가져올 수 있습니다. spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.
    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_message_delete_user.py
    

성공한 경우 응답 본문이 비어 있으며, 이는 메시지가 이(가) 삭제되었습니다.

앱 인증이 포함된 메시지 삭제

다음 메시지가 있는 메시지를 삭제하려면 앱 인증이 필요한 경우 다음을 포함합니다.

  • chat.bot 승인 범위를 지정합니다.
  • 먼저 delete 메서드 (Message 리소스에 있음)
  • name을 삭제할 메시지의 리소스 이름으로 설정합니다.

다음 예는 앱 인증:

Python

  1. 작업 디렉터리에서 chat_delete_message_app.py
  2. chat_delete_message_app.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)
    
    # Delete a Chat message.
    result = chat.spaces().messages().delete(
    
      # The message to delete.
      #
      # 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'
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    # When deleting a message, the response body is empty.
    print(result)
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: name 메시지가 게시되면 spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.
    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_delete_message_app.py
    

성공한 경우 응답 본문이 비어 있으며, 이는 메시지가 이(가) 삭제되었습니다.