取得聊天室詳細資料

本指南說明如何在 Google Chat API 的 Space 資源上使用 get 方法,以便查看聊天室的詳細資料,例如顯示名稱、說明和規範。

Space 資源代表使用者可以和 Chat 應用程式傳送訊息、共用檔案及進行協同合作的位置。聊天室分為以下幾種類型:

  • 即時訊息 (DM) 是兩位使用者或使用者和 Chat 應用程式之間的對話。
  • 群組通訊是指三位以上使用者和 Chat 應用程式之間的對話。
  • 已命名的聊天室是永久的地方,可讓使用者傳送訊息、共用檔案及協同合作。

透過應用程式驗證機制進行驗證可讓 Chat 應用程式取得 Chat 應用程式在 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 應用程式的授權。取得空間可支援以下兩種驗證方法:

取得儲存空間

如要在 Google Chat 中取得聊天室,請在要求中傳遞以下內容:

  • 請使用應用程式驗證,指定 chat.bot 授權範圍。使用使用者驗證時,請指定 chat.spaces.readonlychat.spaces 授權範圍。
  • 呼叫 Space 資源上的 get 方法,並傳遞空間的 name 以取得取得。從 Google Chat 的聊天室資源或聊天室網址取得聊天室名稱。

透過使用者驗證取得聊天室詳細資料

以下說明如何透過使用者驗證取得空間詳細資料:

Python

  1. 在工作目錄中,建立名為 chat_space_get_user.py 的檔案。
  2. chat_space_get_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.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets details about 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().get(
    
              # The space to get.
              #
              # Replace SPACE with a space name.
              # Obtain the space name from the spaces resource of Chat API,
              # or from a space's URL.
              name='spaces/SPACE'
    
          ).execute()
    
        # Prints details about the space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 將程式碼中的 SPACE 替換為聊天室名稱,這個名稱可從 Chat API 的 spaces.list 方法或聊天室網址取得。

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

    python3 chat_space_get_user.py
    

Chat API 會傳回 Space 執行個體,其中包含指定聊天室的詳細資料。

使用應用程式驗證功能取得聊天室詳細資料

如何使用應用程式驗證取得空間詳細資料:

Python

  1. 在工作目錄中,建立名為 chat_space_get_app.py 的檔案。
  2. chat_space_get_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().get(
    
        # The space to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        name='spaces/SPACE'
    
    ).execute()
    
    print(result)
    
  3. 將程式碼中的 SPACE 替換為聊天室名稱,這個名稱可從 Chat API 的 spaces.list() 方法或聊天室網址取得。

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

    python3 chat_space_get_app.py
    

Chat API 會傳回 Space 執行個體,其中包含指定聊天室的詳細資料。