本指南說明如何使用 Google Chat API Section 資源的 position 方法,變更 Google Chat 中某個區段的排序順序。
詳情請參閱「在 Google Chat 中建立及整理區段」。
必要條件
Python
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Python Cloud 用戶端程式庫。
-
為電腦應用程式建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
變更區塊位置
如要變更含有使用者驗證的區段位置,請在要求中傳遞下列內容:
- 指定
chat.users.sections授權範圍。 - 呼叫
PositionSection方法。 - 在要求主體中,指定要放置的區段
name和新位置:- 將
sortOrder設為絕對位置 (大於 0)。 - 或者,將
relativePosition設為START或END。
- 將
以下範例會將某個區段移至清單開頭:
Python
from google.cloud import chat_v1
def position_section():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.PositionSectionRequest(
name="SECTION_NAME",
relative_position=chat_v1.PositionSectionRequest.Position.START
)
# Make the request
response = client.position_section(request=request)
print(response)
如要執行這個範例,請替換下列項目:
SECTION_NAME:區段的資源名稱。您可以呼叫ListSections方法來取得資源名稱。
Chat API 會傳回更新後的 Section 執行個體。