本指南說明如何針對 Reaction
資源使用 delete
方法
Google Chat API,可刪除訊息的回應,例如 👍?、🚲? 以及 Θ。
刪除回應並不會一併刪除該訊息。
Reaction
項資源
所代表的表情符號,例如 👍?、🚲?
和 CANNOT TRANSLATE
必要條件
Python
- 企業或企業 具有存取權的 Google Workspace 帳戶 Google Chat。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用並設定 Google Chat API。 圖示和說明
- 安裝 Python Google API 用戶端程式庫。
-
為電腦版應用程式建立 OAuth 用戶端 ID 憑證。如要在此環境中執行範例
指引,將憑證儲存為名為
client_secrets.json
的 JSON 檔案,並儲存至 本機目錄
- 選擇支援使用者驗證的授權範圍。
刪除回應
如要刪除訊息的回應,請在要求中傳遞下列資訊:
- 指定
chat.messages.reactions
或chat.messages
授權 範圍。 - 在
delete
方法 的Reaction
項資源。 - 將
name
設為要刪除的回應的資源名稱。
以下範例會從訊息中刪除 😀? 回應:
Python
- 在工作目錄中,建立名為
chat_reaction_delete.py
的檔案。 在
chat_reaction_delete.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.reactions"] def main(): ''' Authenticates with Chat API via user credentials, then deletes a reaction to 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().reactions().delete( # The reaction to delete. # # 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. # # Replace REACTION with a reaction name. # Obtain the reaction name from the reaction resource of Chat API. name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION' ).execute() if __name__ == '__main__': main()
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,您可以從中取得 這個spaces.list
方法 或聊天室網址傳送MESSAGE
:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的REACTION
:回應名稱,可取得 從spaces.messages.reactions.list
方法 或是擷取後傳回的回應內文 使用 Chat API 以非同步方式建立回應。
在工作目錄中建構並執行範例:
python3 chat_reaction_delete.py
如果成功,回應主體會是空白的,表示回應為 已刪除。