本指南說明如何在 Google Chat API 的 Message
資源上使用 patch
方法,更新聊天室中的文字或資訊卡訊息。更新訊息即可變更訊息屬性,例如訊息內容或資訊卡的內容。您也可以在資訊卡訊息中加上文字訊息,或是在簡訊中附加資訊卡。
Chat API 也支援 update
方法,但我們強烈建議呼叫 patch
方法,因為該方法使用 PATCH
HTTP 要求,而 update
會使用 PUT
HTTP 要求。詳情請參閱 AIP-134 的 PATCH
和 PUT
一節。
Message
資源代表 Google Chat 中的文字或資訊卡訊息。您可以呼叫對應的方法,在 Google Chat API 中 create
、get
、update
或 delete
訊息。如要進一步瞭解文字和資訊卡訊息,請參閱「Google Chat 訊息總覽」。
必要條件
Python
- Python 3.6 或更高版本
- pip 套件管理工具
最新的 Python 專用 Google 用戶端程式庫。如要安裝或更新,請在指令列介面中執行下列指令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
- 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
設定 Chat 應用程式的授權:
在使用者驗證的情況下更新簡訊,或在卡片訊息前加上簡訊
chat.messages
授權範圍。- 要更新的訊息的
name
。 updateMask='text'
- 指定已更新訊息的
body
。
如果更新的訊息是資訊卡訊息,則文字訊息會在資訊卡訊息前面 (持續顯示)。
以下說明如何更新簡訊,或透過使用者驗證在卡片訊息前加上簡訊:
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
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MESSAGE
:訊息名稱。您可以透過 Chat API 以非同步方式建立訊息後傳回的回應主體取得此名稱,或是在建立時指派給該訊息的自訂名稱。
在工作目錄中,建構並執行範例:
python3 chat_update_text_message_user.py
利用應用程式驗證功能,更新簡訊或在卡片訊息前加上簡訊
chat.bot
授權範圍。- 要更新的訊息的
name
。 updateMask='text'
- 指定已更新訊息的
body
。
如果更新的訊息是資訊卡訊息,則文字訊息會在資訊卡訊息前面 (持續顯示)。
以下說明如何更新簡訊中的簡訊,或使用應用程式驗證在資訊卡訊息前加上簡訊:
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
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。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
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。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
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MESSAGE
:訊息名稱。您可以透過 Chat API 以非同步方式建立訊息後傳回的回應主體取得此名稱,或是在建立時指派給該訊息的自訂名稱。
在工作目錄中,建構並執行範例:
python3 chat_update_text_message_user.py