이 가이드에서는 다음의 Message
리소스에서 get
메서드를 사용하는 방법을 설명합니다.
Google Chat API를 사용하여 문자 또는 카드 메시지에 대한 세부정보를 반환합니다.
이
Message
리소스
는
텍스트
또는
카드
메시지가 표시됩니다. 다음을 수행할 수 있습니다.
다음을 호출하여 Google Chat API의 create
, get
, update
또는 delete
메시지
사용할 수 있습니다. 문자 및 카드 메시지에 대한 자세한 내용은 다음을 참고하세요.
Google Chat 메시지 개요
기본 요건
Python
- 비즈니스 또는 기업 다음 액세스 권한이 있는 Google Workspace 계정 Google Chat
- Python 3.6 이상
- pip 패키지 관리 도구
- 최신 Google 클라이언트 라이브러리 이러한 앱을 설치하거나 업데이트하려면 다음 단계를 따르세요.
명령줄 인터페이스에서 다음 명령어를 실행합니다.
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 환경을 설정합니다.
- Google Cloud 프로젝트를 만듭니다.
- OAuth 동의 화면 구성
- Google Chat API를 사용 설정 및 구성합니다. 아이콘, 채팅 앱 설명이 있습니다.
- Google Chat API 요청에서 인증하려는 방법에 따라 액세스 사용자 인증 정보를 만듭니다.
- Chat 사용자로 인증하려면 다음 단계를 따르세요.
OAuth 클라이언트 ID 만들기
사용자 인증 정보를 만들고 사용자 인증 정보를
client_secrets.json
를 로컬 디렉터리로 복사합니다. - 채팅 앱으로 인증하려면 다음 단계를 따르세요. 서비스 계정 만들기 사용자 인증 정보를 제공합니다.
- Chat 사용자로 인증하려면 다음 단계를 따르세요.
OAuth 클라이언트 ID 만들기
사용자 인증 정보를 만들고 사용자 인증 정보를
- 사용자로 인증할 것인지, 아니면 다음으로 인증할 것인지에 따라 승인 범위를 선택합니다. 채팅 앱
사용자 인증이 포함된 메시지 받기
다음 메시지가 있는 메시지에 대한 세부정보를 확인하려면 다음을 사용하세요. 사용자 인증 요청에 다음을 전달합니다.
chat.messages.readonly
또는chat.messages
승인 범위를 지정합니다.- 먼저
get
메서드 에Message
리소스. name
을 가져올 메시지의 리소스 이름으로 설정합니다.
다음 예는 사용자 인증:
Python
- 작업 디렉터리에
chat_message_get_user.py
라는 파일을 만듭니다. chat_message_get_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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets 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().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' ).execute() # Prints details about the message. print(result) if __name__ == '__main__': main()
코드에서 다음을 바꿉니다.
SPACE
: 스페이스 이름으로, 다음에서 가져올 수 있습니다.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_message_get_user.py
Chat API는
Message
를 참조하세요.
앱 인증이 포함된 메시지 받기
다음 메시지가 있는 메시지에 대한 세부정보를 확인하려면 다음을 사용하세요. 앱 인증 요청에 다음을 전달합니다.
chat.bot
승인 범위를 지정합니다.- 먼저
get
메서드 에Message
리소스. name
을 가져올 메시지의 리소스 이름으로 설정합니다.
다음 예는 앱 인증:
Python
- 작업 디렉터리에
chat_get_message_app.py
라는 파일을 만듭니다. chat_get_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) # Get a Chat message. result = chat.spaces().messages().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' ).execute() # Print Chat API's response in your command line interface. print(result)
코드에서 다음을 바꿉니다.
SPACE
:name
메시지가 게시되면spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_get_message_app.py
Chat API는
Message
를 참조하세요.