建立 Google Chat 聊天室並新增成員

本指南說明如何使用 Google Chat API 的 Space 資源中的 setUp() 方法建立 Chat 聊天室,並在其中新增成員。

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

  • 即時訊息 (DM) 是指兩位使用者之間,或使用者與 Chat 應用程式之間的對話。
  • 群組對話是指三位以上使用者和即時通訊應用程式之間的對話。
  • 具名聊天室是持續存在的空間,可供使用者傳送訊息、分享檔案及協作。

您可以使用 setUp() 方法執行下列任一操作:

  • 建立含有初始成員的具名空間。
  • 建立兩位使用者之間的即時訊息 (DM)。
  • 設定多人群組訊息。

設定空間時,請注意下列事項:

  • 系統會自動將呼叫 (已驗證) 使用者新增至即時通訊空間,因此您不必在要求中指定使用者的成員資格。
  • 建立即時訊息 (DM) 時,如果兩位使用者之間已有即時訊息,系統就會傳回該訊息。否則系統會建立即時訊息。
  • 建立群組通訊時,如果系統無法將要求中提供的任何成員新增至群組通訊 (例如權限問題),則可能會建立空白群組通訊 (只包含通話使用者)。
  • 您無法設定具有討論串回覆的聊天室,也無法新增 Google Workspace 機構外部的使用者。
  • 系統會篩除要求中重複的成員 (包括通話使用者),不會導致要求錯誤。
  • Google Workspace 管理員為整個 Google Workspace 機構安裝 Chat 應用程式後,Google Chat 會在已安裝的 Chat 應用程式和機構中的每位使用者之間建立即時訊息,因此不必以程式輔助方式設定即時訊息。請改為列出聊天室來傳回所有即時訊息,或尋找即時訊息來取得特定即時訊息的詳細資料。

必要條件

Node.js

Python

Java

Apps Script

設定空間

如要設定空間,請在要求中傳遞下列項目:

  • 指定 chat.spaces.createchat.spaces 授權範圍。
  • 呼叫 SetUpSpace() 方法。
  • space 做為 Space 的執行個體傳遞,並包含所有必要欄位,例如 displayNamespaceType
  • Membership 例項陣列的形式傳遞 memberships。針對每個執行個體:
    • 指定 users/{user},將使用者新增為聊天室成員,其中 {user} 是 People API 中 person{person_id},或是 Directory API 中 user 的 ID。舉例來說,如果 People API 人員 resourceNamepeople/123456789,您可以加入成員,方法是加入以 users/123456789 做為 member.name 的成員資格。
    • 指定 groups/{group},將群組新增為聊天室成員,其中 {group} 是要建立成員資格的群組 ID。您可以使用 Cloud Identity API 擷取群組 ID。舉例來說,如果Cloud Identity API 傳回名稱為 groups/123456789 的群組,請將 membership.groupMember.name 設為 groups/123456789。Google 群組只能新增至具名聊天室,無法新增至群組對話或即時訊息。

如要在通話使用者和其他真人使用者之間建立私訊,請在要求中指定真人使用者的成員資格。

如要在通話使用者和通話應用程式之間建立即時訊息,請將 space.singleUserBotDm 設為 true,且不要指定任何成員。您只能使用這個方法,透過通話應用程式設定直接訊息。如要將通話應用程式新增為空間成員,或新增至兩位使用者之間的現有直接訊息,請參閱建立成員資格

下列範例會建立具名空間,並為兩位使用者 (已驗證的使用者和另一位使用者) 建立一個空間成員。

Node.js

chat/client-libraries/cloud/set-up-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';  const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces.create'];  // This sample shows how to set up a named space with one initial member // with user credential async function main() {   // Create a client   const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);    // Initialize request argument(s)   const request = {     space: {       spaceType: 'SPACE',       // Replace DISPLAY_NAME here.       displayName: 'DISPLAY_NAME'     },     memberships: [{       member: {         // Replace USER_NAME here.         name: 'users/USER_NAME',         type: 'HUMAN'       }     }]   };    // Make the request   const response = await chatClient.setUpSpace(request);    // Handle the response   console.log(response); }  main().catch(console.error);

Python

chat/client-libraries/cloud/set_up_space_user_cred.py
from authentication_utils import create_client_with_user_credentials from google.apps import chat_v1 as google_chat  SCOPES = ["https://www.googleapis.com/auth/chat.spaces.create"]  def set_up_space_with_user_cred():     # Create a client     client = create_client_with_user_credentials(SCOPES)      # Initialize request argument(s)     request = google_chat.SetUpSpaceRequest(         space = {             "space_type": 'SPACE',             # Replace DISPLAY_NAME here.             "display_name": 'DISPLAY_NAME'         },         memberships = [{             "member": {                 # Replace USER_NAME here.                 "name": 'users/USER_NAME',                 "type_": 'HUMAN'             }         }]     )      # Make the request     response = client.set_up_space(request)      # Handle the response     print(response)  set_up_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/SetUpSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient; import com.google.chat.v1.Membership; import com.google.chat.v1.SetUpSpaceRequest; import com.google.chat.v1.Space; import com.google.chat.v1.User;  // This sample shows how to set up a named space with one initial member with // user credential. public class SetUpSpaceUserCred {    private static final String SCOPE =     "https://www.googleapis.com/auth/chat.spaces.create";    public static void main(String[] args) throws Exception {     try (ChatServiceClient chatServiceClient =         AuthenticationUtils.createClientWithUserCredentials(           ImmutableList.of(SCOPE))) {       SetUpSpaceRequest.Builder request = SetUpSpaceRequest.newBuilder()         .setSpace(Space.newBuilder()           .setSpaceType(Space.SpaceType.SPACE)           // Replace DISPLAY_NAME here.           .setDisplayName("DISPLAY_NAME"))         .addAllMemberships(ImmutableList.of(Membership.newBuilder()           .setMember(User.newBuilder()             // Replace USER_NAME here.             .setName("users/USER_NAME")             .setType(User.Type.HUMAN)).build()));       Space response = chatServiceClient.setUpSpace(request.build());        System.out.println(JsonFormat.printer().print(response));     }   } }

Apps Script

chat/advanced-service/Main.gs
/**  * This sample shows how to set up a named space with one initial member with  * user credential.  *   * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.create'  * referenced in the manifest file (appsscript.json).  */ function setUpSpaceUserCred() {   // Initialize request argument(s)   const space = {     spaceType: 'SPACE',     // TODO(developer): Replace DISPLAY_NAME here     displayName: 'DISPLAY_NAME'   };   const memberships = [{     member: {       // TODO(developer): Replace USER_NAME here       name: 'users/USER_NAME',       // User type for the membership       type: 'HUMAN'     }   }];    // Make the request   const response = Chat.Spaces.setup({ space: space, memberships: memberships });    // Handle the response   console.log(response); }

如要執行範例,請替換下列項目:

  • DISPLAY_NAME:新空間的顯示名稱。
  • USER_NAME:要納入成員資格的其他使用者 ID。

如要前往聊天室,請使用聊天室的資源 ID 建構聊天室的網址。 您可以從 Google Chat 回覆主體的聊天室 name 取得資源 ID。舉例來說,如果聊天室的 namespaces/1234567,您可以使用下列網址前往該聊天室:https://mail.google.com/chat/u/0/#chat/space/1234567