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

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

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

แหล่งข้อมูล Message แสดงถึงข้อความหรือข้อความบนการ์ดใน Google Chat คุณอาจcreate, get, update หรือdelete ข้อความใน Google Chat API ได้โดยเรียกใช้เมธอดที่เกี่ยวข้อง หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ SMS และข้อความการ์ด โปรดดูภาพรวมข้อความ Google Chat

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

Python

  • Python 3.6 ขึ้นไป
  • เครื่องมือการจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
  • การกำหนดค่าการให้สิทธิ์สำหรับแอป Chat มีดังนี้

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

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

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

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

วิธีอัปเดต SMS หรือเพิ่ม 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 created membership.
        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 ไว้ด้านหน้าข้อความการ์ดด้วยการตรวจสอบสิทธิ์แอป

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

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

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

วิธีอัปเดต SMS ใน SMS หรือเพิ่ม 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
    

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

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

  • ขอบเขตการให้สิทธิ์ 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 created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ส่วนต่อไปนี้

    • SPACE: ชื่อพื้นที่ทำงาน ซึ่งคุณจะดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
    • MESSAGE: ชื่อข้อความที่คุณจะได้รับจากเนื้อหาการตอบกลับที่แสดงหลังจากสร้างข้อความแบบไม่พร้อมกันด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้าง
  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    python3 chat_update_text_message_user.py