이 가이드에서는 다음의 Message
리소스에서 patch
메서드를 사용하는 방법을 설명합니다.
Google Chat API를 사용하여 스페이스에서 텍스트 또는 카드 메시지를 업데이트합니다.
메시지를 사용하여 메시지 내용 또는 메시지의 내용과 같은 메시지 속성을
카드를 사용합니다. 또한
카드 메시지를 사용하거나 문자 메시지에 카드를 추가할 수 있습니다.
Chat API는 또한
update
메서드,
하지만
patch
메서드
왜냐하면 그것은 PATCH
HTTP 요청을 사용하는 동안
update
는
PUT
HTTP 요청 자세한 내용은
AIP-134의 PATCH
및 PUT
섹션.
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 클라이언트 라이브러리를 참조하세요.
- Google Chat API에서 인증하려는 방법에 따라 액세스 사용자 인증 정보를 만듭니다.
요청:
<ph type="x-smartling-placeholder">
- </ph>
- Chat 사용자로 인증하려면 다음 단계를 따르세요.
OAuth 클라이언트 ID 만들기
사용자 인증 정보를 만들고 사용자 인증 정보를
client_secrets.json
을 로컬 디렉터리로 복사합니다. - 채팅 앱으로 인증하려면 다음 단계를 따르세요.
서비스 계정 만들기
사용자 인증 정보를 만들고 사용자 인증 정보를
credentials.json
입니다.
- Chat 사용자로 인증하려면 다음 단계를 따르세요.
OAuth 클라이언트 ID 만들기
사용자 인증 정보를 만들고 사용자 인증 정보를
- <ph type="x-smartling-placeholder"></ph> 사용자로 인증할 것인지, 아니면 다음으로 인증할 것인지에 따라 승인 범위를 선택합니다. 채팅 앱
사용자 인증을 통해 문자 메시지를 업데이트하거나 카드 메시지 앞에 문자 메시지 추가
문자 메시지 다음 코드로 교체합니다. 사용자 인증, 다음과 같이 요청합니다.
chat.messages
승인 범위- 업데이트할 메시지의
name
입니다. updateMask='text'
- 업데이트된 메시지를 지정하는
body
입니다.
업데이트된 메시지가 카드 메시지, 문자 메시지가 카드 메시지 앞에 추가됩니다 (계속 표시됨).
새 Search Ads 360 환경의 문자 메시지 또는 앞에 문자 메시지를 추가할 때 카드 메시지 다음 코드로 교체합니다. 사용자 인증:
Python
- 작업 디렉터리에서
chat_update_text_message_user.py
chat_update_text_message_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 updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text', body={'text': 'Updated message!'} ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
코드에서 다음을 바꿉니다.
SPACE
: 스페이스 이름으로, 다음에서 가져올 수 있습니다.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_update_text_message_user.py
앱 인증을 통해 문자 메시지를 업데이트하거나 카드 메시지 앞에 문자 메시지 추가
문자 메시지 다음 코드로 교체합니다. 앱 인증 요청에 다음을 전달합니다.
chat.bot
승인 범위- 업데이트할 메시지의
name
입니다. updateMask='text'
- 업데이트된 메시지를 지정하는
body
입니다.
업데이트된 메시지가 카드 메시지인 경우 문자 메시지가 카드 메시지 앞에 추가됩니다 (계속 표시됨).
새 Search Ads 360 환경의 문자 메시지 또는 문자 메시지 앞에 문자 메시지를 추가할 수 있습니다. 카드 메시지 다음 코드로 교체합니다. 앱 인증:
Python
- 작업 디렉터리에서
chat_update_text_message_app.py
chat_update_text_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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text', body={'text': 'Updated message!'} ).execute() # Print Chat API's response in your command line interface. print(result)
코드에서 다음을 바꿉니다.
SPACE
: 스페이스 이름으로, 다음에서 가져올 수 있습니다.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_update_text_message_app.py
카드 메시지를 업데이트하거나 문자 메시지에 카드 메시지 추가하기
카드 메시지, 요청에 다음을 전달합니다.
chat.bot
승인 범위 카드 메시지 업데이트 요구사항 앱 인증- 업데이트할 메시지의
name
입니다. updateMask='cardsV2'
- 업데이트된 메시지를 지정하는
body
입니다.
업데이트된 메시지가 문자 메시지 그러면 카드가 문자 메시지에 추가됩니다 (계속 표시됨). 만약 업데이트된 메시지 자체가 카드를 선택한 경우 표시되는 카드는 다음과 같습니다. 이(가) 업데이트되었습니다.
메일을 카드 메시지:
Python
- 작업 디렉터리에서
chat_update_card_message.py
chat_update_card_message.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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='cardsV2', body= { 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Print Chat API's response in your command line interface. print(result)
코드에서 다음을 바꿉니다.
SPACE
: 스페이스 이름으로, 다음에서 가져올 수 있습니다.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_update_card_message.py
Chat API는
Message
드림
업데이트된 메시지를 표시합니다.
여러 필드 경로를 동시에 사용하여 메시지 업데이트
메시지가 업데이트되면
있습니다. 예를 들어 업데이트 메시지 요청에서
text
및 cardsv2
필드 경로를 동시에 전달하여
문자와 카드가 있습니다. 메시지에 텍스트만 있고 카드가 없는 경우 카드
메시지에 추가됩니다. 지원되는 필드 경로에 대한 자세한 내용은
보기
updateMask
매개변수
둘 다 업데이트하려면
text
드림
및
card
메시지를 보낼 때
사용자 인증,
요청에 다음을 전달합니다.
chat.messages
승인 범위- 업데이트할 메시지의
name
입니다. 업데이트할 메시지 필드 경로를 지정하는
updateMask
(구분됨) 쉼표(updateMask='text', 'cardsV2'
)로 구분.업데이트된 모든 필드를 포함하여 업데이트된 메시지를 지정하는
body
학습합니다.
여기서 text
및 cardsV2
필드 경로를 업데이트하는 방법은
메시지를 보낼 수 있습니다
사용자 인증:
Python
- 작업 디렉터리에서
chat_update_text_message_user.py
chat_update_text_message_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 updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text,cardsV2', body= {'text': 'Updated message!', 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
코드에서 다음을 바꿉니다.
SPACE
: 스페이스 이름으로, 다음에서 가져올 수 있습니다.spaces.list
메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.MESSAGE
: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
작업 디렉터리에서 샘플을 빌드하고 실행합니다.
python3 chat_update_text_message_user.py