本指南說明如何針對 Google Chat API 的 Space
資源使用 create
方法建立具名聊天室。
代表使用者和 Chat 應用程式傳送訊息、共用檔案及協同合作的空間。Space
資源聊天室分為以下幾類:
- 即時訊息 (DM) 是兩位使用者或使用者和 Chat 應用程式之間的對話。
- 群組通訊是三位以上使用者和 Chat 應用程式的對話。
- 已命名的聊天室是使用者傳送訊息、共用檔案及協同合作的永久位置。
已命名空間是使用者可以傳送訊息、分享檔案及協同合作的空間。已命名的聊天室可以包含 Chat 擴充應用程式。已命名聊天室包含未命名的群組對話和即時訊息沒有的額外功能,例如聊天室管理員,可以套用管理設定、說明,以及新增或移除使用者和應用程式。建立具名聊天室後,聊天室唯一的成員是通過驗證的使用者。該聊天室不含其他使用者或應用程式,甚至是建立該聊天室的 Chat 應用程式。如要新增成員,請呼叫 Member
資源上的 create
方法,在聊天室中建立成員。如要瞭解如何建立會員資格,請參閱這篇文章。
如要建立包含多位成員的已命名聊天室 (例如三位以上的未命名群組通訊,或兩位使用者與呼叫 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.create
或chat.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.create
或chat.spaces
授權範圍的使用者驗證。
建立已命名聊天室
如要建立已命名的聊天室,請在要求中傳遞以下內容:
- 指定
chat.spaces.create
或chat.spaces
授權範圍。 - 呼叫
Space
資源上的create
方法。 - 將
spaceType
設為SPACE
。 - 將
displayName
設為向使用者顯示的聊天室名稱。在以下範例中,displayName
設為API-made
。 - 您也可以視需要設定其他聊天室屬性,例如
spaceDetails
(使用者會看到的聊天室說明和一組規範)。
以下說明如何建立已命名的聊天室:
Python
- 在工作目錄中,建立名為
chat_space_create_named.py
的檔案。 在
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()
在工作目錄中建構並執行範例:
python3 chat_space_create_named.py
Node.js
- 在工作目錄中,建立名為
create-space.js
的檔案。 在
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);
在工作目錄中執行範例:
node create-space.js
系統會建立已命名聊天室。如要前往聊天室,請使用聊天室的資源 ID 建構聊天室網址。您可以在 Google Chat 回應主體中找到聊天室 name
的資源 ID。舉例來說,如果聊天室的 name
為 spaces/1234567
,您可以使用以下網址前往聊天室:https://mail.google.com/chat/u/0/#chat/space/1234567
。
相關主題
- 建立成員,即可將使用者和應用程式新增至聊天室。
- 建立訊息,在聊天室中張貼訊息。
- 取得聊天室的詳細資料。
- 列出聊天室。
- 更新聊天室。
- 刪除聊天室。
- 設定聊天室。
- 尋找即時訊息聊天室。
- 將聊天室設為可供特定使用者搜尋。