本指南介绍了如何使用 Google Chat API 的 Message
资源中的 delete
方法来删除文本或卡片消息。
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
授权范围,用户身份验证可以删除该用户创建的消息。如果您是聊天室管理员,或许可以删除其他消息。如需了解详情,请参阅了解您作为聊天室管理员的角色。 - 借助
chat.bot
授权范围,应用身份验证可以使用其服务帐号删除该应用创建的消息。
- 借助
删除通过用户身份验证的邮件
如需删除使用用户身份验证的消息,请在请求中传递以下内容:
- 指定
chat.messages
授权范围。 - 对
Message
资源调用delete
方法。 - 将
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
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。MESSAGE
:消息名称,您可以从使用 Chat API 异步创建消息后返回的响应正文中获取,也可以从创建消息时分配给消息的自定义名称获取。
在您的工作目录中,构建并运行该示例:
python3 chat_message_delete_user.py
如果成功,响应正文为空,表示消息已被删除。
通过应用身份验证删除消息
如需删除使用应用身份验证的消息,请在请求中传递以下内容:
- 指定
chat.bot
授权范围。 - 对
Message
资源调用delete
方法。 - 将
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
,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。MESSAGE
:消息名称,您可以从使用 Chat API 异步创建消息后返回的响应正文中获取,也可以使用在创建消息时分配给消息的自定义名称获取。
在您的工作目录中,构建并运行该示例:
python3 chat_delete_message_app.py
如果成功,响应正文为空,表示消息已被删除。