मैसेज अपडेट करना

इस गाइड में, Google Chat API के Message संसाधन पर मौजूद update() तरीके का इस्तेमाल करके, किसी स्पेस में टेक्स्ट या कार्ड मैसेज अपडेट करने का तरीका बताया गया है. मैसेज की विशेषताओं में बदलाव करने के लिए, मैसेज को अपडेट करें. जैसे, मैसेज में क्या कहा गया है या कार्ड का कॉन्टेंट. कार्ड मैसेज से पहले टेक्स्ट मैसेज भी जोड़ा जा सकता है. इसके अलावा, टेक्स्ट मैसेज के बाद कार्ड भी जोड़ा जा सकता है.

Chat API में, Chat मैसेज को Message संसाधन के तौर पर दिखाया जाता है. Chat के उपयोगकर्ता सिर्फ़ टेक्स्ट वाले मैसेज भेज सकते हैं. हालांकि, Chat ऐप्लिकेशन मैसेज भेजने से जुड़ी कई अन्य सुविधाओं का इस्तेमाल कर सकते हैं. जैसे, स्टैटिक या इंटरैक्टिव यूज़र इंटरफ़ेस दिखाना, उपयोगकर्ताओं से जानकारी इकट्ठा करना, और निजी तौर पर मैसेज भेजना. Chat API के लिए उपलब्ध मैसेज भेजने की सुविधाओं के बारे में ज़्यादा जानने के लिए, Google Chat में मैसेज भेजने की सुविधा के बारे में खास जानकारी देखें.

ज़रूरी शर्तें

Node.js

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Python

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Java

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Apps Script

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास 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);
}

main().catch(console.error);

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() तरीके का इस्तेमाल करें या स्पेस के यूआरएल से आईडी पाएं.
  • 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);
}

main().catch(console.error);

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() तरीके का इस्तेमाल करें या स्पेस के यूआरएल से आईडी पाएं.
  • MESSAGE_NAME: मैसेज के name से मिला आईडी. Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद, आपको जवाब के मुख्य हिस्से से आईडी मिल सकता है. इसके अलावा, मैसेज बनाते समय असाइन किए गए कस्टम नाम से भी आईडी मिल सकता है.

Chat API, Message का एक इंस्टेंस दिखाता है. इसमें अपडेट किए गए मैसेज के बारे में जानकारी होती है.