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

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

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

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

Node.js

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Java

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Apps Script

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

อัปเดตข้อความในนามของผู้ใช้

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

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

  • ระบุขอบเขตการให้สิทธิ์ chat.messages
  • เรียกใช้เมธอด UpdateMessage
  • ส่ง message เป็นอินสแตนซ์ของ Message พร้อมด้วยข้อมูลต่อไปนี้
    • ช่อง name ที่ตั้งค่าเป็นข้อความที่จะอัปเดต ซึ่งรวมถึงรหัสพื้นที่ทำงานและรหัสข้อความ
    • ช่อง text มีข้อความใหม่แล้ว
  • ส่ง updateMask พร้อมค่า text

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

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

Node.js

chat/client-libraries/cloud/update-message-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.messages',
];

// This sample shows how to update a message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    message: {
      // Replace SPACE_NAME and MESSAGE_NAME here
      name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
      text: 'Updated with user credential!',
    },
    // The field paths to update. Separate multiple values with commas or use `*`
    // to update all field paths.
    updateMask: {
      // The field paths to update.
      paths: ['text'],
    },
  };

  // Make the request
  const response = await chatClient.updateMessage(request);

  // Handle the response
  console.log(response);
}

await main();

Python

chat/client-libraries/cloud/update_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages"]

# This sample shows how to update a message with user credential
def update_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.UpdateMessageRequest(
        message = {
            # Replace SPACE_NAME and MESSAGE_NAME here
            "name": "spaces/SPACE_NAME/messages/MESSAGE_NAME",
            "text": "Updated with user credential!"
        },
        # The field paths to update. Separate multiple values with commas or use
        # `*` to update all field paths.
        update_mask = "text"
    )

    # Make the request
    response = client.update_message(request)

    # Handle the response
    print(response)

update_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/UpdateMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.UpdateMessageRequest;
import com.google.chat.v1.Message;
import com.google.protobuf.FieldMask;

// This sample shows how to update message with user credential.
public class UpdateMessageUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.messages";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      UpdateMessageRequest.Builder request = UpdateMessageRequest.newBuilder()
        .setMessage(Message.newBuilder()
          // replace SPACE_NAME and MESSAGE_NAME here
          .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME")
          .setText("Updated with user credential!"))
        .setUpdateMask(FieldMask.newBuilder()
          // The field paths to update.
          .addPaths("text"));
      Message response = chatServiceClient.updateMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to update a message with user credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages'
 * referenced in the manifest file (appsscript.json).
 */
function updateMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = "spaces/SPACE_NAME/messages/MESSAGE_NAME";
  const message = {
    text: "Updated with user credential!",
  };
  // The field paths to update. Separate multiple values with commas or use
  // `*` to update all field paths.
  const updateMask = "text";

  // Make the request
  const response = Chat.Spaces.Messages.patch(message, name, {
    updateMask: updateMask,
  });

  // Handle the response
  console.log(response);
}

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ค่าต่อไปนี้

  • SPACE_NAME: รหัสจาก name ของพื้นที่ทำงาน คุณสามารถดูรหัสได้โดยเรียกใช้เมธอด ListSpaces หรือจาก URL ของพื้นที่ทำงาน
  • MESSAGE_NAME: รหัสจาก name ของข้อความ คุณสามารถรับรหัสจากเนื้อหาการตอบกลับที่แสดงผลหลังจากสร้างข้อความแบบอะซิงโครนัสด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้าง

Chat API จะแสดงผลอินสแตนซ์ของ Message ที่ให้รายละเอียดข้อความที่อัปเดต

อัปเดตข้อความในฐานะแอปใน Chat

เมื่อใช้การตรวจสอบสิทธิ์แอป คุณจะอัปเดตได้ทั้งข้อความและการ์ดของข้อความ

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

  • ระบุขอบเขตการให้สิทธิ์ chat.bot
  • เรียกใช้เมธอด UpdateMessage
  • ส่ง message เป็นอินสแตนซ์ของ Message พร้อมด้วยข้อมูลต่อไปนี้
    • ช่อง name ที่ตั้งค่าเป็นข้อความที่จะอัปเดต ซึ่งรวมถึงรหัสพื้นที่ทำงานและรหัสข้อความ
    • ช่อง text ที่ตั้งค่าด้วยข้อความใหม่หากจำเป็นต้องอัปเดต
    • ช่อง cardsV2 จะตั้งค่าด้วยบัตรใหม่หากจำเป็นต้องอัปเดต
  • ส่ง updateMask พร้อมรายการช่องที่จะอัปเดต เช่น text และ cardsV2

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

