本指南說明如何在 Google Chat API 的 membership
資源上使用 create
方法,邀請使用者或 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
的執行個體,詳細說明已建立的成員。
將 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
的執行個體,詳細說明已建立的成員。