במדריך הזה נסביר איך משתמשים בשיטה list
במשאב Reaction
של Google Chat API כדי לקבל רשימה של התגובות להודעה, כמו 👍, 🚲 ו-🌞.
המשאבים מסוג Reaction
מייצגים אמוג'י שאנשים יכולים להשתמש בו כדי להגיב להודעה, כמו 👍, 🚲 ו-🌞.
דרישות מוקדמות
Python
- עסק או ארגון חשבון Google Workspace עם גישה אל Google Chat.
- מגדירים את הסביבה:
- יוצרים פרויקט ב-Google Cloud.
- מגדירים את מסך ההסכמה של OAuth.
- מפעילים ומגדירים את Google Chat API עם שם, סמל ותיאור לאפליקציית Chat.
- להתקין את Python ספריית הלקוח של Google API.
-
יוצרים פרטי כניסה למזהה הלקוח ב-OAuth לאפליקציה למחשב. כדי להריץ את הדוגמה
מדריך, שומרים את פרטי הכניסה כקובץ JSON בשם
client_secrets.json
הספרייה המקומית.
- בחירת היקף הרשאה שתומך באימות משתמשים.
הצגת רשימה של התגובות
כדי לקבל רשימה של התגובות להודעה, צריך להעביר את הפרמטרים הבאים בבקשה:
- צריך לציין את
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
- בספריית העבודה, יוצרים קובץ בשם
chat_reactions_list.py
. צריך לכלול את הקוד הבא ב-
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()
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_reactions_list.py
Chat API מחזיר מערך של תגובות שמחולק לדפים.