本指南說明如何針對以下項目的 Message
資源使用 delete
方法:
透過 Google Chat API 刪除簡訊或資訊卡訊息。
Message
項資源
代表的
文字
或
資訊卡
訊息。你可以
create
、get
、update
或 delete
,透過呼叫方式在 Google Chat API 中傳送訊息
對應的方法如要進一步瞭解簡訊和資訊卡訊息,請參閱
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
授權範圍,因此能刪除郵件 由該使用者建立如果你是聊天室管理員,或許可以 刪除其他訊息。若需更多資訊,請參閲 瞭解聊天室管理員角色。 - 應用程式驗證,
在
chat.bot
授權範圍下,可以刪除由 使用服務帳戶存取該應用程式
- 使用者驗證、
刪除具備使用者驗證機制的訊息
如要刪除具有使用者驗證機制的郵件, 並在要求中傳遞下列資訊:
- 指定
chat.messages
授權範圍。 - 在
delete
方法 的Message
項資源。 - 將
name
設為要刪除的訊息的資源名稱。
下列範例會刪除含有 使用者驗證:
Python
- 在工作目錄中,建立名為
chat_message_delete_user.py
。 在
chat_message_delete_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 deletes 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().delete( # The message 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. name = 'spaces/SPACE/messages/MESSAGE' ).execute() # Prints response to the Chat API call. # When deleting a message, the response body is empty. print(result) if __name__ == '__main__': main()
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,您可以從中取得 這個spaces.list
方法 或聊天室網址傳送MESSAGE
:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的
在工作目錄中建構並執行範例:
python3 chat_message_delete_user.py
如果成功,回應主體會是空白的,表示訊息內容 已刪除。
刪除提供應用程式驗證機制的訊息
如何刪除訊息 應用程式驗證,請將 中的下列資訊:
- 指定
chat.bot
授權範圍。 - 在
delete
方法 對Message
資源而言。 - 將
name
設為要刪除的訊息的資源名稱。
下列範例會刪除含有 應用程式驗證:
Python
- 在工作目錄中,建立名為
chat_delete_message_app.py
。 在
chat_delete_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) # Delete a Chat message. result = chat.spaces().messages().delete( # The message 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. name='spaces/SPACE/messages/MESSAGE' ).execute() # Print Chat API's response in your command line interface. # When deleting a message, the response body is empty. print(result)
請在程式碼中替換下列內容:
SPACE
:也就是空間的name
訊息也會張貼,您可以在spaces.list
方法 或聊天室網址傳送MESSAGE
:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的
在工作目錄中建構並執行範例:
python3 chat_delete_message_app.py
如果成功,回應主體會是空白的,表示訊息內容 已刪除。