이 가이드에서는 다음의 Message
리소스에서 list
메서드를 사용하는 방법을 설명합니다.
Google Chat API를 사용하여 스페이스에서 페이지로 나눈 필터링 가능한 메시지 목록을 확인합니다.
Chat API에서 Chat 메시지는
Message
리소스.
Chat 사용자는 텍스트가 포함된 메시지만 보낼 수 있지만
채팅 앱에서는 다음과 같은 다양한 메시지 기능을 사용할 수 있습니다.
정적 또는 대화형 사용자 인터페이스를 표시하여
메시지를 비공개로
전달할 수 있습니다 메시지 기능 자세히 알아보기
Chat API에서 사용할 수 있는 기능에 대한 자세한 내용은
Google Chat 메시지 개요
기본 요건
Python
- 비즈니스 또는 기업 다음 액세스 권한이 있는 Google Workspace 계정 Google Chat
- 환경을 설정합니다.
<ph type="x-smartling-placeholder">
- </ph>
- Google Cloud 프로젝트를 만듭니다.
- OAuth 동의 화면 구성
- Google Chat API를 사용 설정 및 구성합니다. 아이콘, 채팅 앱 설명이 있습니다.
- 설치 Python Google API 클라이언트 라이브러리를 참조하세요.
- <ph type="x-smartling-placeholder"></ph>
데스크톱 애플리케이션용 OAuth 클라이언트 ID 사용자 인증 정보 만들기 이 실습에서 샘플을 실행하려면
가이드에서 사용자 인증 정보를
client_secrets.json
이라는 JSON 파일로 로컬 디렉터리에 저장합니다
- <ph type="x-smartling-placeholder"></ph> 사용자 인증을 지원하는 승인 범위를 선택합니다.
메시지 나열
다음을 포함하는 메시지 나열 사용자 인증 요청에 다음을 전달합니다.
chat.messages.readonly
또는chat.messages
승인 범위를 지정합니다.- 먼저
list
메서드 에Message
리소스.
다음 예시는 다음 날짜 이후에 전송된 Chat 스페이스의 메시지를 나열합니다. 2023년 3월 16일:
Python
- 작업 디렉터리에
chat_messages_list.py
라는 파일을 만듭니다. chat_messages_list.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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists messages in a space sent after March 16, 2023. ''' # 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().list( # The space for which to list messages. parent = 'spaces/SPACE', # An optional filter that returns messages # created after March 16, 2023. filter = 'createTime > "2023-03-16T00:00:00-00:00"' ).execute() # Prints the list of messages. print(result) if __name__ == '__main__': main()
코드에서
SPACE
을 스페이스 이름으로 바꿉니다. GCP 콘솔에서spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_messages_list.py
Chat API는 지정된 스페이스에서 전송된 메시지 목록을 반환합니다.
2023년 3월 16일 이후부터 적용됩니다. 요청으로부터 받은 메시지가 없으면
Chat API 응답이 빈 객체를 반환합니다.
REST/HTTP 인터페이스에서 응답에 빈 JSON 객체 {}
가 포함되어 있습니다.