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

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

משאב אחד (Message) מייצג טקסט או כרטיס ב-Google Chat. אפשר הודעה אחת (create), get, update או delete ב-Google Chat API: השיטות המתאימות. למידע נוסף על הודעות טקסט וכרטיסי מוצר, אפשר לעיין במאמר סקירה כללית על ההודעות ב-Google Chat

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

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

Python

  • Python 3.6 ומעלה
  • הכלי לניהול חבילות pip
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

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

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

בדוגמה הבאה נמחק הודעה עם אימות משתמש:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_message_delete_user.py
  2. צריך לכלול את הקוד הבא ב-chat_message_delete_user.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a message.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().delete(
    
            # The message to delete.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints response to the Chat API call.
        # When deleting a message, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.
    • MESSAGE: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_message_delete_user.py
    

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

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

כדי למחוק הודעה עם app authentication, מעבירים את הבא בבקשה שלך:

בדוגמה הבאה נמחק הודעה עם אימות אפליקציות:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_delete_message_app.py
  2. צריך לכלול את הקוד הבא ב-chat_delete_message_app.py:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Delete a Chat message.
    result = chat.spaces().messages().delete(
    
      # The message to delete.
      #
      # Replace SPACE with a space name.
      # Obtain the space name from the spaces resource of Chat API,
      # or from a space's URL.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE'
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    # When deleting a message, the response body is empty.
    print(result)
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: ה-name של המרחב המשותף שבו פורסמה, אותה ניתן לקבל שיטת spaces.list מ-Chat API או מכתובת ה-URL של מרחב משותף.
    • MESSAGE: שם ההודעה, שאותו אפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_delete_message_app.py
    

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