حذف رسالة

يوضّح هذا الدليل كيفية استخدام طريقة delete() في مورد Message ضِمن Google Chat API لحذف رسالة نصية أو رسالة بطاقة.

في Chat API، يتم تمثيل رسالة Chat من خلال المورد Message. في حين أنّه بإمكان مستخدمي Chat إرسال رسائل تتضمّن نصًا فقط، يمكن لتطبيقات Chat استخدام العديد من ميزات المراسلة الأخرى، بما في ذلك عرض واجهات مستخدم ثابتة أو تفاعلية، وجمع المعلومات من المستخدمين، وتسليم الرسائل بشكل خاص. لمزيد من المعلومات حول ميزات المراسلة المتاحة لواجهة برمجة تطبيقات Chat، يُرجى الاطّلاع على نظرة عامة على رسائل Google Chat.

باستخدام مصادقة التطبيق، يمكنك استخدام هذه الطريقة لحذف رسالة أرسلها تطبيق Chat. باستخدام مصادقة المستخدم، يمكنك استخدام هذه الطريقة لحذف رسالة أرسلها المستخدم الذي تمت مصادقته. إذا كان المستخدم مدير مساحة، قد تتمكّن أيضًا من حذف رسالة أرسلها أعضاء آخرون في المساحة. لمزيد من المعلومات، يُرجى الاطّلاع على مقالة التعرّف على دورك كمدير مساحة.

المتطلبات الأساسية

Node.js

Python

Java

برمجة تطبيقات

حذف رسالة باستخدام مصادقة المستخدم

لحذف رسالة تتضمّن مصادقة المستخدم، يجب تضمين ما يلي في طلبك:

  • حدِّد 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());
    }
  }
}

برمجة تطبيقات

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() أو من عنوان URL الخاص بالمساحة.
  • 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());
    }
  }
}

برمجة تطبيقات

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() أو من عنوان URL الخاص بالمساحة.
  • MESSAGE_NAME: المعرّف من name الخاص بالرسالة يمكنك الحصول على المعرّف من نص الرد الذي يتم إرجاعه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API، أو باستخدام الاسم المخصّص الذي تم تعيينه للرسالة عند إنشائها.

إذا كانت الاستجابة ناجحة، سيكون نص الاستجابة فارغًا، ما يشير إلى أنّه تم حذف الرسالة.