במדריך הזה מוסבר איך משתמשים בשיטה list
במשאב Message
של Google Chat API כדי להציג רשימת הודעות מחלוקה לדפים שניתנות לסינון.
המשאב Message
מייצג הודעות טקסט או כרטיס ב-Google Chat. אפשר create
, get
, update
או delete
הודעה ב-Google Chat API באמצעות קריאה לשיטות מתאימות. למידע נוסף על הודעות טקסט בכרטיסים, תוכלו לקרוא את הסקירה הכללית על הודעות ב-Google Chat.
דרישות מוקדמות
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.readonly
אוchat.messages
.
הצגת רשימה של הודעות
כדי להציג רשימה של הודעות עם אימות משתמשים, צריך להעביר בבקשה את הפרטים הבאים:
- צריך לציין את היקף ההרשאה
chat.messages.readonly
אוchat.messages
. - קוראים ל-method
list
במשאבMessage
.
בדוגמה הבאה מוצגות ההודעות במרחב ב-Chat שנשלחו אחרי 16 במרץ 2023:
Python
- בספריית העבודה, יוצרים קובץ בשם
chat_messages_list.py
. יש לכלול את הקוד הבא ב-
chat_messages_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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists messages in a space sent after March 16, 2023. ''' # 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().list( # The space for which to list messages. parent = 'spaces/SPACE', # An optional filter that returns messages # created after March 16, 2023. filter = 'createTime > "2023-03-16T00:00:00-00:00"' ).execute() # Prints the list of messages. print(result) if __name__ == '__main__': main()
בקוד, מחליפים את
SPACE
בשם של מרחב משותף, שאותו אפשר לקבל מה-methodspaces.list
ב-Chat API או מכתובת ה-URL של המרחב המשותף.בספריית העבודה, יוצרים ומריצים את הדוגמה:
python3 chat_messages_list.py
ב-Google Chat API תוצג רשימת הודעות שנשלחו במרחב המשותף שצוין אחרי 16 במרץ 2023.
נושאים קשורים
- עיצוב הודעה.
- איך מוחקים הודעות?
- איך מקבלים פרטים על ההודעה?
- רוצים לדעת איך מעדכנים את ההודעה?
- לשלוח הודעה.