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

מדריך זה מסביר איך להשתמש בשיטה get במשאב 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.bot יכול לקבל הודעות שנשלחו לאפליקציה.

קבלת הודעה עם אימות משתמש

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

  • צריך לציין את היקף ההרשאה chat.messages.readonly או chat.messages.
  • קוראים ל-method 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 created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

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

    python3 chat_message_get_user.py
    

ה-Chat API מחזיר מופע של Message שכולל פרטים על ההודעה שצוינה.

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

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

  • צריך לציין את היקף ההרשאה של chat.bot.
  • קוראים ל-method get במשאב Message.
  • מגדירים את name לשם המשאב של ההודעה שרוצים לקבל.

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

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 של המרחב המשותף שבו ההודעה פורסמה. אפשר לקבל אותו דרך ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

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

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

    python3 chat_get_message_app.py
    

ה-Chat API מחזיר מופע של Message שכולל פרטים על ההודעה שצוינה.