במדריך הזה מוסבר איך להשתמש ב-method create
במשאב 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.create
,chat.messages.reactions
או היקף ההרשאהchat.messages
. - קוראים לפונקציה
אמצעי תשלום אחד (
create
) ב משאבReaction
. - מגדירים את
parent
לשם המשאב של ההודעה שעליה רוצים להגיב. - מגדירים את
body
(גוף הבקשה) למופע שלReaction
שבהן השדהunicode
הוא אמוג'י רגיל שמיוצג על ידי Unicode String.
בדוגמה הבאה מגיבים להודעה עם האמוג'י 🧧:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_reaction_create.py
. צריך לכלול את הקוד הבא ב-
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()
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: ה-name
של המרחב המשותף שבו פורסמה, אותה ניתן לקבל שיטתspaces.list
מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_reaction_create.py
Chat API מחזיר מופע של
Reaction
שמפרטת את התגובה שנוצרה.