Usuwanie wiadomości

Ten przewodnik wyjaśnia, jak używać metody delete() w zasobie Message interfejsu Google Chat API do usuwania wiadomości tekstowych lub kart.

W interfejsie Chat API wiadomość w Google Chat jest reprezentowana przez Messagezasób. Użytkownicy Google Chat mogą wysyłać tylko wiadomości tekstowe, ale aplikacje Google Chat mogą korzystać z wielu innych funkcji przesyłania wiadomości, w tym wyświetlać statyczne lub interaktywne interfejsy użytkownika, zbierać informacje od użytkowników i dostarczać wiadomości prywatne. Więcej informacji o funkcjach przesyłania wiadomości dostępnych w interfejsie Chat API znajdziesz w przeglądzie wiadomości w Google Chat.

W przypadku uwierzytelniania aplikacji możesz użyć tej metody, aby usunąć wiadomość wysłaną przez aplikację Chat. W przypadku uwierzytelniania użytkownika możesz użyć tej metody, aby usunąć wiadomość wysłaną przez uwierzytelnionego użytkownika. Jeśli użytkownik jest menedżerem pokoju, możesz też usunąć wiadomość wysłaną przez innego użytkownika. Więcej informacji znajdziesz w artykule Więcej informacji o roli menedżera pokoju.

Wymagania wstępne

Node.js

Python

Java

Google Apps Script

Usuwanie wiadomości z uwierzytelnianiem użytkownika

Aby usunąć wiadomość z uwierzytelnianiem użytkownika, w żądaniu przekaż te informacje:

  • Określ zakres autoryzacji chat.messages.
  • Wywołaj metodę DeleteMessage().
  • Ustaw name na nazwę zasobu wiadomości do usunięcia.

W tym przykładzie usuwamy wiadomość z uwierzytelnianiem użytkownika:

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());
    }
  }
}

Google 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);
}

Aby uruchomić ten przykład, zastąp te elementy:

  • SPACE_NAME: identyfikator z name pokoju. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.
  • MESSAGE_NAME: identyfikator z pola name wiadomości. Identyfikator możesz uzyskać z treści odpowiedzi zwróconej po utworzeniu wiadomości asynchronicznie za pomocą interfejsu Chat API lub za pomocą niestandardowej nazwy przypisanej do wiadomości podczas jej tworzenia.

Jeśli operacja się uda, treść odpowiedzi będzie pusta, co oznacza, że wiadomość została usunięta.

Usuwanie wiadomości z uwierzytelnianiem aplikacji

Aby usunąć wiadomość za pomocą uwierzytelniania aplikacji, w żądaniu przekaż następujące informacje:

  • Określ zakres autoryzacji chat.bot.
  • Wywołaj metodę DeleteMessage().
  • Ustaw name na nazwę zasobu wiadomości do usunięcia.

W tym przykładzie usuwamy wiadomość z uwierzytelnianiem aplikacji:

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());
    }
  }
}

Google 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);
}

Aby uruchomić ten przykład, zastąp te elementy:

  • SPACE_NAME: identyfikator z name pokoju. Możesz go uzyskać, wywołując metodę ListSpaces() lub z adresu URL pokoju.
  • MESSAGE_NAME: identyfikator z pola name wiadomości. Identyfikator możesz uzyskać z treści odpowiedzi zwróconej po utworzeniu wiadomości asynchronicznie za pomocą interfejsu Chat API lub za pomocą niestandardowej nazwy przypisanej do wiadomości podczas jej tworzenia.

Jeśli operacja się uda, treść odpowiedzi będzie pusta, co oznacza, że wiadomość została usunięta.