메시지 업데이트하기

이 가이드에서는 다음의 Message 리소스에서 patch 메서드를 사용하는 방법을 설명합니다. Google Chat API를 사용하여 스페이스에서 텍스트 또는 카드 메시지를 업데이트합니다. 메시지를 사용하여 메시지 내용 또는 메시지의 내용과 같은 메시지 속성을 카드를 사용합니다. 또한 카드 메시지를 사용하거나 문자 메시지에 카드를 추가할 수 있습니다.

Chat API는 또한 update 메서드, 하지만 patch 메서드 왜냐하면 그것은 PATCH HTTP 요청을 사용하는 동안 updatePUT HTTP 요청 자세한 내용은 AIP-134의 PATCHPUT 섹션.

Chat API에서 Chat 메시지는 Message 리소스. Chat 사용자는 텍스트가 포함된 메시지만 보낼 수 있지만 채팅 앱에서는 다음과 같은 다양한 메시지 기능을 사용할 수 있습니다. 정적 또는 대화형 사용자 인터페이스를 표시하여 메시지를 비공개로 전달할 수 있습니다 메시지 기능 자세히 알아보기 Chat API에서 사용할 수 있는 기능에 대한 자세한 내용은 Google Chat 메시지 개요

기본 요건

Python

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

사용자 인증을 통해 문자 메시지를 업데이트하거나 카드 메시지 앞에 문자 메시지 추가

문자 메시지 다음 코드로 교체합니다. 사용자 인증, 다음과 같이 요청합니다.

  • chat.messages 승인 범위
  • 업데이트할 메시지의 name입니다.
  • updateMask='text'
  • 업데이트된 메시지를 지정하는 body입니다.

업데이트된 메시지가 카드 메시지, 문자 메시지가 카드 메시지 앞에 추가됩니다 (계속 표시됨).

새 Search Ads 360 환경의 문자 메시지 또는 앞에 문자 메시지를 추가할 때 카드 메시지 다음 코드로 교체합니다. 사용자 인증:

