ইন্টারেক্টিভ কার্ড তৈরি এবং আপডেট করুন

এই নির্দেশিকাটি ব্যাখ্যা করে যে কীভাবে ব্যবহারকারীদের পক্ষ থেকে ইন্টারেক্টিভ কার্ড ধারণকারী বার্তা তৈরি করতে Google Chat API ব্যবহার করতে হয় এবং কীভাবে সেই কার্ডগুলিকে অ্যাসিঙ্ক্রোনাসভাবে আপডেট করতে হয়।

নিম্নলিখিত কাজগুলি করার সময় কার্ড তৈরি এবং আপডেট করা কার্যকর:

  • ব্যবহারকারীর পক্ষ থেকে একটি টাস্ক বা বহিরাগত সম্পদের প্রতিনিধিত্ব করে এমন একটি কার্ড পোস্ট করুন।
  • ব্যবহারকারীর ইন্টারঅ্যাকশনের জন্য অপেক্ষা না করেই, বাহ্যিক ইভেন্টের উপর ভিত্তি করে কার্ডের স্থিতি (উদাহরণস্বরূপ, "প্রগতিতে" থেকে "সম্পূর্ণ") আপডেট করুন।
  • ব্যবহারকারীর মেসেজে কার্ডের কন্টেন্ট রিফ্রেশ করুন, যেমন লিঙ্ক প্রিভিউ।

ডেভেলপার প্রিভিউ প্রোগ্রামের বাইরে, ব্যবহারকারী প্রমাণীকরণের মাধ্যমে তৈরি বার্তাগুলিতে কেবল পাঠ্য থাকতে পারে।

পূর্বশর্ত

নোড.জেএস

  • Google Chat অ্যাক্সেস সহ একটি ব্যবসা বা এন্টারপ্রাইজ Google Workspace অ্যাকাউন্ট।

পাইথন

  • Google Chat অ্যাক্সেস সহ একটি ব্যবসা বা এন্টারপ্রাইজ Google Workspace অ্যাকাউন্ট।

জাভা

  • Google Chat অ্যাক্সেস সহ একটি ব্যবসা বা এন্টারপ্রাইজ Google Workspace অ্যাকাউন্ট।

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

  • Google Chat অ্যাক্সেস সহ একটি ব্যবসা বা এন্টারপ্রাইজ Google Workspace অ্যাকাউন্ট।

ডেভেলপার প্রিভিউ বৈশিষ্ট্যগুলি অ্যাক্সেস করার জন্য একটি API কী তৈরি করুন

ডেভেলপার প্রিভিউ API পদ্ধতি কল করার জন্য, আপনাকে API আবিষ্কার ডকুমেন্টের একটি অ-সর্বজনীন ডেভেলপার প্রিভিউ সংস্করণ ব্যবহার করতে হবে। অনুরোধটি প্রমাণীকরণ করতে, আপনাকে একটি API কী পাস করতে হবে।

API কী তৈরি করতে, আপনার অ্যাপের Google Cloud Project খুলুন এবং নিম্নলিখিতগুলি করুন:

  1. গুগল ক্লাউড কনসোলে, মেনু > API এবং পরিষেবা > শংসাপত্রগুলিতে যান।

    শংসাপত্রগুলিতে যান

  2. ক্রেডেনশিয়াল তৈরি করুন > API কী ক্লিক করুন।
  3. আপনার নতুন API কী প্রদর্শিত হবে।
    • আপনার অ্যাপের কোডে ব্যবহারের জন্য আপনার API কী কপি করতে, "Copy এ ক্লিক করুন। API কীটি আপনার প্রকল্পের শংসাপত্রের "API Keys" বিভাগেও পাওয়া যাবে।
    • অননুমোদিত ব্যবহার রোধ করার জন্য, আমরা সুপারিশ করছি যে API কী কোথায় এবং কোন API-এর জন্য ব্যবহার করা যেতে পারে তা সীমাবদ্ধ করুন। আরও বিস্তারিত জানার জন্য, API সীমাবদ্ধতা যোগ করুন দেখুন।

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

ব্যবহারকারীর পক্ষ থেকে কার্ড ব্যবহার করে একটি বার্তা তৈরি করতে, ব্যবহারকারী প্রমাণীকরণ ব্যবহার করুন।

