मैसेज मिटाना

इस गाइड में, Google Chat API के Message संसाधन पर delete() तरीके का इस्तेमाल करके, टेक्स्ट या कार्ड मैसेज मिटाने का तरीका बताया गया है.

Chat API में, Chat मैसेज को Message संसाधन के तौर पर दिखाया जाता है. Chat के उपयोगकर्ता सिर्फ़ टेक्स्ट वाले मैसेज भेज सकते हैं. हालांकि, Chat ऐप्लिकेशन मैसेज भेजने से जुड़ी कई अन्य सुविधाओं का इस्तेमाल कर सकते हैं. जैसे, स्टैटिक या इंटरैक्टिव यूज़र इंटरफ़ेस दिखाना, उपयोगकर्ताओं से जानकारी इकट्ठा करना, और निजी तौर पर मैसेज भेजना. Chat API के लिए उपलब्ध मैसेज भेजने की सुविधाओं के बारे में ज़्यादा जानने के लिए, Google Chat में मैसेज भेजने की सुविधा के बारे में खास जानकारी देखें.

ऐप्लिकेशन की पुष्टि करने की सुविधा की मदद से, इस तरीके का इस्तेमाल करके Chat ऐप्लिकेशन से भेजे गए मैसेज को मिटाया जा सकता है. उपयोगकर्ता की पुष्टि करने की सुविधा की मदद से, इस तरीके का इस्तेमाल करके, पुष्टि किए गए उपयोगकर्ता के भेजे गए मैसेज को मिटाया जा सकता है. अगर उपयोगकर्ता स्पेस मैनेजर है, तो उसके पास स्पेस के अन्य सदस्यों के भेजे गए मैसेज को मिटाने का विकल्प भी होता है. ज़्यादा जानकारी के लिए, स्पेस मैनेजर की भूमिका के बारे में जानें लेख पढ़ें.

ज़रूरी शर्तें

Node.js

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Python

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Java

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Apps Script

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

उपयोगकर्ता की पुष्टि करके मैसेज मिटाना

उपयोगकर्ता की पुष्टि वाले मैसेज को मिटाने के लिए, अपने अनुरोध में यह जानकारी शामिल करें:

  • 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 का आईडी. आईडी पाने के लिए, ListSpaces() तरीके को कॉल करें या स्पेस के यूआरएल से आईडी पाएं.
  • MESSAGE_NAME: मैसेज के 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 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 का आईडी. आईडी पाने के लिए, ListSpaces() तरीके को कॉल करें या स्पेस के यूआरएल से आईडी पाएं.
  • MESSAGE_NAME: मैसेज के name से मिला आईडी. Chat API की मदद से, मैसेज को एसिंक्रोनस तरीके से बनाने के बाद, जवाब के मुख्य हिस्से से आईडी पाया जा सकता है. इसके अलावा, मैसेज बनाते समय असाइन किए गए कस्टम नाम से भी आईडी पाया जा सकता है.

अगर अनुरोध पूरा हो जाता है, तो जवाब का मुख्य हिस्सा खाली होता है. इससे पता चलता है कि मैसेज मिटा दिया गया है.