อัปเดตข้อความ

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด patch ในแหล่งข้อมูล Message ของ Google Chat API เพื่ออัปเดต SMS หรือข้อความการ์ดในพื้นที่ทำงาน อัปเดต ข้อความเพื่อเปลี่ยนแอตทริบิวต์ของข้อความ เช่นสิ่งที่เขียน หรือเนื้อหาของข้อความ นอกจากนี้คุณยังเพิ่ม SMS ไว้ด้านหน้า ข้อความการ์ด หรือเพิ่มการ์ดต่อท้ายข้อความ

Chat API ยังรองรับ update เมธอด, แต่เราขอแนะนำให้คุณเรียก patch วิธี เนื่องจากใช้คำขอ HTTP PATCH ขณะที่ update ใช้องค์ประกอบ คำขอ HTTP PUT ดูข้อมูลเพิ่มเติมได้ที่ ส่วน PATCH และ PUT ของ AIP-134

ใน Chat API ข้อความใน Chat จะแสดงด้วยส่วนหัว แหล่งข้อมูล Message แม้ว่าผู้ใช้ Chat จะส่งได้เฉพาะข้อความที่มีข้อความเท่านั้น แอปแชทสามารถใช้ฟีเจอร์การรับส่งข้อความอื่นๆ ได้มากมาย รวมถึง แสดงอินเทอร์เฟซผู้ใช้แบบคงที่หรือแบบอินเทอร์แอกทีฟ ซึ่งรวบรวมข้อมูลจาก ผู้ใช้ และการส่งข้อความแบบส่วนตัว หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับการรับส่งข้อความ ฟีเจอร์ที่ใช้ได้กับ Chat API โปรดดู ภาพรวมข้อความ Google Chat

ข้อกำหนดเบื้องต้น

Python

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • เครื่องมือจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

อัปเดต SMS หรือแทรกข้อความไว้ด้านหน้าข้อความในการ์ดที่มีการตรวจสอบสิทธิ์ผู้ใช้

หากต้องการอัปเดต SMS กับ การตรวจสอบสิทธิ์ผู้ใช้, ผ่าน ต่อไปนี้ในคำขอของคุณ

  • ขอบเขตการให้สิทธิ์ chat.messages
  • name ของข้อความที่จะอัปเดต
  • updateMask='text'
  • body ที่ระบุข้อความที่อัปเดตแล้ว

หากข้อความที่อัปเดตเป็น ข้อความในการ์ด ข้อความนั้นจะแทรกไว้ข้างหน้าข้อความของการ์ด (ซึ่งจะแสดงต่อไป)

ต่อไปนี้เป็นวิธีอัปเดต SMS หรือ เพิ่มข้อความไว้ด้านหน้า ข้อความในการ์ด กับ การตรวจสอบสิทธิ์ผู้ใช้:

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
    

อัปเดต SMS หรือแทรกข้อความไว้ด้านหน้าข้อความในการ์ดโดยใช้การตรวจสอบสิทธิ์แอป

หากต้องการอัปเดต SMS กับ การตรวจสอบสิทธิ์แอป ส่งต่อสิ่งต่อไปนี้ในคำขอของคุณ

  • ขอบเขตการให้สิทธิ์ chat.bot
  • name ของข้อความที่จะอัปเดต
  • updateMask='text'
  • body ที่ระบุข้อความที่อัปเดตแล้ว

หากข้อความที่อัปเดตเป็นข้อความของการ์ด ข้อความนั้นจะแทรกไว้ข้างหน้าข้อความของการ์ด (ซึ่งจะแสดงต่อไป)

ต่อไปนี้เป็นวิธีอัปเดต SMS ต่อท้ายข้อความ หรือเพิ่มข้อความ ข้อความในการ์ด กับ การตรวจสอบสิทธิ์แอป:

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
    

อัปเดตข้อความการ์ดหรือเพิ่มข้อความในการ์ดต่อท้าย SMS

หากต้องการอัปเดต ข้อความในการ์ด ส่งต่อสิ่งต่อไปนี้ในคำขอของคุณ

  • ขอบเขตการให้สิทธิ์ 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 ซึ่งแสดงรายละเอียดข้อความที่ได้รับการอัปเดต

อัปเดตข้อความด้วยเส้นทางช่องหลายเส้นทางพร้อมกัน

เมื่ออัปเดตข้อความแล้ว คุณสามารถอัปเดตเส้นทางช่องข้อความหลายเส้นทางได้ที่ เช่น ในคำขออัปเดตข้อความ คุณสามารถระบุการเปลี่ยนแปลง 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 วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
    • MESSAGE: ชื่อข้อความซึ่งคุณจะดูได้ จากเนื้อหาการตอบกลับที่ส่งคืนหลังจากสร้างข้อความแบบไม่พร้อมกัน ด้วย Chat API หรือ ชื่อที่กำหนดเอง ที่กำหนดให้กับข้อความขณะสร้าง
  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_update_text_message_user.py