বার্তাটি তৈরি করতে, আপনার অনুরোধে নিম্নলিখিতগুলি উল্লেখ করুন:

  • chat.messages.create অথবা chat.messages অনুমোদনের সুযোগ।
  • Message রিসোর্সের cardsV2 ক্ষেত্র, যেখানে কার্ডের ডেটা থাকবে।
  • প্রতিটি কার্ডের cardId , যা অ্যাসিঙ্ক্রোনাস আপডেটের জন্য প্রয়োজনীয়।

নিম্নলিখিত উদাহরণে দেখানো হয়েছে কিভাবে একজন ব্যবহারকারীর পক্ষ থেকে একটি কার্ড ব্যবহার করে একটি বার্তা তৈরি করতে হয়:

নোড.জেএস

/**
 * This sample shows how to create a message with a card on behalf of a user.
 */
const {google} = require('googleapis');
const {auth} = require('google-auth-library');

async function main() {
  // Create a client
  const authClient = await auth.getClient({
    scopes: ['https://www.googleapis.com/auth/chat.messages.create']
  });
  google.options({auth: authClient});

  // Initialize the Chat API with Developer Preview labels
  const chat = await google.discoverAPI(
    'https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY'
  );

  // The space to create the message in.
  const parent = 'spaces/SPACE_NAME';

  // Create the request
  const request = {
    parent: parent,
    requestBody: {
      text: 'Here is a card created on my behalf:',
      cardsV2: [{
        cardId: 'unique-card-id',
        card: {
          header: {
            title: 'Card Title',
            subtitle: 'Card Subtitle'
          },
          sections: [{
            widgets: [{
              textParagraph: {
                text: 'This card is attached to a user message.'
              }
            }]
          }]
        }
      }]
    }
  };

  // Call the API
  const response = await chat.spaces.messages.create(request);

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

main().catch(console.error);

পাইথন

"""
This sample shows how to create a message with a card on behalf of a user.
"""
from google.oauth2 import service_account
from googleapiclient.discovery import build
import google.auth

def create_message_with_card():
    # Create a client
    scopes = ["https://www.googleapis.com/auth/chat.messages.create"]
    credentials, _ = google.auth.default(scopes=scopes)

    # Build the service endpoint for Chat API with Developer Preview labels.
    service = build(
        'chat',
        'v1',
        credentials=credentials,
        discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY'
    )

    # The space to create the message in.
    parent = "spaces/SPACE_NAME"

    # Create the request
    result = service.spaces().messages().create(
        parent=parent,
        body={
            'text': 'Here is a card created on my behalf:',
            'cardsV2': [{
                'cardId': 'unique-card-id',
                'card': {
                    'header': {
                        'title': 'Card Title',
                        'subtitle': 'Card Subtitle'
                    },
                    'sections': [{
                        'widgets': [{
                            'textParagraph': {
                                'text': 'This card is attached to a user message.'
                            }
                        }]
                    }]
                }
            }]
        }
    ).execute()

    print(result)

if __name__ == "__main__":
    create_message_with_card()

জাভা

/**
 * This sample shows how to create a message with a card on behalf of a user.
 */
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CreateMessageWithCard {
  public static void main(String[] args) throws Exception {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    GsonFactory jsonFactory = GsonFactory.getDefaultInstance();

    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Arrays.asList("https://www.googleapis.com/auth/chat.messages.create"));
    HttpRequestFactory requestFactory = transport.createRequestFactory(new HttpCredentialsAdapter(credentials));

    String parent = "spaces/SPACE_NAME";
    GenericUrl url = new GenericUrl("https://chat.googleapis.com/v1/" + parent + "/messages");

    // Construct the message body
    Map<String, Object> message = new HashMap<>();
    message.put("text", "Here is a card created on my behalf:");

    Map<String, Object> header = new HashMap<>();
    header.put("title", "Card Title");
    header.put("subtitle", "Card Subtitle");

    Map<String, Object> textParagraph = new HashMap<>();
    textParagraph.put("text", "This card is attached to a user message.");

    Map<String, Object> widget = new HashMap<>();
    widget.put("textParagraph", textParagraph);

    Map<String, Object> section = new HashMap<>();
    section.put("widgets", Collections.singletonList(widget));

    Map<String, Object> card = new HashMap<>();
    card.put("header", header);
    card.put("sections", Collections.singletonList(section));

    Map<String, Object> cardWithId = new HashMap<>();
    cardWithId.put("cardId", "unique-card-id");
    cardWithId.put("card", card);

    message.put("cardsV2", Collections.singletonList(cardWithId));

    HttpRequest request = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, message));
    System.out.println(request.execute().parseAsString());
  }
}

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

