스페이스 업데이트하기

이 가이드에서는 Google Chat API의 Space 리소스에서 patch 메서드를 사용하여 스페이스를 업데이트하는 방법을 설명합니다. 스페이스를 업데이트하여 사용자에게 표시되는 표시 이름, 설명, 가이드라인 등 스페이스와 관련된 속성을 변경합니다.

Space 리소스는 사용자와 채팅 앱이 메시지를 보내고 파일을 공유하며 공동작업할 수 있는 위치를 나타냅니다. 스페이스에는 다음과 같은 여러 유형이 있습니다.

  • 채팅 메시지 (DM)는 두 사용자 또는 사용자와 채팅 앱 간의 대화입니다.
  • 그룹 채팅은 3명 이상의 사용자와 채팅 앱 간의 대화입니다.
  • 이름이 지정된 스페이스는 사용자가 계속해서 메시지를 보내고 파일을 공유하며 공동작업할 수 있는 공간입니다.

기본 요건

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.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.spaces사용자 인증이 필요합니다.

스페이스 업데이트하기

Google Chat의 기존 스페이스를 업데이트하려면 요청에 다음을 전달합니다.

  • chat.spaces 승인 범위를 지정합니다.
  • Space 리소스에서 patch 메서드를 호출하고 업데이트할 공간의 name와 업데이트된 공간 속성을 지정하는 updateMaskbody를 전달합니다.
  • updateMask는 업데이트할 공간의 측면을 지정하며 다음을 포함합니다.
    • displayName: Google Chat UI에 표시되는 스페이스의 사용자가 읽을 수 있는 이름을 업데이트합니다. SPACE 유형으로 스페이스의 표시 이름 변경 또는 GROUP_CHAT 스페이스 유형을 SPACE로 변경하기 위해 spaceType 마스크도 포함하는 경우에만 지원 GROUP_CHAT 또는 DIRECT_MESSAGE 공간의 표시 이름을 업데이트하려고 하면 잘못된 인수 오류가 발생합니다.
    • spaceType: 스페이스 유형을 업데이트하지만 GROUP_CHAT 스페이스 유형을 SPACE로 변경하는 것만 지원합니다. 업데이트 마스크에 spaceType와 함께 displayName를 포함하고 지정된 공간에 비어 있지 않은 displayNameSPACE 공간 유형이 있는지 확인합니다. 기존 스페이스에 이미 SPACE 유형이 있는 경우(표시 이름을 업데이트할 때 지정된 공간에 spaceType 마스크 및 SPACE 유형을 포함하는 것은 선택사항입니다 . 공백 유형을 다른 방식으로 업데이트하려고 하면 잘못된 인수 오류가 발생합니다.
    • spaceDetails: 설명 및 규칙을 포함한 스페이스에 관한 세부정보입니다.
    • spaceHistoryState: 조직에서 사용자가 기록 설정을 변경하도록 허용하는 경우 스페이스의 기록 사용 설정 또는 중지를 지원합니다. 다른 모든 필드 경로와 상호 배타적입니다.

기존 스페이스의 spaceDetails를 업데이트하는 방법은 다음과 같습니다.

Python

  1. 작업 디렉터리에서 chat_space_update.py라는 파일을 만듭니다.
  2. chat_space_update.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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates the specified space description and guidelines.
        '''
    
        # 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().patch(
    
          # The space to update, and the updated space details.
          #
          # Replace {space} with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          name='spaces/SPACE',
          updateMask='spaceDetails',
          body={
    
            'spaceDetails': {
              'description': 'This description was updated with Chat API!',
              'guidelines': 'These guidelines were updated with Chat API!'
            }
    
          }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 SPACE을 스페이스 이름으로 바꿉니다. 이 이름은 Chat API의 spaces.list 메서드 또는 스페이스의 URL에서 가져올 수 있습니다.

  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_space_update.py
    

Node.js

  1. 작업 디렉터리에서 update-space.js라는 파일을 만듭니다.
  2. update-space.js에 다음 코드를 포함합니다.

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a Chat space with the description and guidelines.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.patch({
        name: 'spaces/SPACE',
        updateMask: 'spaceDetails',
        requestBody: {
          spaceDetails: {
            description: 'This description was updated with Chat API!',
            guidelines: 'These guidelines were updated with Chat API!'
          },
        }
      });
    }
    
    updateSpace().then(console.log);
    
  3. 코드에서 SPACE을 스페이스 이름으로 바꿉니다. 이 이름은 Chat API의 spaces.list 메서드 또는 스페이스의 URL에서 가져올 수 있습니다.

  4. 작업 디렉터리에서 샘플을 실행합니다.

    node update-space.js
    

Google Chat API는 업데이트를 반영하는 Space 리소스의 인스턴스를 반환합니다.