建立聊天室

本指南說明如何在 Google Chat API 的 Space 資源上使用 create 方法,建立已命名的聊天室。

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

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

已命名的聊天室是使用者可以傳送訊息、共用檔案及進行協作的地方。已命名的聊天室可包含 Chat 擴充應用程式。已命名的聊天室包含未命名的群組對話和即時訊息沒有的額外功能,例如聊天室管理員可套用管理設定、說明,以及新增或移除使用者和應用程式。建立已命名的聊天室後,只有已驗證的使用者是聊天室成員。該聊天室不會包含其他使用者或應用程式,甚至不會包含建立該聊天室的 Chat 應用程式。如要新增使用者,請呼叫 Member 資源create 方法,在聊天室中建立成員資格。詳情請參閱建立會員資格

如要建立內含多位成員的已命名聊天室 (例如三人以上的未命名群組通訊,或兩人之間的即時訊息),或與呼叫 Chat API 的使用者與 Chat 應用程式進行即時訊息對話,請改為設定聊天室

必要條件

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 授權範圍。
  • 呼叫 Space 資源上的 create 方法
  • 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 membership.
        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 建立聊天室網址。您可以在 Google Chat 回應主體中找到聊天室 name 中的資源 ID。舉例來說,如果聊天室的 namespaces/1234567,您可以使用以下網址前往該聊天室:https://mail.google.com/chat/u/0/#chat/space/1234567