วิธีอัปเดตข้อความและการ์ดของข้อความด้วยการตรวจสอบสิทธิ์แอปมีดังนี้

Node.js

chat/client-libraries/cloud/update-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to update a message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    message: {
      // Replace SPACE_NAME and MESSAGE_NAME here
      name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
      text: 'Text updated with app credential!',
      cardsV2: [
        {
          card: {
            header: {
              title: 'Card updated with app credential!',
              imageUrl:
                'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg',
            },
          },
        },
      ],
    },
    // The field paths to update. Separate multiple values with commas or use `*`
    // to update all field paths.
    updateMask: {
      // The field paths to update.
      paths: ['text', 'cards_v2'],
    },
  };

  // Make the request
  const response = await chatClient.updateMessage(request);

  // Handle the response
  console.log(response);
}

await main();

Python

chat/client-libraries/cloud/update_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to update a message with app credential
def update_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.UpdateMessageRequest(
        message = {
            # Replace SPACE_NAME and MESSAGE_NAME here
            "name": "spaces/SPACE_NAME/messages/MESSAGE_NAME",
            "text": "Text updated with app credential!",
            "cards_v2" : [{ "card": { "header": {
                "title": 'Card updated with app credential!',
                "image_url": 'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg'
            }}}]
        },
        # The field paths to update. Separate multiple values with commas or use
        # `*` to update all field paths.
        update_mask = "text,cardsV2"
    )

    # Make the request
    response = client.update_message(request)

    # Handle the response
    print(response)

update_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/UpdateMessageAppCred.java
import com.google.apps.card.v1.Card;
import com.google.apps.card.v1.Card.CardHeader;
import com.google.chat.v1.CardWithId;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.UpdateMessageRequest;
import com.google.chat.v1.Message;
import com.google.protobuf.FieldMask;

// This sample shows how to update message with app credential.
public class UpdateMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      UpdateMessageRequest.Builder request = UpdateMessageRequest.newBuilder()
        .setMessage(Message.newBuilder()
          // replace SPACE_NAME and MESSAGE_NAME here
          .setName("spaces/SPACE_NAME/messages/MESSAGE_NAME")
          .setText("Text updated with app credential!")
          .addCardsV2(CardWithId.newBuilder().setCard(Card.newBuilder()
            .setHeader(CardHeader.newBuilder()
              .setTitle("Card updated with app credential!")
              .setImageUrl("https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg")))))
        .setUpdateMask(FieldMask.newBuilder()
          // The field paths to update.
          .addAllPaths(List.of("text", "cards_v2")));
      Message response = chatServiceClient.updateMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to update a message with app credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function updateMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = "spaces/SPACE_NAME/messages/MESSAGE_NAME";
  const message = {
    text: "Text updated with app credential!",
    cardsV2: [
      {
        card: {
          header: {
            title: "Card updated with app credential!",
            imageUrl:
              "https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg",
          },
        },
      },
    ],
  };
  // The field paths to update. Separate multiple values with commas or use
  // `*` to update all field paths.
  const updateMask = "text,cardsV2";

  // Make the request
  const response = Chat.Spaces.Messages.patch(
    message,
    name,
    {
      updateMask: updateMask,
    },
    getHeaderWithAppCredentials(),
  );

  // Handle the response
  console.log(response);
}

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ค่าต่อไปนี้

  • SPACE_NAME: รหัสจาก name ของพื้นที่ทำงาน คุณสามารถดูรหัสได้โดยเรียกใช้เมธอด ListSpaces หรือจาก URL ของพื้นที่ทำงาน
  • MESSAGE_NAME: รหัสจาก name ของข้อความ คุณสามารถรับรหัสจากเนื้อหาการตอบกลับที่แสดงผลหลังจากสร้างข้อความแบบอะซิงโครนัสด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้าง

Chat API จะแสดงผลอินสแตนซ์ของ Message ที่ให้รายละเอียดข้อความที่อัปเดต

อัปเดตการ์ดแบบอะซิงโครนัส

คุณสามารถอัปเดตการ์ดในข้อความแบบไม่พร้อมกันได้โดยใช้เมธอด replaceCards ซึ่งจะเป็นประโยชน์สำหรับการอัปเดตเนื้อหาของการ์ดโดยไม่ต้องมีการโต้ตอบของผู้ใช้ เช่น การรีเฟรชตัวอย่างลิงก์หรือการอัปเดตสถานะของงาน วิธีนี้ใช้ได้กับข้อความที่สร้างโดยแอป รวมถึงข้อความที่สร้างในนามของผู้ใช้

ดูรายละเอียดได้ที่หัวข้อสร้างและอัปเดตการ์ดแบบอินเทอร์แอกทีฟ