Google Chat 스페이스에서 메시지 고정 또는 고정 해제하기

Chat API의 MessagePin 리소스를 사용하면 앱에서 메시지를 고정하고, 메시지를 고정 해제하고, Google Chat 스페이스에서 고정된 모든 메시지의 목록을 가져올 수 있습니다. 고정된 메시지는 스페이스의 모든 참여자의 Chat 인터페이스에 표시됩니다. 이 API는 앱이 사용자를 대신하여 고정된 메시지를 관리하도록 지원합니다.

사용자가 스페이스에서 메시지를 고정하는 방법에 관한 자세한 내용은 스페이스 및 메시지에서 메시지, 파일, 링크 고정하기를 참고하세요.

기본 요건

Node.js

고려사항

  • 스페이스에 이미 있는 메시지만 고정할 수 있습니다. 동일한 요청에서 새 메시지를 만들고 고정할 수는 없습니다.
  • 나에게만 표시되는 메시지는 고정할 수 없습니다. 예를 들어 앱에서 수신한 비공개 메시지는 고정할 수 없습니다.
  • 각 Chat 스페이스에는 최대 100개의 고정된 메시지가 있을 수 있습니다. 앱에서 101번째 메시지를 고정하려고 하면 API에서 오류를 반환합니다.

메시지 고정

사용자 인증으로 메시지를 고정하려면 요청에 다음을 전달하세요.

  • chat.spaces.pins 또는 chat.spaces 승인 범위를 지정합니다.
  • messagePins.create을 호출합니다.
  • parent (스페이스 이름)를 지정하고 원래 메시지의 message 리소스 이름이 포함된 body을 제공합니다.

다음 샘플은 스페이스에 메시지를 고정합니다.

Python

  1. 작업 디렉터리에 chat_pin_message.py라는 파일을 만듭니다.
  2. chat_pin_message.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then pins a message in a space.
        '''
    
        # 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)
    
        # Pin a message.
        result = service.spaces().messagePins().create(
            # The space to pin the message in.
            #
            # Replace SPACE with a space ID or name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The message to pin.
            body={
                'message': 'spaces/SPACE/messages/MESSAGE'
            }
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스의 name에서 가져온 ID입니다.
    • MESSAGE: 메시지의 name에서 가져온 ID입니다.
  4. 작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.

    python3 chat_pin_message.py

메시지 고정 해제

사용자 인증으로 메시지를 고정 해제하려면 요청에 다음을 전달하세요.

  • chat.spaces.pins 또는 chat.spaces 승인 범위를 지정합니다.
  • messagePins.delete을 호출합니다.
  • name을 삭제할 MessagePin의 리소스 이름으로 설정합니다.

메시지를 고정 해제하는 방법은 다음과 같습니다.

Python

  1. 작업 디렉터리에 chat_unpin_message.py라는 파일을 만듭니다.
  2. chat_unpin_message.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then unpins a message from a space.
        '''
    
        # 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)
    
        # Unpin a message.
        result = service.spaces().messagePins().delete(
            # The resource name of the message pin to delete.
            #
            # Replace SPACE with a space ID or name, and MESSAGE with the message ID.
            name='spaces/SPACE/messagePins/MESSAGE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스의 name에서 가져온 ID입니다.
    • MESSAGE: 메시지의 name에서 가져온 ID입니다.
  4. 작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.

    python3 chat_unpin_message.py

고정된 메시지 목록 가져오기

액세스할 수 있는 스페이스의 고정된 메시지 목록을 가져오려면 사용자 인증을 사용하고 요청에 다음을 전달하세요.

  • chat.spaces.pins.readonly, chat.spaces.readonly, chat.spaces.pins, chat.spaces 읽기 범위 중 하나를 지정합니다.
  • messagePins.list을 호출합니다.
  • 고정된 메시지를 가져올 스페이스 이름으로 parent를 지정합니다.

고정된 메시지를 나열하는 방법은 다음과 같습니다.

Python

  1. 작업 디렉터리에 chat_list_pinned_messages.py라는 파일을 만듭니다.
  2. chat_list_pinned_messages.py에 다음 코드를 포함합니다.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists pinned messages in a space.
        '''
    
        # 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)
    
        # List pinned messages.
        result = service.spaces().messagePins().list(
            # The space to list pinned messages from.
            #
            # Replace SPACE with a space ID or name.
            parent='spaces/SPACE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 SPACE를 스페이스의 name에 있는 스페이스 ID로 바꿉니다.

  4. 작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.

    python3 chat_list_pinned_messages.py