刪除留言

本指南將說明如何使用 delete() Google Chat API 的 Message 資源方法,用於刪除文字或資訊卡 撰寫新的電子郵件訊息

在 Chat API 中,Chat 訊息會以 Message 資源表示。儘管 Chat 使用者只能傳送含有文字的訊息, 即時通訊應用程式可以使用許多其他訊息功能,包括 靜態或互動式使用者介面,可用來收集 使用者,以及進行私人訊息傳遞。進一步瞭解訊息功能 如要瞭解 Chat API 可用的功能,請參閱 Google Chat 訊息總覽

透過應用程式驗證, 您可以使用這個方法來刪除 已傳送即時通訊應用程式。取代為 使用者驗證,則 就能使用這個方法刪除已驗證使用者傳送的訊息。如果使用者是聊天室的聊天室管理員,您也許可以刪除其他聊天室成員傳送的訊息。詳情請參閱「瞭解聊天室管理員角色」。

必要條件

Node.js

Python

Java

Apps Script

刪除具備使用者驗證機制的訊息

如要刪除具有使用者驗證機制的郵件, 並在要求中傳遞下列資訊:

  • 指定 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 Script

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:訊息 name 中的 ID。建立完成後,您可從系統傳回的回應主體中取得 ID 或是以非同步方式傳送 自訂名稱 是在建立訊息時指派的

如果成功,回應主體會留白,表示訊息已刪除。

刪除含有應用程式驗證的訊息

如何刪除訊息 應用程式驗證,請將 中的下列資訊:

  • 指定 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 Script

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:訊息 name 中的 ID。建立完成後,您可從系統傳回的回應主體中取得 ID 或是以非同步方式傳送 自訂名稱 是在建立訊息時指派的

如果成功,回應主體會是空白的,表示訊息內容 已刪除。