列出聊天室中的成員

本指南說明如何在 Google Chat API 的 membership 資源上使用 list 方法,以分頁形式列出聊天室中的成員,做為聊天室中可篩選的成員清單。採用應用程式驗證功能的成員資格會列出 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 應用程式設定授權。清單成員資格支援下列兩種驗證方式:

透過使用者驗證機制列出聊天室中的成員

如要在通過驗證的使用者可存取的聊天室中,列出使用者、Google 網路論壇和 Chat 應用程式,請在要求中傳遞以下內容:

  • 透過使用者驗證,請指定 chat.memberships.readonlychat.memberships 授權範圍。
  • 呼叫 membership 資源上的 list 方法
  • 如要列出 Google 群組,請將查詢參數 showGroups 設為 true

以下範例列出通過驗證的使用者可檢視的 Google 群組、人類和應用程式成員。

Python

  1. 在工作目錄中,建立名為 chat_member_list_user.py 的檔案。
  2. 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 Google Group, human, and app members 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',
    
            # Set this parameter to list Google Groups.
            showGroups = 'true'
    
        ).execute()
    
        # Prints the list of memberships.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,將 SPACE 替換為聊天室名稱,您可以從 Chat API 的 spaces.list 方法或聊天室網址取得。

  4. 在工作目錄中建構並執行範例:

    python3 chat_member_list_user.py
    

Google Chat API 會傳回指定聊天室中的 Google 群組、人員和應用程式成員清單。

透過應用程式驗證功能列出聊天室成員

如要在已驗證應用程式可存取的聊天室中列出使用者和 Chat 應用程式,請在要求中傳遞以下內容:

以下範例列出 Chat 應用程式可以查看的使用者聊天室成員,非聊天室管理員:

Python

  1. 在工作目錄中,建立名為 chat_member_list_app.py 的檔案。
  2. 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)
    
  3. 在程式碼中,將 SPACE 替換為聊天室名稱,您可以從 Chat API 的 spaces.list 方法或聊天室網址取得。

  4. 在工作目錄中建構並執行範例:

    python3 chat_member_list_app.py
    

Google Chat API 會傳回指定聊天室中的真人聊天室成員清單 (不含聊天室管理員)。

自訂分頁或篩選清單

如要列出成員資格,請傳遞下列查詢參數,以便自訂加入或篩選所列成員的分頁:

  • pageSize:傳回的會員數量上限。服務傳回的值可能會少於這個值。如未指定,最多會傳回 100 個空格。許可的最大值為 1,000;超過 1,000 的值會自動變更為 1,000。
  • pageToken:從先前的清單聊天室呼叫接收的網頁權杖。提供這個權杖即可擷取後續網頁。進行分頁時,篩選器值應與提供網頁權杖的呼叫相符。傳送不同的值可能會導致非預期的結果。
  • filter:查詢篩選器。需要使用者驗證。如需支援的查詢詳細資料,請參閱 spaces.members.list 方法