/**
 * This sample shows how to create a message with a card on behalf of a user.
 */
function createMessageWithCard() {
  const parent = 'spaces/SPACE_NAME';
  const url = `https://chat.googleapis.com/v1/${parent}/messages`;

  const message = {
    text: 'Here is a card created on my behalf:',
    cardsV2: [{
      cardId: 'unique-card-id',
      card: {
        header: {
          title: 'Card Title',
          subtitle: 'Card Subtitle'
        },
        sections: [{
          widgets: [{
            textParagraph: {
              text: 'This card is attached to a user message.'
            }
          }]
        }]
      }
    }]
  };

  const options = {
    method: 'post',
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
    },
    contentType: 'application/json',
    payload: JSON.stringify(message),
    muteHttpExceptions: true
  };

  try {
    const response = UrlFetchApp.fetch(url, options);
    console.log(response.getContentText());
  } catch (err) {
    console.log('Failed to create message: ' + err.message);
  }
}

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

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

কার্ডগুলি আপডেট করতে, নিম্নলিখিত পদ্ধতিতে replaceCards পদ্ধতিতে কল করুন:

  • chat.bot অনুমোদনের সুযোগ।
  • আপডেট করার জন্য বার্তার name
  • নতুন cardsV2 তালিকা। এটি বার্তায় বিদ্যমান সমস্ত কার্ড প্রতিস্থাপন করে। যদি আপনি একটি খালি তালিকা প্রদান করেন, তাহলে কার্ডগুলি সরানো হবে।

নিম্নলিখিত উদাহরণে বার্তার কার্ডগুলি কীভাবে আপডেট করতে হয় তা দেখানো হয়েছে:

নোড.জেএস

/**
 * This sample shows how to update cards on a message.
 */
const {google} = require('googleapis');
const {auth} = require('google-auth-library');

async function main() {
  // Create a client with app credentials
  const authClient = await auth.getClient({
    scopes: ['https://www.googleapis.com/auth/chat.bot']
  });
  google.options({auth: authClient});

  // Initialize the Chat API with Developer Preview labels
  const chat = await google.discoverAPI(
    'https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY'
  );

  // The message to update.
  const messageName = 'spaces/SPACE_NAME/messages/MESSAGE_ID';

  // Create the request
  const request = {
    name: messageName,
    requestBody: {
      cardsV2: [{
        cardId: 'unique-card-id',
        card: {
          header: {
            title: 'Updated Card Title',
            subtitle: 'Updated Card Subtitle'
          },
          sections: [{
            widgets: [{
              textParagraph: {
                text: 'The card content has been updated asynchronously.'
              }
            }]
          }]
        }
      }]
    }
  };

  // Call the API
  await chat.spaces.messages.replaceCards(request);
  console.log('Cards updated.');
}

main().catch(console.error);

পাইথন

"""
This sample shows how to update cards on a message.
"""
from google.oauth2 import service_account
from googleapiclient.discovery import build
import google.auth

def replace_message_cards():
    # Create a client with app credentials
    scopes = ["https://www.googleapis.com/auth/chat.bot"]
    credentials, _ = google.auth.default(scopes=scopes)

    # Build the service endpoint for Chat API with Developer Preview labels.
    service = build(
        'chat',
        'v1',
        credentials=credentials,
        discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY'
    )

    # The message to update.
    message_name = "spaces/SPACE_NAME/messages/MESSAGE_ID"

    # Create the request
    result = service.spaces().messages().replaceCards(
        name=message_name,
        body={
            'cardsV2': [{
                'cardId': 'unique-card-id',
                'card': {
                    'header': {
                        'title': 'Updated Card Title',
                        'subtitle': 'Updated Card Subtitle'
                    },
                    'sections': [{
                        'widgets': [{
                            'textParagraph': {
                                'text': 'The card content has been updated asynchronously.'
                            }
                        }]
                    }]
                }
            }]
        }
    ).execute()

    print("Cards updated.")

if __name__ == "__main__":
    replace_message_cards()

