Chat API의 MessagePin 리소스를 사용하면 앱에서 메시지를 고정하고, 메시지를 고정 해제하고, Google Chat 스페이스에서 고정된 모든 메시지의 목록을 가져올 수 있습니다. 고정된 메시지는 스페이스의 모든 참여자의 Chat 인터페이스에 표시됩니다. 이 API는 앱이 사용자를 대신하여 고정된 메시지를 관리하도록 지원합니다.
사용자가 스페이스에서 메시지를 고정하는 방법에 관한 자세한 내용은 스페이스 및 메시지에서 메시지, 파일, 링크 고정하기를 참고하세요.
기본 요건
Node.js
- Google Chat에 액세스할 수 있는 비즈니스 또는 엔터프라이즈 Google Workspace 계정
- 환경을 설정합니다.
- Google Cloud 프로젝트를 만듭니다.
- OAuth 동의 화면 구성
- Chat 앱의 이름, 아이콘, 설명으로 Google Chat API를 사용 설정하고 구성합니다.
- Node.js Cloud 클라이언트 라이브러리를 설치합니다.
- 데스크톱 애플리케이션의
OAuth 클라이언트 ID 사용자 인증 정보를 만듭니다. 이 가이드의 샘플을 실행하려면 사용자 인증 정보를
credentials.json이라는 JSON 파일로 로컬 디렉터리에 저장하세요.
- 사용자 인증을 지원하는 승인 범위 선택
고려사항
- 스페이스에 이미 있는 메시지만 고정할 수 있습니다. 동일한 요청에서 새 메시지를 만들고 고정할 수는 없습니다.
- 나에게만 표시되는 메시지는 고정할 수 없습니다. 예를 들어 앱에서 수신한 비공개 메시지는 고정할 수 없습니다.
- 각 Chat 스페이스에는 최대 100개의 고정된 메시지가 있을 수 있습니다. 앱에서 101번째 메시지를 고정하려고 하면 API에서 오류를 반환합니다.
메시지 고정
사용자 인증으로 메시지를 고정하려면 요청에 다음을 전달하세요.
chat.spaces.pins또는chat.spaces승인 범위를 지정합니다.messagePins.create을 호출합니다.parent(스페이스 이름)를 지정하고 원래 메시지의message리소스 이름이 포함된body을 제공합니다.
다음 샘플은 스페이스에 메시지를 고정합니다.
Python
- 작업 디렉터리에
chat_pin_message.py라는 파일을 만듭니다. 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()코드에서 다음을 바꿉니다.
작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.
python3 chat_pin_message.py
메시지 고정 해제
사용자 인증으로 메시지를 고정 해제하려면 요청에 다음을 전달하세요.
chat.spaces.pins또는chat.spaces승인 범위를 지정합니다.messagePins.delete을 호출합니다.name을 삭제할MessagePin의 리소스 이름으로 설정합니다.
메시지를 고정 해제하는 방법은 다음과 같습니다.
Python
- 작업 디렉터리에
chat_unpin_message.py라는 파일을 만듭니다. 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()코드에서 다음을 바꿉니다.
작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.
python3 chat_unpin_message.py
고정된 메시지 목록 가져오기
액세스할 수 있는 스페이스의 고정된 메시지 목록을 가져오려면 사용자 인증을 사용하고 요청에 다음을 전달하세요.
chat.spaces.pins.readonly,chat.spaces.readonly,chat.spaces.pins,chat.spaces읽기 범위 중 하나를 지정합니다.messagePins.list을 호출합니다.- 고정된 메시지를 가져올 스페이스 이름으로
parent를 지정합니다.
고정된 메시지를 나열하는 방법은 다음과 같습니다.
Python
- 작업 디렉터리에
chat_list_pinned_messages.py라는 파일을 만듭니다. 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()코드에서
SPACE를 스페이스의name에 있는 스페이스 ID로 바꿉니다.작업 디렉터리에서 다음 샘플을 빌드하고 실행합니다.
python3 chat_list_pinned_messages.py