本指南說明如何在 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-httplib2 google-auth-oauthlib oauth2client
已發布的 Chat 應用程式。如要建立及發布 Chat 應用程式,請參閱「建構 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
中加入下列程式碼:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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
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
中加入下列程式碼:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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
Chat API 會傳回 membership
的執行個體,詳細說明所建立成員資格。