איך מצמידים הודעות ומבטלים את ההצמדה במרחבים ב-Google Chat

באמצעות MessagePin resource ב-Chat API, האפליקציה יכולה להצמיד הודעות, לבטל את ההצמדה של הודעות ולקבל רשימה של כל ההודעות המוצמדות במרחבים ב-Google Chat. כל המשתתפים במרחב יכולים לראות את ההודעות המוצמדות בממשק של Chat. ממשק ה-API הזה עוזר לאפליקציה לנהל הודעות מוצמדות בשם המשתמש.

מידע על הצמדת הודעות במרחבים זמין במאמר איך מצמידים הודעות, קבצים וקישורים במרחבים ובצ'אטים.

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

Node.js

שיקולים

  • אפשר להצמיד רק הודעות שכבר קיימות במרחב. אי אפשר ליצור הודעה חדשה ולהצמיד אותה באותה בקשה.
  • אי אפשר להצמיד הודעות שרק אתם יכולים לראות. לדוגמה, אי אפשר להצמיד הודעות פרטיות שמתקבלות מאפליקציה.
  • בכל מרחב ב-Chat אפשר להצמיד עד 100 הודעות. אם האפליקציה מנסה להצמיד הודעה 101, ה-API מחזיר שגיאה.

הצמדת הודעה

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

  • מציינים את היקף ההרשאות של chat.spaces.pins או chat.spaces.
  • קוראים לפונקציה messagePins.create.
  • מציינים את parent (שם המרחב) ומספקים body עם שם המשאב message של ההודעה המקורית.

בדוגמה הבאה מוצגת הודעה שמוצמדת למרחב:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_pin_message.py.
  2. מוסיפים את הקוד הבא ל-chat_pin_message.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then pins a message in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Pin a message.
        result = service.spaces().messagePins().create(
            # The space to pin the message in.
            #
            # Replace SPACE with a space ID or name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The message to pin.
            body={
                'message': 'spaces/SPACE/messages/MESSAGE'
            }
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את הפרטים הבאים:

    • SPACE: המזהה מname המרחב.
    • MESSAGE: המזהה מתוך name של ההודעה.
  4. בספריית העבודה, מפתחים ומריצים את הדוגמה:

    python3 chat_pin_message.py

ביטול ההצמדה של הודעה

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

  • מציינים את היקף ההרשאות של chat.spaces.pins או chat.spaces.
  • קוראים לפונקציה messagePins.delete.
  • מגדירים את name לשם המשאב של MessagePin שרוצים למחוק.

כדי לבטל את הצמדת ההודעה:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_unpin_message.py.
  2. מוסיפים את הקוד הבא ל-chat_unpin_message.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then unpins a message from a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Unpin a message.
        result = service.spaces().messagePins().delete(
            # The resource name of the message pin to delete.
            #
            # Replace SPACE with a space ID or name, and MESSAGE with the message ID.
            name='spaces/SPACE/messagePins/MESSAGE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את הפרטים הבאים:

    • SPACE: המזהה מname המרחב.
    • MESSAGE: המזהה מתוך name של ההודעה.
  4. בספריית העבודה, מפתחים ומריצים את הדוגמה:

    python3 chat_unpin_message.py

איך רואים את רשימת ההודעות המוצמדות

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

  • מציינים אחד מהיקפי הקריאה: chat.spaces.pins.readonly, chat.spaces.readonly, chat.spaces.pins או chat.spaces.
  • קוראים לפונקציה messagePins.list.
  • מציינים את parent כשם המרחב שממנו רוצים לאחזר את ההודעות המוצמדות.

כדי לראות את ההודעות המוצמדות:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_list_pinned_messages.py.
  2. מוסיפים את הקוד הבא ל-chat_list_pinned_messages.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists pinned messages in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # List pinned messages.
        result = service.spaces().messagePins().list(
            # The space to list pinned messages from.
            #
            # Replace SPACE with a space ID or name.
            parent='spaces/SPACE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. בקוד, מחליפים את SPACE במזהה המרחב מתוך name של המרחב.

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

    python3 chat_list_pinned_messages.py