Python

  1. 작업 디렉터리에서 chat_update_text_message_user.py
  2. chat_update_text_message_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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a message.
        '''
    
        # 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # Replace SPACE with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          #
          # Replace MESSAGE with a message name.
          # Obtain the message name from the response body returned
          # after creating a message asynchronously with Chat REST API.
          name='spaces/SPACE/messages/MESSAGE',
          updateMask='text',
          body={'text': 'Updated message!'}
    
        ).execute()
    
        # Prints details about the updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름으로, 다음에서 가져올 수 있습니다. spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.
    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_update_text_message_user.py
    

앱 인증을 통해 문자 메시지를 업데이트하거나 카드 메시지 앞에 문자 메시지 추가

문자 메시지 다음 코드로 교체합니다. 앱 인증 요청에 다음을 전달합니다.

  • chat.bot 승인 범위
  • 업데이트할 메시지의 name입니다.
  • updateMask='text'
  • 업데이트된 메시지를 지정하는 body입니다.

업데이트된 메시지가 카드 메시지인 경우 문자 메시지가 카드 메시지 앞에 추가됩니다 (계속 표시됨).

새 Search Ads 360 환경의 문자 메시지 또는 문자 메시지 앞에 문자 메시지를 추가할 수 있습니다. 카드 메시지 다음 코드로 교체합니다. 앱 인증:

Python

  1. 작업 디렉터리에서 chat_update_text_message_app.py
  2. chat_update_text_message_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)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # Replace SPACE with a space name.
      # Obtain the space name from the spaces resource of Chat API,
      # or from a space's URL.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE',
      updateMask='text',
      body={'text': 'Updated message!'}
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름으로, 다음에서 가져올 수 있습니다. spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.
    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_update_text_message_app.py
    

카드 메시지를 업데이트하거나 문자 메시지에 카드 메시지 추가하기

카드 메시지, 요청에 다음을 전달합니다.

  • chat.bot 승인 범위 카드 메시지 업데이트 요구사항 앱 인증
  • 업데이트할 메시지의 name입니다.
  • updateMask='cardsV2'
  • 업데이트된 메시지를 지정하는 body입니다.

업데이트된 메시지가 문자 메시지 그러면 카드가 문자 메시지에 추가됩니다 (계속 표시됨). 만약 업데이트된 메시지 자체가 카드를 선택한 경우 표시되는 카드는 다음과 같습니다. 이(가) 업데이트되었습니다.

메일을 카드 메시지:

Python

  1. 작업 디렉터리에서 chat_update_card_message.py
  2. chat_update_card_message.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)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # Replace SPACE with a space name.
      # Obtain the space name from the spaces resource of Chat API,
      # or from a space's URL.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE',
      updateMask='cardsV2',
      body=
      {
        'cardsV2': [{
          'cardId': 'updateCardMessage',
          'card': {
            'header': {
              'title': 'An Updated Card Message!',
              'subtitle': 'Updated with Chat REST API',
              'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
              'imageType': 'CIRCLE'
            },
            'sections': [
              {
                'widgets': [
                  {
                    'buttonList': {
                      'buttons': [
                        {
                          'text': 'Read the docs!',
                          'onClick': {
                            'openLink': {
                              'url': 'https://developers.google.com/chat'
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }]
      }
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름으로, 다음에서 가져올 수 있습니다. spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.

    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.

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

    python3 chat_update_card_message.py
    

Chat API는 Message 드림 업데이트된 메시지를 표시합니다.

여러 필드 경로를 동시에 사용하여 메시지 업데이트

메시지가 업데이트되면 있습니다. 예를 들어 업데이트 메시지 요청에서 textcardsv2 필드 경로를 동시에 전달하여 문자와 카드가 있습니다. 메시지에 텍스트만 있고 카드가 없는 경우 카드 메시지에 추가됩니다. 지원되는 필드 경로에 대한 자세한 내용은 보기 updateMask 매개변수

둘 다 업데이트하려면 text 드림 및 card 메시지를 보낼 때 사용자 인증, 요청에 다음을 전달합니다.

  • chat.messages 승인 범위
  • 업데이트할 메시지의 name입니다.
  • 업데이트할 메시지 필드 경로를 지정하는 updateMask(구분됨) 쉼표(updateMask='text', 'cardsV2')로 구분.

  • 업데이트된 모든 필드를 포함하여 업데이트된 메시지를 지정하는 body 학습합니다.

여기서 textcardsV2 필드 경로를 업데이트하는 방법은 메시지를 보낼 수 있습니다 사용자 인증:

Python

  1. 작업 디렉터리에서 chat_update_text_message_user.py
  2. chat_update_text_message_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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a message.
        '''
    
        # 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # Replace SPACE with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          #
          # Replace MESSAGE with a message name.
          # Obtain the message name from the response body returned
          # after creating a message asynchronously with Chat REST API.
          name='spaces/SPACE/messages/MESSAGE',
          updateMask='text,cardsV2',
          body=
          {'text': 'Updated message!',
                'cardsV2': [{
                  'cardId': 'updateCardMessage',
                  'card': {
                    'header': {
                      'title': 'An Updated Card Message!',
                      'subtitle': 'Updated with Chat REST API',
                      'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
                      'imageType': 'CIRCLE'
                    },
                    'sections': [
                      {
                        'widgets': [
                          {
                            'buttonList': {
                              'buttons': [
                                {
                                  'text': 'Read the docs!',
                                  'onClick': {
                                    'openLink': {
                                      'url': 'https://developers.google.com/chat'
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    ]
                  }
                }]
          }
    
        ).execute()
    
        # Prints details about the updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 코드에서 다음을 바꿉니다.

    • SPACE: 스페이스 이름으로, 다음에서 가져올 수 있습니다. spaces.list 메서드 Chat API 또는 스페이스의 URL에서 가져올 수 있습니다.
    • MESSAGE: 가져올 수 있는 메시지 이름입니다. 비동기식으로 메시지를 만든 후 반환된 응답 본문에서 삭제 Chat API 또는 맞춤 이름 메시지를 만들 때 할당됩니다.
  4. 작업 디렉터리에서 샘플을 빌드하고 실행합니다.

    python3 chat_update_text_message_user.py