초기 멤버로 스페이스 설정하기

이 가이드에서는 setUp() Google Chat API의 Space 리소스에 대한 메서드로 Google Chat을 설정합니다. 있습니다. 스페이스를 설정하면 스페이스가 생성되고 지정된 사용자가 추가됩니다.

Space 리소스 는 사람과 채팅 앱이 메시지를 보낼 수 있는 위치를 나타냅니다. 파일 공유, 공동작업 등이 가능합니다 다음과 같은 여러 유형의 스페이스가 있습니다.

  • 채팅 메시지 (DM)는 사용자 2명 또는 상대방과 채팅 앱
  • 그룹 채팅은 3명 이상의 사용자와 Chat 앱 간의 대화입니다.
  • 이름이 지정된 스페이스는 사용자가 메시지를 보내고, 파일을 공유하고, 협업할 수 있습니다

스페이스를 설정할 때는 다음 사항을 고려하세요.

  • 통화 (인증된) 사용자가 스페이스에 자동으로 추가되므로 요청에 사용자의 멤버십을 지정하지 않아도 됩니다.
  • 채팅 메시지(DM)를 만들 때 두 사용자 간에 DM이 있는 경우 DM이 반환됩니다. 그렇지 않으면 DM이 생성됩니다.
  • 그룹 채팅을 만들 때 요청에 제공된 멤버십이 그룹 채팅에 추가되지 않으면(예: 권한 문제) 호출 사용자만 포함된 빈 그룹 채팅이 생성될 수 있습니다.
  • 대화목록 답글이 있는 스페이스를 설정하거나 Google Workspace 외부의 사용자를 추가할 수 없습니다.
  • 요청에 중복된 멤버십 (통화 사용자 포함) 제공 요청 오류가 발생하지 않고 필터링됩니다.

기본 요건

Node.js

Python

자바

Apps Script

스페이스 설정하기

스페이스를 설정하려면 요청에 다음을 전달합니다.

  • chat.spaces.create 또는 chat.spaces 승인 범위를 지정합니다.
  • SetUpSpace() 메서드를 호출합니다.
  • spacedisplayName 또는 spaceType와 같은 모든 필수 필드와 함께 Space의 인스턴스로 전달합니다.
  • membershipsMembership 인스턴스의 배열로 전달합니다. 각 인스턴스에 대해 다음을 실행합니다.
    • users/{user}를 지정하여 사람 사용자를 스페이스 회원으로 추가합니다. 여기서 {user}는 People API의 person에 대한 {person_id} 또는 Directory API의 user의 ID입니다. 예를 들어 People API 담당자가 resourceName님이 people/123456789인 경우 다음 방법으로 사용자를 스페이스에 추가할 수 있습니다. member.name 멤버십이 users/123456789인 멤버십을 포함합니다.
    • groups/{group}를 지정하여 그룹을 스페이스 구성원으로 추가합니다. 여기서 {group}은 멤버십을 만들 그룹 ID입니다. 그룹의 ID는 Cloud ID API를 사용하여 검색할 수 있습니다. 예를 들어 Cloud ID API 이름이 groups/123456789인 그룹을 반환한 후 membership.groupMember.name에서 groups/123456789(으)로 Google 그룹스는 그룹 채팅 또는 채팅 메시지에 추가되었지만 이름이 지정된 스페이스에만 표시됩니다.

전화를 건 사용자와 다른 실제 사용자 간에 채팅 메시지를 만들려면 사용자의 멤버십을 포함할 수 있습니다.

호출 사용자와 호출 앱 간에 채팅 메시지를 만들려면 space.singleUserBotDmtrue로 설정하고 멤버십을 지정하지 않습니다. 이 메서드는 통화 앱으로 DM을 설정하는 데만 사용할 수 있습니다. 통화 앱을 스페이스의 구성원 또는 두 사람의 기존 DM에 추가하려면 멤버십 만들기를 참고하세요.

다음 예에서는 이름이 지정된 스페이스를 만들고 두 명의 실제 사용자(인증된 사용자와 다른 사용자 1명)를 위한 스페이스 멤버십을 하나 만듭니다.

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()

자바

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를 사용하여 스페이스의 URL을 빌드합니다. Google Chat 응답 본문의 스페이스 name에서 리소스 ID를 가져올 수 있습니다. 예를 들어 스페이스의 namespaces/1234567이면 다음 URL(https://mail.google.com/chat/u/0/#chat/space/1234567)을 사용하여 스페이스로 이동할 수 있습니다.