在 Google Chat 聊天室中固定或取消固定訊息

透過 Chat API 中的 MessagePin 資源,應用程式可以釘選及取消釘選訊息,並取得 Google Chat 聊天室中所有已釘選訊息的清單。所有聊天室成員都可以在 Chat 介面中看到置頂訊息。這項 API 可協助應用程式代表使用者管理已釘選的訊息。

如要瞭解使用者如何在聊天室中置頂訊息,請參閱「將訊息、檔案和連結置頂在聊天室及訊息中」。

必要條件

Node.js

注意事項

  • 你只能將聊天室中現有的訊息置頂,無法在同一個要求中建立新訊息並置頂。
  • 你無法釘選僅供自己查看的訊息,例如從應用程式收到的私人訊息。
  • 每個 Chat 聊天室最多可釘選 100 則訊息。如果應用程式嘗試釘選第 101 則訊息,API 會傳回錯誤。

將訊息置頂

如要使用使用者驗證功能釘選訊息,請在要求中傳遞下列內容:

  • 指定 chat.spaces.pinschat.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 的 ID。
    • MESSAGE:訊息 name 中的 ID。
  4. 在工作目錄中建構及執行範例:

    python3 chat_pin_message.py

取消置頂訊息

如要使用使用者驗證取消釘選訊息,請在要求中傳遞下列項目:

  • 指定 chat.spaces.pinschat.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 的 ID。
    • MESSAGE:訊息 name 中的 ID。
  4. 在工作目錄中建構及執行範例:

    python3 chat_unpin_message.py

取得置頂訊息清單

如要取得您有權存取的聊天室中已釘選的訊息清單,請使用使用者驗證,並在要求中傳遞下列項目:

  • 指定其中一個讀取範圍:chat.spaces.pins.readonlychat.spaces.readonlychat.spaces.pinschat.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 中的空間 ID。

  4. 在工作目錄中建構及執行範例:

    python3 chat_list_pinned_messages.py