メッセージを更新する

このガイドでは、次の Message リソースで patch メソッドを使用する方法について説明します。 スペース内のテキスト メッセージやカード メッセージを更新する方法を学びます。更新 メッセージの内容やメッセージの内容など、メッセージの属性を 。また、スペース名の前にテキスト メッセージを テキスト メッセージにカードを追加することもできます。

Chat API は update メソッド 必ず patch メソッド PATCH HTTP リクエストを使用しているため、 update では、 PUT HTTP リクエスト。詳しくは、 AIP-134 の PATCH および PUT セクション

Chat API では、チャット メッセージは Message リソース。 Chat ユーザーはテキストを含むメッセージしか送信できませんが、 Chat アプリでは、他にもさまざまなメッセージ機能を使用できます。 静的またはインタラクティブなユーザー インターフェースを表示し、 ユーザー、 プライベートでのメッセージの配信などですGoogle Chat 向けの 機能の詳細については、 Google Chat メッセージの概要

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

ユーザー認証を使用してテキスト メッセージを更新する、またはカード メッセージの先頭にテキスト メッセージを追加する

更新するには、 テキスト メッセージ ユーザー認証、 次のコードを追加します。

  • chat.messages 承認スコープ。
  • 更新するメッセージの name
  • updateMask='text'
  • 更新されたメッセージを指定する body

更新されたメッセージが カード メッセージ カード メッセージの前にテキスト メッセージが追加されます(カード メッセージは引き続き表示されます)。

更新方法は以下のとおりです。 テキスト メッセージ テキスト メッセージを カード メッセージ ユーザー認証:

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 メソッド スペースの URL から取得できます。
    • MESSAGE: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_update_text_message_user.py
    

アプリ認証を使用してテキスト メッセージを更新するか、カード メッセージの先頭にテキスト メッセージを追加します

更新するには、 テキスト メッセージ アプリの認証、 リクエストに以下を渡します。

  • chat.bot 承認スコープ。
  • 更新するメッセージの name
  • updateMask='text'
  • 更新されたメッセージを指定する body

更新されたメッセージがカード メッセージの場合、 カード メッセージの前にテキスト メッセージが追加されます(カード メッセージは引き続き表示されます)。

更新方法は以下のとおりです。 テキスト メッセージ テキスト メッセージに追加したり、スペースの カード メッセージ アプリの認証:

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 メソッド スペースの URL から取得できます。
    • MESSAGE: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_update_text_message_app.py
    

カード メッセージを更新する、またはテキスト メッセージにカード メッセージを追加する

更新するには、 カード メッセージ リクエストに以下を渡します。

  • chat.bot 承認スコープ。カード メッセージを更新するには、 アプリの認証
  • 更新するメッセージの name
  • updateMask='cardsV2'
  • 更新されたメッセージを指定する body

更新されたメッセージが テキスト メッセージ テキスト メッセージにカードが追加されます(テキスト メッセージは引き続き表示されます)。もし 更新されたメッセージ自体が card がある場合、表示されるカードは 更新しました。

メッセージを Google Chat に カード メッセージ:

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 メソッド スペースの URL から取得できます。

    • MESSAGE: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_update_card_message.py
    

Chat API は、メッセージに対して Message 更新されたメッセージの詳細が 記載されています

複数のフィールド パスを同時に持つメッセージを同時に更新する

メッセージが更新されると、同じ場所で複数のメッセージ フィールド パスを更新できます。 あります。たとえば、更新メッセージのリクエストでは、 text フィールドと cardsv2 フィールドパスを同時に指定することで、両方のフィールドが更新されます。 テキストとカードの両方が含まれます。メッセージにテキストのみが含まれ、カードが含まれていない場合は、 がメッセージに追加されます。サポートされているフィールドパスについて詳しくは、 updateMask パラメータ

両方のバージョンを text および card 返信することもできます。 ユーザー認証、 リクエストに以下を渡します。

  • chat.messages 承認スコープ。
  • 更新するメッセージの name
  • 更新するメッセージ フィールド パスを区切って指定する updateMask。 カンマで区切って指定します(updateMask='text', 'cardsV2')。

  • 更新されたすべてのフィールドを含む、更新されたメッセージを指定する body。 あります。

ここでは、text および cardsV2 フィールド パスを メッセージを ユーザー認証:

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 メソッド スペースの URL から取得できます。
    • MESSAGE: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_update_text_message_user.py