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

במדריך הזה מוסבר איך להשתמש ב-method delete במשאב Reaction של Google Chat API כדי למחוק תגובה מהודעה, כמו 👍, 🚲 ו-🌞. מחיקת תגובה לא מוחקת את ההודעה.

משאב אחד (Reaction) מייצג אמוג'י שאנשים יכולים להשתמש בו כדי להגיב להודעה, למשל 👍, 🚲, וגם 🌞.

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

Python

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

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

איך מוחקים תגובות באמוג'י

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

  • צריך לציין את ההרשאה chat.messages.reactions או את ההרשאה chat.messages היקף.
  • קוראים לפונקציה אמצעי תשלום אחד (delete) ב משאב Reaction.
  • מגדירים את name כשם המשאב של התגובה שרוצים למחוק.

הדוגמה הבאה מוחקת את התגובה 😀

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_reaction_delete.py.
  2. צריך לכלול את הקוד הבא ב-chat_reaction_delete.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.reactions"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a reaction to 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().reactions().delete(
    
            # The reaction 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.
            #
            # Replace REACTION with a reaction name.
            # Obtain the reaction name from the reaction resource of Chat API.
            name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION'
    
        ).execute()
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

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

    python3 chat_reaction_delete.py
    

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