איך מוחקים הודעות?

במדריך הזה מוסבר איך להשתמש בשיטה delete() במשאב Message של Google Chat API כדי למחוק הודעת טקסט או כרטיס.

ב-Chat API, הודעה ב-Chat מיוצגת על ידימשאב Message. משתמשי Chat יכולים לשלוח רק הודעות שמכילות טקסט, אבל אפליקציות ל-Chat יכולות להשתמש בהרבה תכונות אחרות של הודעות, כולל הצגת ממשקי משתמש סטטיים או אינטראקטיביים, איסוף מידע מהמשתמשים ושליחת הודעות באופן פרטי. מידע נוסף על תכונות ההודעות שזמינות ב-Chat API זמין בסקירה הכללית על הודעות ב-Google Chat.

באמצעות אימות אפליקציות, אפשר להשתמש בשיטה הזו כדי למחוק הודעה שנשלחה מאפליקציית Chat. באמצעות אימות משתמשים, אפשר להשתמש בשיטה הזו כדי למחוק הודעה שהמשתמש המאומת שלח. אם המשתמש הוא מנהל המרחב, יכול להיות שתוכלו גם למחוק הודעה שמשתתפים אחרים במרחב שלחו. מידע נוסף זמין במאמר מידע על התפקיד של מנהל/ת המרחב המשותף.

דרישות מוקדמות

Node.js

Python

Java

Apps Script

מחיקת הודעה באמצעות אימות משתמש

כדי למחוק הודעה עם אימות משתמש, צריך להעביר את הפרטים הבאים בבקשה:

  • מציינים את chat.messages היקף ההרשאה.
  • מבצעים קריאה ל-method‏ 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 המרחב. אפשר לקבל את המזהה באמצעות הקריאה ל-method‏ ListSpaces() או מכתובת ה-URL של המרחב.
  • MESSAGE_NAME: המזהה מההודעה name. אפשר לקבל את המזהה מגוף התגובה שמוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או באמצעות שם בהתאמה אישית שהוקצה להודעה בזמן היצירה.

אם הפעולה בוצעה ללא שגיאות, גוף התגובה יהיה ריק, מה שמציין שההודעה נמחקה.

מחיקת הודעה עם אימות של אפליקציה

כדי למחוק הודעה עם אימות אפליקציה, צריך להעביר את הפרטים הבאים בבקשה:

  • מציינים את chat.bot היקף ההרשאה.
  • מבצעים קריאה ל-method‏ 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 המרחב. אפשר לקבל את המזהה באמצעות הקריאה ל-method‏ ListSpaces() או מכתובת ה-URL של המרחב.
  • MESSAGE_NAME: המזהה מההודעה name. אפשר לקבל את המזהה מגוף התגובה שמוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או באמצעות שם בהתאמה אישית שהוקצה להודעה בזמן היצירה.

אם הפעולה בוצעה ללא שגיאות, גוף התגובה יהיה ריק, מה שמציין שההודעה נמחקה.