更新訊息

本指南說明如何針對以下項目的 Message 資源使用 patch 方法: 使用 Google Chat API 更新聊天室中的文字或資訊卡訊息。更新 訊息變更訊息屬性,例如輸入的內容 資訊卡您也可以在 或將卡片附加在簡訊中

Chat API 也支援 update 方法, 但強烈建議您呼叫 patch 方法 因為 HTTP 要求使用 PATCH HTTP 要求 update 會使用 PUT 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

如果更新的訊息是 資訊卡訊息 那麼簡訊會前面加上資訊卡訊息 (繼續顯示)。

以下是更新 文字訊息或 在 資訊卡訊息 同時 使用者驗證

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 方法 或聊天室網址傳送
    • 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 方法 或聊天室網址傳送
    • 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 方法 或聊天室網址傳送

    • 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 方法 或聊天室網址傳送
    • MESSAGE:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的
  4. 在工作目錄中建構並執行範例:

    python3 chat_update_text_message_user.py