透過 Chat API 中的 MessagePin 資源,應用程式可以釘選及取消釘選訊息,並取得 Google Chat 聊天室中所有已釘選訊息的清單。所有聊天室成員都可以在 Chat 介面中看到置頂訊息。這項 API 可協助應用程式代表使用者管理已釘選的訊息。
如要瞭解使用者如何在聊天室中置頂訊息,請參閱「將訊息、檔案和連結置頂在聊天室及訊息中」。
必要條件
Node.js
- 公司或企業專用 Google Workspace 帳戶,且可存取 Google Chat。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Node.js Cloud 用戶端程式庫。
-
為電腦應用程式建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
注意事項
- 你只能將聊天室中現有的訊息置頂,無法在同一個要求中建立新訊息並置頂。
- 你無法釘選僅供自己查看的訊息,例如從應用程式收到的私人訊息。
- 每個 Chat 聊天室最多可釘選 100 則訊息。如果應用程式嘗試釘選第 101 則訊息,API 會傳回錯誤。
將訊息置頂
如要使用使用者驗證功能釘選訊息,請在要求中傳遞下列內容:
- 指定
chat.spaces.pins或chat.spaces授權範圍。 - 呼叫
messagePins.create。 - 指定
parent(聊天室名稱),並提供body和原始訊息的message資源名稱。
以下範例會在聊天室中釘選訊息:
Python
- 在工作目錄中,建立名為
chat_pin_message.py的檔案。 在
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()在程式碼中,請替換下列項目:
在工作目錄中建構及執行範例:
python3 chat_pin_message.py
取消置頂訊息
如要使用使用者驗證取消釘選訊息,請在要求中傳遞下列項目:
- 指定
chat.spaces.pins或chat.spaces授權範圍。 - 呼叫
messagePins.delete。 - 將
name設為要刪除的MessagePin資源名稱。
如要取消釘選訊息,請按照下列步驟操作:
Python
- 在工作目錄中,建立名為
chat_unpin_message.py的檔案。 在
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()在程式碼中,請替換下列項目:
在工作目錄中建構及執行範例:
python3 chat_unpin_message.py
取得置頂訊息清單
如要取得您有權存取的聊天室中已釘選的訊息清單,請使用使用者驗證,並在要求中傳遞下列項目:
- 指定其中一個讀取範圍:
chat.spaces.pins.readonly、chat.spaces.readonly、chat.spaces.pins或chat.spaces。 - 呼叫
messagePins.list。 - 指定
parent做為聊天室名稱,從中擷取已釘選的訊息。
如要列出已釘選的訊息,請按照下列步驟操作:
Python
- 在工作目錄中,建立名為
chat_list_pinned_messages.py的檔案。 在
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()在程式碼中,將
SPACE換成空間的name中的空間 ID。在工作目錄中建構及執行範例:
python3 chat_list_pinned_messages.py