איך מקבלים פרטים על הודעה מסוימת

במדריך הזה מוסבר איך להשתמש ב-method get במשאב 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.
  • קוראים לפונקציה אמצעי תשלום אחד (get) ב משאב Message.
  • מגדירים את name לשם המשאב של ההודעה שרוצים לקבל.

בדוגמה הבאה מופיעה הודעה עם אימות משתמש:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_message_get_user.py.
  2. צריך לכלול את הקוד הבא ב-chat_message_get_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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets 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().get(
    
            # The message to get.
            #
            # 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'
    
        ).execute()
    
        # Prints details about the message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם של מרחב, שאפשר לקבל ממנו ה אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.
    • MESSAGE: שם ההודעה, שאפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.
  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_message_get_user.py
    

Chat API מחזיר מופע של Message שמפרטת את ההודעה שצוינה.

קבלת הודעה באמצעות אימות אפליקציות

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

בדוגמה הבאה מופיעה הודעה עם אימות אפליקציות:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_get_message_app.py.
  2. צריך לכלול את הקוד הבא ב-chat_get_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)
    
    # Get a Chat message.
    result = chat.spaces().messages().get(
    
        # The message to get.
        #
        # 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'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: ה-name של המרחב המשותף שבו פורסמה, אותה ניתן לקבל שיטת spaces.list מ-Chat API או מכתובת ה-URL של מרחב משותף.

    • MESSAGE: שם ההודעה, שאותו אפשר לקבל מגוף התשובה שהוחזר אחרי יצירת הודעה באופן אסינכרוני באמצעות Chat API, או שם מותאם אישית שהוקצה להודעה בזמן היצירה.

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

    python3 chat_get_message_app.py
    

Chat API מחזיר מופע של Message שמפרטת את ההודעה שצוינה.