本指南說明如何針對 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-httplib2 google-auth-oauthlib oauth2client
已發布的 Chat 應用程式。如要建立及發布 Chat 應用程式,請參閱「建構 Google Chat 應用程式」。
已設定 Chat 應用程式的授權:
提供使用者驗證機制,藉此更新簡訊或在卡片訊息前加上簡訊
如要使用使用者驗證機制更新文字訊息,請在要求中傳遞以下內容:
chat.messages
授權範圍。- 要更新的訊息的
name
。 updateMask='text'
body
用於指定更新的訊息。
如果更新的訊息是資訊卡訊息,則文字訊息會加在資訊卡訊息前 (持續顯示)。
以下說明如何更新簡訊,或使用使用者驗證功能在資訊卡訊息前加上文字訊息:
Python
- 在工作目錄中,建立名為
chat_update_text_message_user.py
的檔案。 在
chat_update_text_message_user.py
中加入下列程式碼:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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 created membership. 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 httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # 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 httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # 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
中加入下列程式碼:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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 created membership. print(result) if __name__ == '__main__': main()
在程式碼中,替換以下內容:
SPACE
:可從 Chat API 的spaces.list
方法或聊天室網址取得的聊天室名稱。MESSAGE
:訊息名稱,您可以在使用 Chat API 以非同步方式建立訊息後,從傳回的回應主體中取得,或在建立訊息時指派給訊息的自訂名稱。
在工作目錄中建構並執行範例:
python3 chat_update_text_message_user.py