建立聊天室

本指南說明如何針對以下項目的 Space 資源使用 create 方法: 使用 Google Chat API 建立具名聊天室。

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

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

已命名聊天室可讓使用者傳送訊息、分享檔案 合作。已命名的聊天室可以包含 Chat 擴充應用程式。具名聊天室 包含額外功能,可用於未命名的群組對話和即時訊息 例如可以套用管理設定的聊天室管理員 以及新增或移除使用者和應用程式建立已命名的聊天室後 聊天室唯一的成員是已驗證的使用者。聊天室沒有 包含其他人或應用程式甚至包括 Chat 應用程式 建立應用程式如要新增使用者,請呼叫 create 方法Member 項資源。 如要瞭解如何建立會員資格,請參閱這篇文章

如要建立有多位成員的已命名聊天室 (未命名的群組通訊), 或兩位以上的使用者,或兩人之間的即時訊息對話,或 呼叫 Chat API:設定聊天室

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Python 專用 Google 用戶端程式庫。如何安裝或更新 請在指令列介面中執行下列指令:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。詳細步驟請參閱 建構 Google Chat 應用程式
  • 已為 Chat 應用程式設定授權。建立中 聊天室需要 使用者驗證 chat.spaces.createchat.spaces 授權範圍。

Node.js

  • Node.js 與npm
  • Node.js 的最新 Google 用戶端程式庫。如要安裝這些應用程式,請執行 執行下列指令:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。詳細步驟請參閱 建構 Google Chat 應用程式
  • 已為 Chat 應用程式設定授權。建立中 聊天室需要 使用者驗證 chat.spaces.createchat.spaces 授權範圍。

建立已命名聊天室

如要建立已命名的聊天室,請在要求中傳遞以下內容:

  • 指定 chat.spaces.createchat.spaces 授權範圍。
  • create 方法Space 項資源
  • 組合 spaceType敬上 至 SPACE
  • 組合 displayName敬上 向使用者顯示的聊天室名稱。在以下範例中:displayName 已設為 API-made
  • 你也可以設定其他聊天室屬性,例如 spaceDetails敬上 (使用者可以看到的聊天室說明和一組規範)。

以下說明如何建立已命名的聊天室:

Python

  1. 在工作目錄中,建立名為 chat_space_create_named.py 的檔案。
  2. chat_space_create_named.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 creates a Chat 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().create(
    
          # Details about the space to create.
          body = {
    
            # To create a named space, set spaceType to SPACE.
            'spaceType': 'SPACE',
    
            # The user-visible name of the space.
            'displayName': 'API-made'
          }
    
          ).execute()
    
        # Prints details about the created space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在工作目錄中建構並執行範例:

    python3 chat_space_create_named.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Creates a new chat space.
    * @return {!Promise<!Object>}
    */
    async function createSpace() {
      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.create(
          {requestBody: {spaceType: 'SPACE', displayName: 'API-made'}});
    }
    
    createSpace().then(console.log);
    
  3. 在工作目錄中執行範例:

    node create-space.js
    

系統會建立已命名聊天室。如要前往聊天室,請使用聊天室的資源 ID 來建立聊天室的網址您可以在聊天室中找到資源 ID name。舉例來說,如果你的聊天室 namespaces/1234567,你可以使用下列指令前往聊天室 網址:https://mail.google.com/chat/u/0/#chat/space/1234567