הודעות ברשימות

במדריך הזה מוסבר איך להשתמש ב-method list במשאב Message של של Google Chat API כדי לראות את רשימת ההודעות במרחב המשותף עם חלוקה לדפים, שאפשר לסנן.

משאב אחד (Message) מייצג טקסט או כרטיס ב-Google Chat. אפשר הודעה אחת (create), get, update או delete ב-Google Chat API: השיטות המתאימות. למידע נוסף על הודעות טקסט וכרטיסי מוצר, אפשר לעיין במאמר סקירה כללית על ההודעות ב-Google Chat

דרישות מוקדמות

Python

  • Python 3.6 ומעלה
  • הכלי לניהול חבילות pip
  • ספריות הלקוח העדכניות של Google. כדי להתקין או לעדכן אותם, מריצים את הפקודה הבאה בממשק שורת הפקודה:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

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

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

  • מציינים את היקף ההרשאה chat.messages.readonly או chat.messages.
  • קוראים לפונקציה שיטת list ב משאב Message.

בדוגמה הבאה מופיעות הודעות במרחב המשותף ב-Chat שנשלחו אחרי 16 במרץ 2023:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_messages_list.py.
  2. צריך לכלול את הקוד הבא ב-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()
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_messages_list.py
    

כש-Chat API מחזיר רשימה של הודעות שנשלחו במרחב המשותף שצוין אחרי 16 במרץ 2023. אם אין הודעות מהבקשה, התשובה ב-Chat API מחזירה אובייקט ריק. כשמשתמשים ב- בממשק REST/HTTP, התגובה מכילה אובייקט JSON ריק, {}.