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

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

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

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

Python

  • Python 3.6 ומעלה
  • הכלי pip לניהול חבילות
  • ספריות הלקוח העדכניות של Google ל-Python. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • פרויקט ב-Google Cloud שמופעל ומוגדר בו Google Chat API. במאמר איך מפתחים אפליקציות ב-Google Chat מוסבר איך עושים את זה.
  • הוגדרה הרשאה לאפליקציית Chat. מחיקת הודעה תומכת בשתי שיטות האימות הבאות:

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

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

  • מציינים את היקף ההרשאה chat.messages.
  • קוראים ל-method delete במשאב Message.
  • מגדירים את name לשם המשאב של ההודעה שרוצים למחוק.

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

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: שם המרחב, שאפשר לקבל באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.
    • MESSAGE: שם ההודעה, שאפשר לקבל מגוף התשובה שמוחזר אחרי שיוצרים הודעה באופן אסינכרוני באמצעות Chat API, או עם השם המותאם אישית שהוקצה להודעה בזמן יצירת ההודעה.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_message_delete_user.py
    

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

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

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

  • מציינים את היקף ההרשאה chat.bot.
  • קוראים ל-method delete במשאב Message.
  • מגדירים את name לשם המשאב של ההודעה שרוצים למחוק.

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

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 של המרחב שבו ההודעה פורסמה. אפשר לקבל אותו באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.
    • MESSAGE: שם ההודעה, שאפשר לקבל מגוף התשובה שמוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או עם השם המותאם אישית שהוקצה להודעה בזמן יצירת ההודעה.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_delete_message_app.py
    

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