删除邮件

本指南介绍了如何对 Google Chat API 的 Message 资源使用 delete() 方法来删除文本或卡片消息。

在 Chat API 中,Chat 消息由 Message 资源。 Chat 用户只能发送包含文字的消息, 聊天应用可以使用许多其他即时通讯功能,包括 显示静态或交互式界面,从 以及以私密方式传递消息。如需详细了解 Chat API 提供的消息功能,请参阅 Google Chat 消息概览

借助应用身份验证,您可以使用此方法删除 Chat 应用发送的消息。启用用户身份验证后,您可以使用此方法删除经过身份验证的用户发送的消息。如果该用户是聊天室的管理员,您或许还可以删除其他聊天室成员发送的消息。如需了解详情,请参阅了解您作为聊天室管理员的角色

前提条件

Node.js

Python

Java

Apps 脚本

删除通过用户身份验证的邮件

如需删除经过用户身份验证的邮件,请执行以下操作: 请在请求中传递以下内容:

  • 指定 chat.messages 授权范围。
  • 调用 DeleteMessage() 方法。
  • name 设置为要删除的消息的资源名称。

以下示例使用 用户身份验证

Node.js

chat/client-libraries/cloud/delete-message-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages'];

// This sample shows how to delete a message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/delete_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages"]

# This sample shows how to delete a message with user credential
def delete_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.DeleteMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.delete_message(request)

    # Handle the response
    print(response)

delete_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/DeleteMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.DeleteMessageRequest;
import com.google.chat.v1.SpaceName;

// This sample shows how to delete message with user credential.
public class DeleteMessageUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.messages";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      DeleteMessageRequest.Builder request = DeleteMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME");
      chatServiceClient.deleteMessage(request.build());
    }
  }
}

Apps 脚本

chat/advanced-service/Main.gs
/**
 * This sample shows how to delete a message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages'
 * referenced in the manifest file (appsscript.json).
 */
function deleteMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';

  // Make the request
  const response = Chat.Spaces.Messages.remove(name);

  // Handle the response
  console.log(response);
}

如需运行此示例,请替换以下内容:

  • SPACE_NAME:聊天室的 name 中的 ID。可通过调用 ListSpaces() 方法或聊天室的网址来执行此操作。
  • MESSAGE_NAME:消息的 ID name。 您可以在创建 与 Chat API 异步发送消息,或使用 自定义名称 分配给消息。

如果成功,则响应正文为空,这表示消息 已删除。

使用应用身份验证删除消息

如要删除消息,请按以下步骤操作: 应用身份验证,请将 以下内容:

  • 指定 chat.bot 授权范围。
  • 调用 DeleteMessage() 方法。
  • name 设置为要删除的消息的资源名称。

以下示例展示了如何使用应用身份验证删除消息:

Node.js

chat/client-libraries/cloud/delete-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to delete a message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/delete_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to delete a message with app credential
def delete_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.DeleteMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.delete_message(request)

    # Handle the response
    print(response)

delete_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/DeleteMessageAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.DeleteMessageRequest;

// This sample shows how to delete message with app credential.
public class DeleteMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      DeleteMessageRequest.Builder request = DeleteMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME");
      chatServiceClient.deleteMessage(request.build());
    }
  }
}

Apps 脚本

chat/advanced-service/Main.gs
/**
 * This sample shows how to delete a message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function deleteMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.remove(name, parameters, getHeaderWithAppCredentials());

  // Handle the response
  console.log(response);
}

如需运行此示例,请替换以下内容:

  • SPACE_NAME:聊天室的 name 中的 ID。您可以通过调用 ListSpaces() 方法或从聊天室的网址中获取 ID。
  • MESSAGE_NAME:消息的 ID name。 您可以在创建 与 Chat API 异步发送消息,或使用 自定义名称 分配给消息。

如果成功,则响应正文为空,表示消息已被删除。