채팅 메시지 (DM) 스페이스 찾기

이 가이드에서는 Space에서 findDirectMessage 메서드를 사용하는 방법을 설명합니다. Google Chat API의 리소스를 사용하여 채팅 메시지 (DM) 스페이스에 관한 세부정보를 확인하세요.

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

  • 채팅 메시지 (DM)는 사용자 2명 또는 상대방과 채팅 앱
  • 그룹 채팅은 세 명 이상의 사용자와 채팅 앱
  • 이름이 지정된 스페이스는 사용자가 메시지를 보내고, 파일을 공유하고, 공동작업도 가능합니다

다음을 사용하여 인증 앱 인증 채팅 앱에서 채팅 앱이 Google Chat에서 에 액세스할 수 있음 (예: 소속된 채팅 메시지) 다음을 사용하여 인증 사용자 인증은 액세스 권한이 있는지 확인합니다.

기본 요건

Python

  • Python 3.6 이상
  • pip 패키지 관리 도구
  • 최신 Google 클라이언트 라이브러리 이러한 앱을 설치하거나 업데이트하려면 다음 단계를 따르세요. 명령줄 인터페이스에서 다음 명령어를 실행합니다.
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 이상
  • npm 패키지 관리 도구
  • 최신 Google 클라이언트 라이브러리 이러한 앱을 설치하거나 업데이트하려면 다음 단계를 따르세요. 명령줄 인터페이스에서 다음 명령어를 실행합니다.
    npm install @google-cloud/local-auth @googleapis/chat
    

채팅 메시지 찾기

Google Chat에서 채팅 메시지를 찾으려면 다음을 전달합니다. 요청:

  • 앱 인증에서 다음을 지정합니다. chat.bot 승인 범위 다음으로 바꿉니다. 사용자 인증 chat.spaces.readonly 또는 chat.spaces 승인 범위를 지정합니다.
  • 먼저 findDirectMessage 메서드 User 리소스에서 name를 전달 다시 요청할 수 있습니다 다음으로 바꿉니다. 사용자 인증 이 메서드는 호출하는 사용자와 지정된 사용자 간의 DM을 반환합니다. 다음으로 바꿉니다. 앱 인증을 사용할 경우 이 방법이 호출 앱과 지정된 사용자 간의 DM을 반환합니다.
  • 실제 사용자를 스페이스 멤버로 추가하려면 users/{user}를 지정합니다. 각 항목의 의미는 다음과 같습니다. {user}{person_id} person 또는 user 사용할 수 있습니다. 예를 들어 People API 사람 resourceNamepeople/123456789 상태인 경우 다음을 포함하여 스페이스에 사용자를 추가할 수 있습니다. member.name이(가) users/123456789인 멤버십입니다.

사용자 인증이 포함된 채팅 메시지 찾기

채팅 메시지를 찾는 방법은 다음과 같습니다. 사용자 인증:

Python

  1. 작업 디렉터리에 chat_space_find_dm_user.py라는 파일을 만듭니다.
  2. chat_space_find_dm_user.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then returns details about a specified DM.
        '''
    
        # 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().findDirectMessage(
    
              # The other user in the direct message (DM) to return.
              #
              # Replace USER with a user name.
              name='users/USER'
    
          ).execute()
    
        # Prints details about the direct message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 USERname Google Chat의 User

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

    python3 chat_space_find_dm_user.py
    

Node.js

  1. 작업 디렉터리에서 find-direct-message-space.js

  2. find-direct-message-space.js에 다음 코드를 포함합니다.

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Find a direct message Chat space for a user.
    * @return {!Promise<!Object>}
    */
    async function findDirectMessageSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.findDirectMessage(
          {name: 'users/USER'});
    }
    
    findDirectMessageSpace().then(console.log);
    
  3. 코드에서 USERname Google Chat의 User

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

    node find-direct-message-space.js
    

Chat API는 Space 세부정보를 확인할 수 있습니다

앱 인증이 포함된 채팅 메시지 찾기

채팅 메시지를 찾는 방법은 다음과 같습니다. 앱 인증:

Python

  1. 작업 디렉터리에 chat_space_find_dm_app.py라는 파일을 만듭니다.
  2. chat_space_find_dm_app.py에 다음 코드를 포함합니다.

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().findDirectMessage(
    
        # The other user in the direct message (DM) to return.
        #
        # Replace USER with a user name.
        name='users/USER'
    
    ).execute()
    
    print(result)
    
  3. 코드에서 USERname Google Chat의 User

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

    python3 chat_space_find_dm_app.py
    

Node.js

  1. 작업 디렉터리에서 app-find-direct-message-space.js

  2. app-find-direct-message-space.js에 다음 코드를 포함합니다.

    const chat = require('@googleapis/chat');
    
    /**
    * Find a direct message Chat space for a user.
    * @return {!Promise<!Object>}
    */
    async function findDirectMessageSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.findDirectMessage(
          {name: 'users/USER'});
    }
    
    findDirectMessageSpace().then(console.log);
    
  3. 코드에서 USERname Google Chat의 User

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

    node app-find-direct-message-space.js
    

Chat API는 지정된 DM을 자세히 설명하는 Space입니다.