একটি বার্তা আপডেট করুন

এই নির্দেশিকাটি ব্যাখ্যা করে যে কীভাবে একটি স্পেসের মধ্যে থাকা টেক্সট বা কার্ড মেসেজ আপডেট করার জন্য Google Chat API-এর Message রিসোর্সের update মেথডটি ব্যবহার করতে হয়। মেসেজের অ্যাট্রিবিউট, যেমন মেসেজের লেখা বা কার্ডের বিষয়বস্তু পরিবর্তন করতে একটি মেসেজ আপডেট করুন। এছাড়াও আপনি একটি কার্ড মেসেজের শুরুতে একটি টেক্সট মেসেজ যোগ করতে পারেন, অথবা একটি টেক্সট মেসেজের শেষে একটি কার্ড যুক্ত করতে পারেন।

চ্যাট এপিআই-তে, একটি চ্যাট মেসেজকে Message রিসোর্স দ্বারা উপস্থাপন করা হয়। যদিও চ্যাট ব্যবহারকারীরা শুধুমাত্র টেক্সট-ভিত্তিক মেসেজ পাঠাতে পারেন, চ্যাট অ্যাপগুলো আরও অনেক মেসেজিং ফিচার ব্যবহার করতে পারে, যার মধ্যে রয়েছে স্ট্যাটিক বা ইন্টারেক্টিভ ইউজার ইন্টারফেস প্রদর্শন করা, ব্যবহারকারীদের কাছ থেকে তথ্য সংগ্রহ করা এবং ব্যক্তিগতভাবে মেসেজ পাঠানো। চ্যাট এপিআই-এর জন্য উপলব্ধ মেসেজিং ফিচারগুলো সম্পর্কে আরও জানতে, গুগল চ্যাট মেসেজ ওভারভিউ দেখুন।

পূর্বশর্ত

নোড.জেএস

পাইথন

জাভা

অ্যাপস স্ক্রিপ্ট

একজন ব্যবহারকারীর পক্ষ থেকে একটি বার্তা আপডেট করুন

ব্যবহারকারী প্রমাণীকরণের মাধ্যমে শুধুমাত্র বার্তার লেখা আপডেট করা যায়।

ব্যবহারকারীর প্রমাণীকরণের মাধ্যমে কোনো বার্তা আপডেট করতে, আপনার অনুরোধে নিম্নলিখিত তথ্যগুলো প্রদান করুন:

  • chat.messages এর অনুমোদনের পরিধি নির্দিষ্ট করুন।
  • UpdateMessage মেথডটি কল করুন।
  • নিম্নলিখিতভাবে Message এর একটি ইনস্ট্যান্স হিসেবে message পাঠান:
    • আপডেট করার জন্য মেসেজটির name ফিল্ড সেট করা হয়, যার মধ্যে একটি স্পেস আইডি এবং একটি মেসেজ আইডি অন্তর্ভুক্ত থাকে।
    • text ফিল্ডটি নতুন টেক্সট দিয়ে সেট করা হয়েছে।
  • text ভ্যালুটি দিয়ে updateMask পাস করুন।

আপডেট করা বার্তাটি যদি একটি কার্ড বার্তা হয়, তাহলে লেখাটি কার্ডগুলোর শুরুতে যুক্ত হয় (এবং কার্ডগুলো প্রদর্শিত হতে থাকে)।

ব্যবহারকারীর প্রমাণীকরণের মাধ্যমে কীভাবে একটি বার্তা আপডেট করবেন, অথবা কার্ড বার্তার শুরুতে একটি টেক্সট বার্তা যুক্ত করবেন, তা এখানে দেওয়া হলো:

নোড.জেএস

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();

পাইথন

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()

জাভা

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));
    }
  }
}

অ্যাপস স্ক্রিপ্ট

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) ব্যবহার করে অ্যাসিঙ্ক্রোনাসভাবে একটি বার্তা তৈরি করার পরে ফেরত আসা রেসপন্স বডি থেকে, অথবা বার্তা তৈরির সময় নির্ধারিত কাস্টম নাম থেকে এই আইডিটি পেতে পারেন।

