במדריך הזה מוסבר איך להשתמש ב-method patch
במשאב Message
של
להשתמש ב-Google Chat API כדי לעדכן הודעות טקסט או הודעות בכרטיס המרחב המשותף. עדכון של
כדי לשנות מאפיינים של הודעה, למשל מה שכתוב בה או תוכן של הודעה
. אפשר גם לצרף הודעת טקסט בהתחלה
הודעה בכרטיס או לצרף כרטיס להודעת טקסט.
Chat API תומך גם
השיטה update
,
אבל מומלץ מאוד לקרוא ל-
אמצעי תשלום אחד (patch
)
כי הוא משתמש בבקשת HTTP PATCH
בזמן
update
משתמש
בקשת HTTP של PUT
מידע נוסף זמין במאמר הבא:
הסעיפים PATCH
ו-PUT
של AIP-134.
ב-Chat API, הודעה ב-Chat מיוצגת על ידי
משאב אחד (Message
).
משתמשי Chat יכולים לשלוח רק הודעות שמכילות טקסט,
אפליקציות צ'אט יכולות להשתמש בתכונות רבות נוספות של העברת הודעות, כולל
הצגת ממשקי משתמש סטטיים או אינטראקטיביים, איסוף מידע
משתמשים ולהעביר הודעות באופן פרטי. למידע נוסף על התכונה 'העברת הודעות'
שזמינות ל-Chat API,
סקירה כללית על ההודעות ב-Google Chat
דרישות מוקדמות
Python
- עסק או ארגון חשבון Google Workspace עם גישה אל Google Chat.
- מגדירים את הסביבה:
- יוצרים פרויקט ב-Google Cloud.
- מגדירים את מסך ההסכמה של OAuth.
- מפעילים ומגדירים את Google Chat API עם שם, הסמל והתיאור של אפליקציית Chat.
- להתקין את Python ספריית הלקוח של Google API.
- יוצרים פרטי כניסה לגישה בהתאם לאופן שבו רוצים לבצע אימות ב-Google Chat API
בקשה:
- כדי לבצע אימות כמשתמשי Chat:
יצירת מזהה לקוח ב-OAuth
פרטי כניסה ולשמור את פרטי הכניסה כקובץ JSON בשם
client_secrets.json
לספרייה המקומית שלך. - כדי לבצע אימות כאפליקציית Chat:
יצירת חשבון שירות
פרטי כניסה ולשמור את פרטי הכניסה כקובץ JSON בשם
credentials.json
.
- כדי לבצע אימות כמשתמשי Chat:
יצירת מזהה לקוח ב-OAuth
פרטי כניסה ולשמור את פרטי הכניסה כקובץ JSON בשם
- בוחרים את היקף ההרשאה בהתאם למה שרוצים לבצע בו אימות כמשתמשים או אפליקציית Chat.
עדכון הודעת טקסט או צירוף הודעת טקסט בהתחלה להודעה בכרטיס באמצעות אימות משתמש
כדי לעדכן הודעת טקסט עם אימות משתמש, אישור מעבר את הפרטים הבאים בבקשה שלכם:
- היקף ההרשאה
chat.messages
. name
של ההודעה שצריך לעדכן.updateMask='text'
body
שמציין את ההודעה המעודכנת.
אם ההודעה המעודכנת הודעה בכרטיס, הודעת הטקסט מופיעה בתחילת הודעת הכרטיס (שממשיכת להופיע).
כך מעדכנים הודעת טקסט, או להוסיף הודעת טקסט בהתחלה הודעה בכרטיס עם אימות המשתמש:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_update_text_message_user.py
צריך לכלול את הקוד הבא ב-
chat_update_text_message_user.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"] def main(): ''' Authenticates with Chat API via user credentials, then updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_update_text_message_user.py
באמצעות אימות אפליקציה, אפשר לעדכן הודעת טקסט או לצרף הודעת טקסט בהודעה בכרטיס
כדי לעדכן הודעת טקסט עם אימות אפליקציות, מעבירים את הפרטים הבאים בבקשה:
- היקף ההרשאה
chat.bot
. name
של ההודעה שצריך לעדכן.updateMask='text'
body
שמציין את ההודעה המעודכנת.
אם ההודעה המעודכנת היא הודעה בכרטיס, הודעת הטקסט מופיעה בתחילת הודעת הכרטיס (שממשיכת להופיע).
כך מעדכנים הודעת טקסט להודעת טקסט, או להוסיף הודעת טקסט בהתחלה הודעה בכרטיס עם אימות אפליקציות:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_update_text_message_app.py
צריך לכלול את הקוד הבא ב-
chat_update_text_message_app.py
:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Print Chat API's response in your command line interface. print(result)
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_update_text_message_app.py
עדכון הודעה בכרטיס או צירוף הודעה בכרטיס להודעת טקסט
כדי לעדכן הודעה בכרטיס, מעבירים את הפרטים הבאים בבקשה:
- היקף ההרשאה
chat.bot
. צריך לעדכן הודעה בכרטיס אימות אפליקציות. name
של ההודעה שצריך לעדכן.updateMask='cardsV2'
body
שמציין את ההודעה המעודכנת.
אם ההודעה המעודכנת הודעת טקסט, לאחר מכן מצורף כרטיס להודעת הטקסט (שממשיכת להופיע). אם היא עצמה card, הכרטיס המוצג עודכן.
כך מעדכנים הודעה הודעה בכרטיס:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_update_card_message.py
צריך לכלול את הקוד הבא ב-
chat_update_card_message.py
:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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. name='spaces/SPACE/messages/MESSAGE', updateMask='cardsV2', body= { 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Print Chat API's response in your command line interface. print(result)
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_update_card_message.py
Chat API מחזיר מופע של
Message
שבה מופיעה ההודעה שמתעדכנת.
עדכון הודעה עם מספר נתיבי שדות בו-זמנית
כשהודעה מתעדכנת, אפשר לעדכן מספר נתיבים בשדה ההודעה
בזמן האימון. לדוגמה, בבקשת עדכון אפשר לציין שינוי
נתיבי השדות text
ו-cardsv2
בו-זמנית, מה שמעדכן את שני הנתיבים
את הודעות הטקסט והכרטיס של ההודעה. אם ההודעה כוללת רק טקסט ולא כוללת כרטיס,
נוסף להודעה. למידע נוסף על נתיבי השדות הנתמכים:
לראות
updateMask
פרמטרים.
כדי לעדכן גם את
text
וגם
card
של הודעה עם
אימות משתמש,
מעבירים את הפרטים הבאים בבקשה:
- היקף ההרשאה
chat.messages
. name
של ההודעה שצריך לעדכן.updateMask
שמציין את הנתיבים של שדות ההודעה לעדכון, מופרדים בפסיקים:updateMask='text', 'cardsV2'
.body
שמציין את ההודעה המעודכנת, כולל כל השדות המעודכנים .
כך מעדכנים את נתיבי השדות text
ו-cardsV2
הודעה עם
אימות משתמש:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_update_text_message_user.py
צריך לכלול את הקוד הבא ב-
chat_update_text_message_user.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"] def main(): ''' Authenticates with Chat API via user credentials, then updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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. name='spaces/SPACE/messages/MESSAGE', updateMask='text,cardsV2', body= {'text': 'Updated message!', 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
בקוד, מחליפים את מה שכתוב בשדות הבאים:
SPACE
: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list
) מ-Chat API או מכתובת ה-URL של מרחב משותף.MESSAGE
: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_update_text_message_user.py
נושאים קשורים
- איך מעצבים הודעות?
- איך מוחקים הודעות?
- איך מקבלים פרטים על הודעות?
- הצגת רשימה של ההודעות במרחבים משותפים
- לשלוח הודעה.