জাভা

/**
 * This sample shows how to update cards on a message.
 */
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ReplaceMessageCards {
  public static void main(String[] args) throws Exception {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    GsonFactory jsonFactory = GsonFactory.getDefaultInstance();

    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Arrays.asList("https://www.googleapis.com/auth/chat.bot"));
    HttpRequestFactory requestFactory = transport.createRequestFactory(new HttpCredentialsAdapter(credentials));

    String messageName = "spaces/SPACE_NAME/messages/MESSAGE_ID";
    GenericUrl url = new GenericUrl("https://chat.googleapis.com/v1/" + messageName + ":replaceCards");

    // Construct the body
    Map<String, Object> header = new HashMap<>();
    header.put("title", "Updated Card Title");
    header.put("subtitle", "Updated Card Subtitle");

    Map<String, Object> textParagraph = new HashMap<>();
    textParagraph.put("text", "The card content has been updated asynchronously.");

    Map<String, Object> widget = new HashMap<>();
    widget.put("textParagraph", textParagraph);

    Map<String, Object> section = new HashMap<>();
    section.put("widgets", Collections.singletonList(widget));

    Map<String, Object> card = new HashMap<>();
    card.put("header", header);
    card.put("sections", Collections.singletonList(section));

    Map<String, Object> cardWithId = new HashMap<>();
    cardWithId.put("cardId", "unique-card-id");
    cardWithId.put("card", card);

    Map<String, Object> body = new HashMap<>();
    body.put("cardsV2", Collections.singletonList(cardWithId));

    HttpRequest request = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, body));
    request.execute();
    System.out.println("Cards updated.");
  }
}

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

/**
 * This sample shows how to update cards on a message.
 */
function replaceMessageCards() {
  const messageName = 'spaces/SPACE_NAME/messages/MESSAGE_ID';
  const url = `https://chat.googleapis.com/v1/${messageName}:replaceCards`;

  const request = {
    cardsV2: [{
      cardId: 'unique-card-id',
      card: {
        header: {
          title: 'Updated Card Title',
          subtitle: 'Updated Card Subtitle'
        },
        sections: [{
          widgets: [{
            textParagraph: {
              text: 'The card content has been updated asynchronously.'
            }
          }]
        }]
      }
    }]
  };

  const options = {
    method: 'post',
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
    },
    contentType: 'application/json',
    payload: JSON.stringify(request),
    muteHttpExceptions: true
  };

  try {
    const response = UrlFetchApp.fetch(url, options);
    console.log('Cards updated.');
  } catch (err) {
    console.log('Failed to update cards: ' + err.message);
  }
}

সীমাবদ্ধতা

  • কোনও ব্যবহারকারীর পক্ষ থেকে কার্ড ব্যবহার করে মেসেজ তৈরি করার সময় বা কার্ড আপডেট করার সময়, চ্যাট অ্যাপটিকে অবশ্যই স্পেসের সদস্য হতে হবে। এই প্রয়োজনীয়তা প্রযোজ্য হবে যখন:

    এই প্রয়োজনীয়তা অন্যান্য API থেকে আলাদা যা ব্যবহারকারী প্রমাণীকরণ ব্যবহার করে, যার জন্য সাধারণত অ্যাপটিকে স্পেসের সদস্য হতে হয় না।

  • replaceCards পদ্ধতি কার্ড প্রতিস্থাপন এবং অপসারণ সমর্থন করে, এবং আপনি সেগুলি প্রতিস্থাপন করার সময় অতিরিক্ত কার্ড যোগ করতে পারেন, কিন্তু আপনি এমন কোনও বার্তায় কার্ড যোগ করতে পারবেন না যার ইতিমধ্যে কার্ড নেই।

  • চ্যাট অ্যাপটি কেবল সেই কার্ডগুলি প্রতিস্থাপন করতে পারে যা এটি একটি বার্তার সাথে সংযুক্ত করে, অন্যান্য চ্যাট অ্যাপগুলি সংযুক্ত করে এমন কার্ডগুলি নয়।

  • যদি কোনও ব্যবহারকারী বার্তার টেক্সট সম্পাদনা করেন, তাহলে চ্যাট অ্যাপের মালিকানাধীন কার্ডগুলি সরানো হবে এবং আপনি আর সেগুলি আপডেট করতে পারবেন না।