איך מגיבים להודעה

במדריך הזה מוסבר איך משתמשים בשיטה create במשאב 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.create, chat.messages.reactions או chat.messages.

איך מגיבים להודעות

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

  • מציינים את היקף ההרשאה chat.messages.reactions.create, chat.messages.reactions או chat.messages.
  • קוראים ל-method create במשאב Reaction.
  • מגדירים את parent לשם המשאב של ההודעה שעליה רוצים להגיב.
  • מגדירים את body (גוף הבקשה) למופע של Reaction שבו השדה unicode הוא אמוג'י סטנדרטי שמיוצג על ידי מחרוזת Unicode.

בדוגמה הבאה מגיבים להודעה עם האמוג'י 🧧:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_reaction_create.py.
  2. צריך לכלול את הקוד הבא ב-chat_reaction_create.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.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then creates 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().create(
    
            # The message to create a reaction to.
            #
            # 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.
            parent = 'spaces/SPACE/messages/MESSAGE',
    
            # The reaction to the message.
            body = {
    
                'emoji': {
    
                    # A standard emoji represented by a unicode string.
                    'unicode': '😀'
                }
    
            }
    
        ).execute()
    
        # Prints details about the created reaction.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: ה-name של המרחב שבו ההודעה פורסמה. אפשר לקבל אותו באמצעות ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב.

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

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_reaction_create.py
    

ה-Chat API מחזיר מופע של Reaction שבו מפורט התגובה שנוצרה.