הצגת רשימה של תגובות להודעות

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

הצגת רשימה של תגובות

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

  • צריך לציין את היקף ההרשאה chat.messages.reactions.readonly, chat.messages.reactions, chat.messages.readonly או chat.messages.
  • קוראים לפונקציה [list method]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) במשאב Reaction.

הדוגמה הבאה מפרטת את התגובות להודעה מסוימת:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_reactions_list.py.
  2. יש לכלול את הקוד הבא ב-chat_reactions_list.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists reactions 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().list(
    
            # The message to list reactions 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'
    
        ).execute()
    
        # Prints details about the created reactions.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

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

    python3 chat_reactions_list.py
    

ה-API של Chat מחזיר מערך מספור של תגובות.