本指南說明如何在 Google Chat API 的 membership
資源上使用 create
方法,邀請或將使用者、Google 群組或 Chat 應用程式新增至聊天室,也稱為「建立成員」。建立成員時,如果指定成員關閉了自動接受政策,系統就會傳送邀請,對方必須接受聊天室邀請才能加入。否則,建立成員時,系統會將成員直接新增至指定聊天室。
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.memberships.app
授權範圍的使用者驗證。
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.memberships.app
授權範圍的使用者驗證。
邀請或新增使用者至聊天室
如要邀請或新增使用者至聊天室,請在要求中傳遞以下內容:
- 指定
chat.memberships
授權範圍。 - 呼叫
membership
資源上的create
方法。 - 將
parent
設為要在其中建立成員資格的聊天室資源名稱。 - 將
member
設為users/{user}
,其中{user}
是您要建立成員資格的使用者,且符合以下任一條件:
以下範例將使用者新增至聊天室:
Python
- 在工作目錄中,建立名為
chat_membership_user_create.py
的檔案。 在
chat_membership_user_create.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 adds a user to a Chat space by creating a membership. ''' # 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().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Specify which user the membership is for. body = { 'member': { 'name':'users/USER', 'type': 'HUMAN' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,可以從 Chat API 的spaces.list
方法或聊天室網址取得。USER
:使用者 ID。
在工作目錄中建構並執行範例:
python3 chat_membership_user_create.py
Node.js
- 在工作目錄中,建立名為
add-user-to-space.js
的檔案。 在
add-user-to-space.js
中加入下列程式碼:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the user to the Chat space. * @return {!Promise<!Object>} */ async function addUserToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/USER', type: 'HUMAN'}} }); } addUserToSpace().then(console.log);
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,可以從 Chat API 的spaces.list
方法或聊天室網址取得。USER
:使用者 ID。
在工作目錄中執行範例:
node add-user-to-space.js
Chat API 會傳回 membership
的執行個體,詳細說明已建立的使用者成員資格。
邀請或新增 Google 群組至聊天室
如要邀請或將 Google 群組新增至聊天室,請在要求中傳遞以下內容:
- 指定
chat.memberships
授權範圍。 - 呼叫
membership
資源上的create
方法。 - 將
parent
設為要在其中建立成員資格的聊天室資源名稱。 - 將
groupMember
設為groups/{group}
,其中{group}
是您要為其建立成員資格的群組 ID。您可以使用 Cloud Identity API 擷取群組 ID。舉例來說,如果 Cloud Identity API 傳回名為groups/123456789
的群組,請將membership.groupMember.name
設為groups/123456789
。
Google 網路論壇只能新增至已命名聊天室,因此無法加入群組通訊或即時訊息。以下範例會將群組新增至已命名的聊天室:
Python
- 在工作目錄中,建立名為
chat_membership_group_create.py
的檔案。 在
chat_membership_group_create.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 adds a group to a Chat space by creating a membership. ''' # 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().create( # The named space in which to create a membership. parent = 'spaces/SPACE', # Specify which group the membership is for. body = { 'groupMember': { 'name':'groups/GROUP', } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,可以從 Chat API 的spaces.list
方法或聊天室網址取得。GROUP
:群組 ID。
在工作目錄中建構並執行範例:
python3 chat_membership_group_create.py
Node.js
- 在工作目錄中,建立名為
add-group-to-space.js
的檔案。 在
add-group-to-space.js
中加入下列程式碼:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the group to the Chat space. * @return {!Promise<!Object>} */ async function addUserToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {groupMember: {name: 'groups/GROUP'}} }); } addUserToSpace().then(console.log);
請在程式碼中替換下列內容:
SPACE
:聊天室名稱,可以從 Chat API 的spaces.list
方法或聊天室網址取得。GROUP
:群組 ID。
在工作目錄中執行範例:
node add-group-to-space.js
Chat API 會傳回 membership
的執行個體,詳細說明建立的群組成員。
將 Chat 應用程式新增至聊天室
Chat 應用程式無法將其他應用程式新增為聊天室的成員。如要將 Chat 應用程式新增至聊天室,或兩位真人使用者之間的即時訊息,請在要求中傳遞以下內容:
- 指定
chat.memberships.app
授權範圍。 - 呼叫
membership
資源上的create
方法。 - 將
parent
設為要在其中建立成員資格的聊天室資源名稱。 - 將
member
設為users/app
;別名,代表呼叫 Chat API 的應用程式。
以下範例會將 Chat 應用程式新增至聊天室:
Python
- 在工作目錄中,建立名為
chat_membership_app_create.py
的檔案。 在
chat_membership_app_create.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.app"] def main(): ''' Authenticates with Chat API via user credentials, then adds the Chat app to a Chat space. ''' # 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().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Set the Chat app as the entity that gets added to the space. # 'app' is an alias for the Chat app calling the API. body = { 'member': { 'name':'users/app', 'type': 'BOT' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
在程式碼中,將
SPACE
替換為聊天室名稱,您可以從 Chat API 的spaces.list
方法或聊天室網址取得。在工作目錄中建構並執行範例:
python3 chat_membership_app_create.py
Node.js
- 在工作目錄中,建立名為
add-app-to-space.js
的檔案。 在
add-app-to-space.js
中加入下列程式碼:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the app to the Chat space. * @return {!Promise<!Object>} */ async function addAppToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships.app', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/app', type: 'BOT'}} }); } addAppToSpace().then(console.log);
在程式碼中,將
SPACE
替換為聊天室名稱,您可以從 Chat API 的spaces.list
方法或聊天室網址取得。在工作目錄中執行範例:
node add-app-to-space.js
Chat API 會傳回 membership
的執行個體,詳細說明已建立的應用程式成員資格。