設定有初始成員的聊天室

本指南說明如何針對以下項目的 Space 資源使用 setup 方法: 用於設定 Google Chat 聊天室的 Google Chat API。設定聊天室會建立聊天室 並新增指定的使用者

Space 項資源 代表使用者和 Chat 擴充應用程式可以傳送訊息的地方 分享檔案及協同合作聊天室分為以下幾類:

  • 即時訊息 (DM) 是指兩位使用者或使用者之間的對話, Chat 應用程式。
  • 群組通訊是 3 位以上使用者之間的對話, Chat 擴充應用程式。
  • 已命名的聊天室是使用者傳送訊息、共用檔案、 這項原則著重於透過對開放式探究 嚴謹治學、誠信與合作的堅持來達到上述目標

設定聊天室時,請注意下列事項:

  • 呼叫 (已驗證) 的使用者會自動新增至聊天室,因此您 不需要在要求中指定使用者的成員資格。
  • 建立即時訊息 (DM) 時,如果兩位使用者之間存在即時訊息 系統便會傳回即時訊息否則系統會建立即時訊息。
  • 建立群組通訊時 (如果要求中未提供任何成員資格)。 已成功加入群組通訊 (例如權限問題),然後 系統可能會建立空白的群組通訊 (包括僅限通話的使用者)。
  • 你無法設定含有對話串回覆的聊天室,也無法新增外部使用者 Google Workspace
  • 要求中提供的成員重複 (包括發出呼叫的使用者) 而非導致要求錯誤。

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 或以上版本
  • npm 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    npm install @google-cloud/local-auth @googleapis/chat
    

設定聊天室

如要設定聊天室,請在要求中傳遞以下內容:

  • 指定 chat.spaces.createchat.spaces 授權範圍。
  • setup 方法Space 資源而言。
  • 如要將真人使用者新增為聊天室成員,請指定 users/{user},其中 {user}{person_id}person 從 People API 擷取的 ID user 。舉例來說,如果 People API 人員 resourceNamepeople/123456789,你可以透過下列方式將使用者新增至聊天室 包含以 users/123456789 的身分成為 member.name 的會員。
  • 如要將群組新增為聊天室成員,請指定 groups/{group},其中 {group} 是 要建立成員資格的群組 ID。群組 ID 可以 已使用 Cloud Identity API 擷取。 舉例來說,如果 Cloud Identity API 會傳回名為 groups/123456789 的群組,然後設定 membership.groupMember.namegroups/123456789。無法新增 Google 網路論壇 系統會將你加入群組通訊或即時訊息,但只會在已命名的聊天室中加入。
  • 建立傳送者與另一位真人使用者之間的即時訊息 時,請在請求中指定真人使用者的成員資格。
  • 如要在發起通話的使用者和通話應用程式之間建立即時訊息,請設定 Space.singleUserBotDm 變更為 true,而且不指定任何成員資格。你可以 只能用這個方法透過呼叫應用程式設定即時訊息。如何新增通話 新增為聊天室成員,或兩個真人使用者之間的現有即時訊息,請參閱 建立會員資格

下例會建立一個具名聊天室,並建立聊天室成員 一個群組和三個一般使用者 (包括已驗證使用者和兩個使用者) 其他指定使用者)。

Python

  1. 在工作目錄中,建立名為 chat_space_setup.py 的檔案。
  2. chat_space_setup.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.spaces.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then sets up a Chat space by creating a space and adding members.
        '''
    
        # 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().setup(
    
          # Details about the space to set up.
          body = {
    
            # Attributes of the space to set up, like space type and display name.
            'space': {
    
                # To set up a named space, set spaceType to SPACE.
                'spaceType': 'SPACE',
    
                # The user-visible name of the space
                'displayName': 'API-setup'
            },
    
            # The users and groups to add to the space.
            #
            # The authenticated user is automatically added to the space,
            # and doesn't need to be specified in the memberships array.
            'memberships': [
                {
                  'member': {
                    'name':'users/123456789',
                    'type': 'HUMAN'
                  }
                },
                {
                  'member': {
                    'name':'users/987654321',
                    'type': 'HUMAN'
                  }
                },
                {
                  'groupMember': {
                    'name': 'groups/11223344'
                  }
                }
            ]
          }
    
          ).execute()
    
        # Prints details about the created space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在工作目錄中建構並執行範例:

    python3 chat_space_setup.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Sets up a new Chat space with users.
    * @return {!Promise<!Object>}
    */
    async function setupSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.create',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.setup({
        requestBody: {
          space: {
            spaceType: 'SPACE',
            displayName: 'API-made',
          },
          memberships: [
            {member: {name: 'users/123456789', type: 'HUMAN'}},
            {member: {name: 'users/987654321', type: 'HUMAN'}},
            {groupMember: {name: 'groups/11223344'}},
          ]
        }
      });
    }
    
    setupSpace().then(console.log);
    
  3. 在工作目錄中執行範例:

    node setup-space.js
    

有 1 個群組和三位真人使用者的已命名聊天室,包括 已驗證使用者的設定

如要前往聊天室,請使用聊天室的資源 ID 建立聊天室網址。 你可以在 Google Chat 回覆中取得聊天室「name」的資源 ID 舉例來說,如果聊天室的namespaces/1234567,你可以 加入「聊天室」: https://mail.google.com/chat/u/0/#chat/space/1234567