本指南說明如何在 Google Chat API 的 membership
資源上使用 list
方法,將聊天室中的使用者和 Chat 應用程式列為可篩選的分頁成員清單。透過應用程式驗證列出成員資格時,會列出 Chat 應用程式可存取的聊天室中的成員,但不含 Chat 應用程式成員資格 (包括其擁有的聊天室)。透過使用者驗證功能列出成員資格,會列出已驗證使用者可存取的聊天室中的成員。
Membership
資源代表使用者或 Google Chat 應用程式是否收到聊天室的邀請、部分或缺席。
必要條件
Python
- Python 3.6 或更高版本
- pip 套件管理工具
最新的 Python 專用 Google 用戶端程式庫。如要安裝或更新這類軟體,請在指令列介面中執行下列指令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
- 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
已為 Chat 應用程式設定授權。商家資訊成員資格支援以下兩種驗證方式:
在提供使用者驗證機制的聊天室中列出使用者和 Chat 應用程式
如要在已驗證使用者可存取的聊天室中列出使用者和 Chat 應用程式,請在要求中傳遞以下內容:
- 透過使用者驗證,請指定
chat.memberships.readonly
或chat.memberships
授權範圍。 - 呼叫
membership
資源上的list
方法。
以下範例會列出已驗證使用者可以看到的聊天室成員 (而非聊天室管理員),因為 filter
已設為 ROLE_Member
:
Python
- 在工作目錄中,建立名為
chat_member_list_user.py
的檔案。 在
chat_member_list_user.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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists human space members (but not space managers) in a specified 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().list( # The space for which to list memberships. parent = 'spaces/SPACE', # An optional filter that returns only human space members. filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"' ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
在程式碼中,將
SPACE
替換為聊天室名稱,您可以在 Chat API 的spaces.list
方法或聊天室網址取得這項資訊。在工作目錄中,建構並執行範例:
python3 chat_member_list_user.py
Google Chat API 會傳回指定聊天室中的真人聊天室成員清單 (不包括聊天室管理員) 和應用程式成員清單。
在具備應用程式驗證功能的聊天室中列出使用者和 Chat 應用程式
如要在已驗證應用程式可存取的聊天室中列出使用者和 Chat 應用程式,請在要求中傳遞以下內容:
以下範例會列出 Chat 應用程式可見的人聊天室成員 (而非聊天室管理員):
Python
- 在工作目錄中,建立名為
chat_member_list_app.py
的檔案。 在
chat_member_list_app.py
中加入下列程式碼:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Use the service endpoint to call Chat API. result = chat.spaces().members().list( # The space for which to list memberships. parent = 'spaces/SPACE', # An optional filter that returns only human space members. filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"' ).execute() print(result)
在程式碼中,將
SPACE
替換為聊天室名稱,您可以在 Chat API 的spaces.list
方法或聊天室網址取得這項資訊。在工作目錄中,建構並執行範例:
python3 chat_member_list_app.py
Google Chat API 會從指定聊天室傳回人類聊天室成員清單 (不含聊天室管理員)。
自訂分頁或篩選清單
如要列出成員資格,請傳遞下列查詢參數,以便自訂所列成員的分頁,或是篩選這些成員:
pageSize
:要傳回的成員資格數量上限。服務傳回的結果可能會少於這個值。如未指定,最多會傳回 100 個空格。最大值是 1,000;超過 1,000 的值會自動變更為 1,000。pageToken
:從先前的清單聊天室呼叫收到的網頁權杖。提供這個權杖以擷取後續網頁。進行分頁時,篩選器值應與提供網頁權杖的呼叫相符。傳送不同的值可能會導致非預期的結果。filter
:查詢篩選器。需要使用者驗證。如需支援的查詢詳細資料,請參閱spaces.members.list
方法相關說明。
相關主題
- 取得使用者或 Chat 應用程式會員資格的詳細資料。
- 邀請使用者或 Chat 應用程式加入聊天室,或邀請他們加入。
- 更新 Google Chat 聊天室中的使用者成員資格。
- 從聊天室中移除使用者或 Chat 應用程式。