চ্যাট এপিআই Message এর একটি ইনস্ট্যান্স রিটার্ন করে, যা আপডেট করা মেসেজটির বিবরণ দেয়।

চ্যাট অ্যাপ হিসেবে একটি বার্তা আপডেট করুন

অ্যাপ অথেনটিকেশনের মাধ্যমে একটি মেসেজের টেক্সট এবং কার্ড উভয়ই আপডেট করা যায়।

অ্যাপ প্রমাণীকরণের মাধ্যমে কোনো বার্তা আপডেট করতে, আপনার অনুরোধে নিম্নলিখিত তথ্যগুলো দিন:

  • chat.bot অনুমোদনের পরিধি নির্দিষ্ট করুন।
  • UpdateMessage মেথডটি কল করুন।
  • নিম্নলিখিতভাবে Message এর একটি ইনস্ট্যান্স হিসেবে message পাঠান:
    • আপডেট করার জন্য মেসেজটির name ফিল্ড সেট করা হয়, যার মধ্যে একটি স্পেস আইডি এবং একটি মেসেজ আইডি অন্তর্ভুক্ত থাকে।
    • আপডেট করার প্রয়োজন হলে text ফিল্ডটিতে নতুন টেক্সট সেট করা হবে।
    • প্রয়োজন হলে cardsV2 ফিল্ডটি নতুন কার্ডগুলো দিয়ে সেট করা হবে।
  • আপডেট করার জন্য ফিল্ডের তালিকা, যেমন text এবং cardsV2 , সহ updateMask পাস করুন।

যদি আপডেট করা বার্তাটি একটি কার্ড বার্তা হয় এবং টেক্সট আপডেট করা হয়, তাহলে আপডেট করা টেক্সটটি কার্ডগুলোর শুরুতে যুক্ত হয় (যেগুলো প্রদর্শিত হতে থাকে)। যদি আপডেট করা বার্তাটি একটি টেক্সট বার্তা হয় এবং কার্ডগুলো আপডেট করা হয়, তাহলে আপডেট করা কার্ডগুলো টেক্সটের শেষে যুক্ত হয় (যেটি প্রদর্শিত হতে থাকে)।

অ্যাপ অথেন্টিকেশন ব্যবহার করে একটি মেসেজের টেক্সট ও কার্ড আপডেট করার পদ্ধতি নিচে দেওয়া হলো:

নোড.জেএস

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();

পাইথন

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()

জাভা

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));
    }
  }
}

অ্যাপস স্ক্রিপ্ট

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) ব্যবহার করে অ্যাসিঙ্ক্রোনাসভাবে একটি বার্তা তৈরি করার পরে ফেরত আসা রেসপন্স বডি থেকে, অথবা বার্তা তৈরির সময় নির্ধারিত কাস্টম নাম থেকে এই আইডিটি পেতে পারেন।

চ্যাট এপিআই Message এর একটি ইনস্ট্যান্স রিটার্ন করে, যা আপডেট করা মেসেজটির বিবরণ দেয়।

অ্যাসিঙ্ক্রোনাসভাবে কার্ডগুলি আপডেট করুন

ডেভেলপার প্রিভিউতে , আপনি ` replaceCards মেথড ব্যবহার করে একটি মেসেজের কার্ডগুলো অ্যাসিঙ্ক্রোনাসভাবে আপডেট করতে পারেন। ব্যবহারকারীর হস্তক্ষেপ ছাড়াই কোনো কার্ডের বিষয়বস্তু আপডেট করার জন্য এটি উপযোগী, যেমন—লিঙ্ক প্রিভিউ রিফ্রেশ করা বা কোনো টাস্কের স্ট্যাটাস আপডেট করা। এই মেথডটি অ্যাপ দ্বারা তৈরি করা মেসেজগুলোর জন্য কাজ করে, যার মধ্যে ব্যবহারকারীর পক্ষ থেকে তৈরি করা মেসেজও অন্তর্ভুক্ত।

বিস্তারিত জানতে, কার্ড তৈরি ও আপডেট করুন দেখুন।