本指南說明如何在 Google Chat API 的 membership
資源上使用 patch
方法,變更成員資格的屬性,例如將聊天室成員變更為聊天室管理員,或者將聊天室管理員變更為聊天室成員。
Membership
資源代表使用者或 Google Chat 應用程式是否收到聊天室的邀請、部分或缺席。
Python
- Python 3.6 或更高版本
- pip 套件管理工具
最新的 Python 專用 Google 用戶端程式庫。如要安裝或更新,請在指令列介面中執行下列指令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
已為 Chat 應用程式設定授權。如要更新成員資格,需要使用
chat.memberships
授權範圍進行使用者驗證;如要將資料匯入 Chat,則需要chat.import
授權範圍。
Node.js
- Node.js 與 npm
最新的 Node.js 專用 Google 用戶端程式庫。如要安裝,請在指令列介面中執行下列指令:
npm install @google-cloud/local-auth @googleapis/chat
- 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
已為 Chat 應用程式設定授權。如要更新成員資格,需要使用
chat.memberships
授權範圍進行使用者驗證;如要將資料匯入 Chat,則需要chat.import
授權範圍。
Apps Script
- 擁有可存取 Google Chat 的 Google Workspace 帳戶。
- 已發布的 Chat 應用程式。如要建構 Chat 應用程式,請按照這個quickstart操作。
- 已為 Chat 應用程式設定授權。如要更新成員資格,需要使用
chat.memberships
授權範圍進行使用者驗證;如要將資料匯入 Chat,則需要chat.import
授權範圍。
更新會員資格
如要更新聊天室成員資格,請在要求中傳遞以下內容:
- 指定
chat.memberships
授權範圍。 - 呼叫
Membership
資源上的patch
方法,並傳遞要更新的成員資格name
,以及指定更新成員屬性的updateMask
和body
。 updateMask
會指定要更新的成員面向,包括:role
:Chat 聊天室中的使用者角色,決定使用者可在聊天室中執行的動作。可能的值包括:ROLE_MEMBER
:聊天室成員。使用者擁有基本權限,例如傳送訊息到聊天室。在 1:1 和未命名的群組對話中 所有人都具備這個角色ROLE_MANAGER
:聊天室管理員。使用者俱備所有基本權限,以及可讓他們管理聊天室的管理員權限,例如新增或移除成員。僅支援spaceType
為SPACE
的聊天室 (具名空格)。
將一般聊天室成員設為聊天室管理員
以下範例會在指定更新的成員屬性的 body
中指定 role
做為 ROLE_MANAGER
,藉此將一般聊天室成員設為聊天室管理員:
Python
- 在工作目錄中,建立名為
chat_membership_update.py
的檔案。 在
chat_membership_update.py
中加入下列程式碼:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.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.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then updates a specified space member to change it from a regular member to a space manager. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().members().patch( # The membership to update, and the updated role. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBERSHIP with a membership name. # Obtain the membership name from the membership of Chat API. name='spaces/SPACE/members/MEMBERSHIP', updateMask='role', body={'role': 'ROLE_MANAGER'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()
在程式碼中,替換以下內容:
SPACE
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MEMBERSHIP
:成員名稱,您可以透過 Chat API 的spaces.members.list
方法取得此名稱。
在工作目錄中,建構並執行範例:
python3 chat_membership_update.py
Node.js
- 在工作目錄中,建立名為
chat_membership_update.js
的檔案。 在
chat_membership_update.js
中加入下列程式碼:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a membership in a Chat space to change it from * a space member to a space manager. * @return {!Promise<!Object>} */ async function updateSpace() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.spaces.patch({ /** * The membership to update, and the updated role. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. * * Replace MEMBERSHIP with a membership name. * Obtain the membership name from the membership of Chat API. */ name: 'spaces/SPACE/members/MEMBERSHIP', updateMask: 'role', requestBody: { role: 'ROLE_MANAGER' } }); } /** * Use the service endpoint to call Chat API. */ updateSpace().then(console.log);
在程式碼中,替換以下內容:
SPACE
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MEMBERSHIP
:成員名稱,您可以透過 Chat API 的spaces.members.list
方法取得此名稱。
在工作目錄中,建構並執行範例:
python3 chat_membership_update.js
Apps Script
本範例使用進階 Chat 服務呼叫 Chat API。
將
chat.memberships
授權範圍新增至 Apps Script 專案的appsscript.json
檔案:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
將以下函式新增至 Apps Script 專案的程式碼:
/** * Updates a membership from space member to space manager. * @param {string} memberName The resource name of the membership. */ function updateMembershipToSpaceManager(memberName) { try { const body = {'role': 'ROLE_MANAGER'}; Chat.Spaces.Members.patch(memberName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to create message with error %s', err.message); } }
Google Chat API 會將指定成員資格變更為聊天室管理員,並傳回詳述變更的 Membership
執行個體。
將聊天室管理員設為一般成員
以下範例會在指定更新的成員屬性的 body
中指定 role
做為 ROLE_MEMBER
,藉此將聊天室管理員設為一般聊天室成員:
Python
- 在工作目錄中,建立名為
chat_membership_update.py
的檔案。 在
chat_membership_update.py
中加入下列程式碼:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.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.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then updates a specified space member to change it from a regular member to a space manager. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().members().patch( # The membership to update, and the updated role. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBERSHIP with a membership name. # Obtain the membership name from the membership of Chat API. name='spaces/SPACE/members/MEMBERSHIP', updateMask='role', body={'role': 'ROLE_MEMBER'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()
在程式碼中,替換以下內容:
SPACE
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MEMBERSHIP
:成員名稱,您可以透過 Chat API 的spaces.members.list
方法取得此名稱。
在工作目錄中,建構並執行範例:
python3 chat_membership_update.py
Node.js
- 在工作目錄中,建立名為
chat_membership_update.js
的檔案。 在
chat_membership_update.js
中加入下列程式碼:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a membership in a Chat space to change it from * a space manager to a space member. * @return {!Promise<!Object>} */ async function updateSpace() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.spaces.patch({ /** * The membership to update, and the updated role. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. * * Replace MEMBERSHIP with a membership name. * Obtain the membership name from the membership of Chat API. */ name: 'spaces/SPACE/members/MEMBERSHIP', updateMask: 'role', requestBody: { role: 'ROLE_MEMBER' } }); } /** * Use the service endpoint to call Chat API. */ updateSpace().then(console.log);
在程式碼中,替換以下內容:
SPACE
:聊天室名稱,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MEMBERSHIP
:成員名稱,您可以透過 Chat API 的spaces.members.list
方法取得此名稱。
在工作目錄中,建構並執行範例:
python3 chat_membership_update.js
Apps Script
本範例使用進階 Chat 服務呼叫 Chat API。
將
chat.memberships
授權範圍新增至 Apps Script 專案的appsscript.json
檔案:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships" ]
將以下函式新增至 Apps Script 專案的程式碼:
/** * Updates a membership from space manager to space member. * @param {string} memberName The resource name of the membership. */ function updateMembershipToSpaceMember(memberName) { try { const body = {'role': 'ROLE_MEMBER'}; Chat.Spaces.Members.patch(memberName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to create message with error %s', err.message); } }
Google Chat API 會將指定成員資格變更為聊天室管理員,並傳回詳述變更的 Membership
執行個體。