本指南說明如何使用 Google Chat API Space
資源的 patch()
方法更新聊天室。更新聊天室可變更聊天室的屬性,例如使用者可見的顯示名稱、說明和規範。
如果您是 Google Workspace 管理員,可以呼叫 patch()
方法,更新 Google Workspace 機構中的現有聊天室。
Space
資源代表使用者和 Chat 應用程式可傳送訊息、共用檔案及協作的空間。聊天室分為以下幾種類型:
- 即時訊息 (DM) 是指兩位使用者之間,或使用者與 Chat 應用程式之間的對話。
- 群組對話是指三位以上使用者和即時通訊應用程式之間的對話。
- 具名聊天室是持續存在的空間,可供使用者傳送訊息、分享檔案及協作。
必要條件
Node.js
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Node.js Cloud 用戶端程式庫。
- 為電腦版應用程式
建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json
的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
Python
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Python Cloud 用戶端程式庫。
- 為電腦版應用程式
建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json
的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
Java
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Java Cloud 用戶端程式庫。
- 為電腦版應用程式
建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json
的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
Apps Script
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 建立獨立的 Apps Script 專案,並開啟進階 Chat 服務。
- 選擇支援使用者驗證的授權範圍。
以使用者身分更新空間
如要使用使用者驗證更新 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:
- 指定
chat.spaces
授權範圍。 - 呼叫
UpdateSpace()
方法。在要求中,您會指定空間name
欄位、updateMask
欄位 (內含一或多個要更新的欄位),以及body
欄位 (內含更新後的空間資訊)。
你可以更新顯示名稱、空間類型、記錄狀態等資訊。如要查看可更新的所有欄位,請參閱參考說明文件。
如要更新現有聊天室的 displayName
欄位,請按照下列步驟操作:
Node.js
Python
Java
Apps Script
如要執行這個範例,請將 SPACE_NAME
替換為空間的 name
欄位中的 ID。您可以呼叫 ListSpaces()
方法,或從空間的網址取得 ID。
Google Chat API 會傳回 Space
的執行個體,反映更新內容。
以 Google Workspace 管理員身分更新聊天室
如果您是 Google Workspace 管理員,可以呼叫 UpdateSpace()
方法,更新 Google Workspace 機構中的任何空間。
如要以 Google Workspace 管理員身分呼叫這個方法,請按照下列步驟操作:
如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。
以 Chat 應用程式更新聊天室
應用程式驗證需要管理員核准一次。
如要使用應用程式驗證更新 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:
- 指定
chat.app.spaces
授權範圍。 使用應用程式驗證時,您只能更新由 Chat 應用程式建立的聊天室。 - 呼叫
Space
資源的patch
方法。在要求中,您會指定空間name
欄位、updateMask
欄位 (含一或多個要更新的欄位),以及body
(含更新的空間資訊)。
您可以更新顯示名稱、聊天室類型、記錄狀態、權限設定等項目。如要查看可更新的所有欄位,請參閱參考說明文件。
建立 API 金鑰
如要呼叫開發人員預先發布版 API 方法,您必須使用非公開的開發人員預先發布版 API 探索文件。如要驗證要求,您必須傳遞 API 金鑰。
如要建立 API 金鑰,請開啟應用程式的 Google Cloud 專案,然後執行下列操作:
- 在 Google Cloud 控制台中,依序前往「選單」圖示 >「API 和服務」 >「憑證」。
- 依序按一下「建立憑證」>「API 金鑰」。
- 系統會顯示新的 API 金鑰。
- 按一下「複製」圖示 ,即可複製 API 金鑰,在應用程式的程式碼中使用。您也可以在專案憑證的「API 金鑰」部分找到 API 金鑰。
- 按一下「限制金鑰」,即可更新進階設定並限制 API 金鑰的使用方式。詳情請參閱「套用 API 金鑰限制」一文。
編寫呼叫 Chat API 的指令碼
如要更新現有聊天室的 spaceDetails
欄位,請按照下列步驟操作:
Python
- 在工作目錄中,建立名為
chat_space_update_app.py
的檔案。 在
chat_space_update_app.py
中加入下列程式碼:from google.oauth2 import service_account from apiclient.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.app.spaces"] def main(): ''' Authenticates with Chat API using app authentication, then updates the specified space description and guidelines. ''' # Specify service account details. creds = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY') # Use the service endpoint to call Chat API. result = chat.spaces().patch( # The space to update, and the updated space details. # # Replace {space} with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='spaces/SPACE', updateMask='spaceDetails', body={ 'spaceDetails': { 'description': 'This description was updated with Chat API!', 'guidelines': 'These guidelines were updated with Chat API!' } } ).execute() # Prints details about the updated space. print(result) if __name__ == '__main__': main()
在程式碼中,請替換下列項目:
API_KEY
:您建立的 API 金鑰,用於建構 Chat API 的服務端點。SPACE
,您可透過 Chat API 的spaces.list
方法或聊天室網址取得聊天室名稱。
在工作目錄中,建構並執行範例:
python3 chat_space_update_app.py
Google Chat API 會傳回 Space
資源的執行個體,反映更新內容。
限制和注意事項
- 使用應用程式驗證,
Chat 應用程式:
- 只能在自己建立的聊天室中更新
predefinedPermissionSettings
或permissionSettings
。 - 無法更新「
accessSettings.audience
」。
- 只能在自己建立的聊天室中更新