在聊天室中列出使用者和 Google Chat 應用程式

本指南說明如何針對 Google Chat API 的 membership 資源使用 list 方法,將聊天室中的使用者和 Chat 應用程式列為一個可篩選的聊天室成員清單。透過應用程式驗證列出成員資格會在 Chat 應用程式可存取的聊天室中列出成員資格,但不含 Chat 應用程式成員 (包括 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 應用程式

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

以下範例列出由於 filter 設為 ROLE_Member,因此已驗證使用者可見的聊天室成員 (非聊天室管理員):

Python

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

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

    python3 chat_member_list_user.py
    

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

透過應用程式驗證功能在聊天室中列出使用者和 Chat 應用程式

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

以下範例列出 Chat 應用程式可見的聊天室成員 (非聊天室管理員):

Python

  1. 在工作目錄中,建立名為 chat_member_list_app.py 的檔案。
  2. chat_member_list_app.py 中加入下列程式碼:

    from httplib2 import Http
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', SCOPES)
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http()))
    
    # 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 方法