এই নির্দেশিকা থেকে আপনি জানতে পারবেন যে কীভাবে Google Chat API ব্যবহার করে ব্যবহারকারীর হয়ে ইন্টার্যাক্টিভ কার্ড সহ মেসেজ তৈরি করা যায় এবং কীভাবে সেইসব কার্ড অ্যাসিঙ্ক্রোনাসভাবে আপডেট করা যায়।
আপনি নিম্নলিখিত কাজগুলি করতে চাইলে কার্ড তৈরি ও আপডেট করা উপযোগী:
- ব্যবহারকারীর হয়ে কোনও টাস্ক বা এক্সটার্নাল রিসোর্স দেখায় এমন কার্ড পোস্ট করুন।
- ব্যবহারকারীর ইন্টার্যাকশনের জন্য অপেক্ষা না করে, এক্সটার্নাল ইভেন্টের উপর ভিত্তি করে কার্ডের স্ট্যাটাস আপডেট করুন (যেমন, "প্রসেস চলছে" থেকে "সম্পূর্ণ হয়েছে")।
- ব্যবহারকারীর মেসেজে কার্ডের কন্টেন্ট রিফ্রেশ করুন, যেমন লিঙ্কের প্রিভিউ।
পূর্বশর্ত
Node.js
- Google Chat-এ অ্যাক্সেস সহ Google Workspace-এর Business বা Enterprise অ্যাকাউন্ট।
- আপনার পরিবেশ সেট-আপ করুন:
- Google Cloud প্রোজেক্ট তৈরি করুন।
- OAuth-এর সম্মতি সংক্রান্ত স্ক্রিন কনফিগার করুন।
- আপনার Chat অ্যাপের নাম, আইকন ও বিবরণ সহ Google Chat API চালু ও কনফিগার করুন।
- Node.js Google API ক্লায়েন্ট লাইব্রেরি ইনস্টল করুন।
- আপনার Google Chat API অনুরোধে আপনি কীভাবে যাচাই করতে চান তার উপর ভিত্তি করে অ্যাক্সেস ক্রেডেনশিয়াল তৈরি করুন:
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন। - Chat অ্যাপ হিসেবে যাচাই করতে,
পরিষেবা অ্যাকাউন্ট
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন।
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
- আপনি ব্যবহারকারী নাকি Chat অ্যাপ হিসেবে যাচাইকরণ করতে চান তার উপর ভিত্তি করে অনুমোদনের স্কোপ বেছে নিন।
Python
- Google Chat-এ অ্যাক্সেস সহ Google Workspace-এর Business বা Enterprise অ্যাকাউন্ট।
- আপনার পরিবেশ সেট-আপ করুন:
- Google Cloud প্রোজেক্ট তৈরি করুন।
- OAuth-এর সম্মতি সংক্রান্ত স্ক্রিন কনফিগার করুন।
- আপনার Chat অ্যাপের নাম, আইকন ও বিবরণ সহ Google Chat API চালু ও কনফিগার করুন।
- Python Google API ক্লায়েন্ট লাইব্রেরি ইনস্টল করুন।
- আপনার Google Chat API অনুরোধে আপনি কীভাবে যাচাই করতে চান তার উপর ভিত্তি করে অ্যাক্সেস ক্রেডেনশিয়াল তৈরি করুন:
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন। - Chat অ্যাপ হিসেবে যাচাই করতে,
পরিষেবা অ্যাকাউন্ট
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন।
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
- আপনি ব্যবহারকারী নাকি Chat অ্যাপ হিসেবে যাচাইকরণ করতে চান তার উপর ভিত্তি করে অনুমোদনের স্কোপ বেছে নিন।
Java
- Google Chat-এ অ্যাক্সেস সহ Google Workspace-এর Business বা Enterprise অ্যাকাউন্ট।
- আপনার পরিবেশ সেট-আপ করুন:
- Google Cloud প্রোজেক্ট তৈরি করুন।
- OAuth-এর সম্মতি সংক্রান্ত স্ক্রিন কনফিগার করুন।
- আপনার Chat অ্যাপের নাম, আইকন ও বিবরণ সহ Google Chat API চালু ও কনফিগার করুন।
- Java Google API ক্লায়েন্ট লাইব্রেরি ইনস্টল করুন।
- আপনার Google Chat API অনুরোধে আপনি কীভাবে যাচাই করতে চান তার উপর ভিত্তি করে অ্যাক্সেস ক্রেডেনশিয়াল তৈরি করুন:
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন। - Chat অ্যাপ হিসেবে যাচাই করতে,
পরিষেবা অ্যাকাউন্ট
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি
credentials.jsonনামের JSON ফাইল হিসেবে সেভ করুন।
- Chat ব্যবহারকারী হিসেবে যাচাইকরণ করতে,
OAuth ক্লায়েন্ট আইডি
ক্রেডেনশিয়াল তৈরি করুন এবং ক্রেডেনশিয়ালটি আপনার লোকাল ডিরেক্টরিতে
- আপনি ব্যবহারকারী নাকি Chat অ্যাপ হিসেবে যাচাইকরণ করতে চান তার উপর ভিত্তি করে অনুমোদনের স্কোপ বেছে নিন।
Apps Script
- Google Chat-এ অ্যাক্সেস সহ Google Workspace-এর Business বা Enterprise অ্যাকাউন্ট।
- আপনার পরিবেশ সেট-আপ করুন:
- Google Cloud প্রোজেক্ট তৈরি করুন।
- OAuth-এর সম্মতি সংক্রান্ত স্ক্রিন কনফিগার করুন।
- আপনার Chat অ্যাপের নাম, আইকন ও বিবরণ সহ Google Chat API চালু ও কনফিগার করুন।
- স্ট্যান্ডঅ্যালোন Apps স্ক্রিপ্ট প্রোজেক্ট তৈরি করুন, এবং উন্নত চ্যাট পরিষেবা চালু করুন।
- এই গাইডে, আপনাকে অবশ্যই ব্যবহারকারী অথবা অ্যাপ যাচাইকরণ ব্যবহার করতে হবে। Chat অ্যাপ হিসেবে যাচাই করতে, পরিষেবা অ্যাকাউন্টের ক্রেডেনশিয়াল তৈরি করুন। ধাপগুলি জানতে, Google Chat অ্যাপ হিসেবে যাচাই ও অনুমোদন করুন দেখুন।
- আপনি ব্যবহারকারী নাকি Chat অ্যাপ হিসেবে যাচাইকরণ করতে চান তার উপর ভিত্তি করে অনুমোদনের স্কোপ বেছে নিন।
ব্যবহারকারীর হয়ে কার্ড মেসেজ তৈরি করুন
কোনও ব্যবহারকারীর হয়ে কার্ড সহ মেসেজ তৈরি করতে, ব্যবহারকারীর যাচাইকরণ ব্যবহার করুন।
মেসেজ তৈরি করতে, আপনার অনুরোধে নিম্নলিখিত তথ্য উল্লেখ করুন:
chat.messages.createবাchat.messagesঅনুমোদনের স্কোপ।Messageরিসোর্সেরcardsV2ফিল্ড, যাতে কার্ডের ডেটা আছে।- প্রতিটি কার্ডের জন্য
cardId, যা অ্যাসিঙ্ক্রোনাস আপডেটের জন্য প্রয়োজন।
নিম্নলিখিত উদাহরণে দেখানো হয়েছে কীভাবে কোনও ব্যবহারকারীর হয়ে কার্ড সহ মেসেজ তৈরি করতে হয়
Node.js
/**
* 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
const chat = google.chat({version: 'v1'});
// 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);
Python
"""
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.
service = build('chat', 'v1', credentials=credentials)
# 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()
Java
/**
* 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());
}
}
Apps Script
/**
* 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);
}
}
অ্যাসিঙ্ক্রোনাসভাবে কার্ড আপডেট করুন
কার্ড সহ মেসেজ তৈরি করার পরে, আপনি অ্যাপ যাচাইকরণ ব্যবহার করে অ্যাসিঙ্ক্রোনাসভাবে কার্ড আপডেট করতে পারবেন। এর ফলে, ব্যবহারকারীর ইন্টার্যাকশন ছাড়াই আপনার অ্যাপ কার্ডের কন্টেন্ট রিফ্রেশ করতে পারে। ব্যবহারকারীর মেসেজে যে Chat অ্যাপ কার্ড যোগ করেছে, শুধুমাত্র সেটিই কার্ডটি পরিবর্তন করতে পারবে। ব্যবহারকারী মেসেজের টেক্সট এডিট করলে, অ্যাপের মালিকানাধীন কার্ড সরিয়ে দেওয়া হয় এবং আপনার অ্যাপ আর সেগুলি আপডেট করতে পারে না।
কার্ড আপডেট করতে, নিম্নলিখিত তথ্য সহ replaceCards মেথড কল করুন:
chat.botঅনুমোদনের স্কোপ।- আপডেট করার জন্য মেসেজের
name। - নতুন
cardsV2তালিকা। এটি মেসেজে আগে থেকে থাকা সব কার্ড পরিবর্তন করে দেয়। আপনি ফাঁকা তালিকা দিলে, কার্ড সরিয়ে দেওয়া হয়।
কোনও মেসেজের কার্ড কীভাবে আপডেট করতে হয় তা নিম্নলিখিত উদাহরণ থেকে দেখুন:
Node.js
/**
* 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
const chat = google.chat({version: 'v1'});
// 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);
Python
"""
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.
service = build('chat', 'v1', credentials=credentials)
# 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()
Java
/**
* 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.");
}
}
Apps Script
/**
* 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);
}
}
সীমাবদ্ধতা
ব্যবহারকারীর হয়ে কার্ড সহ মেসেজ তৈরি বা কার্ড আপডেট করার সময়, Chat অ্যাপকে অবশ্যই স্পেসের মেম্বার হতে হবে। এইসব ক্ষেত্রে এই প্রয়োজনীয়তা প্রযোজ্য হয়:
- ব্যবহারকারীর হয়ে কার্ড সহ মেসেজ তৈরি করা।
- কোনও মেসেজের কার্ড পরিবর্তন বা আপডেট করা।
এই প্রয়োজনীয়তা অন্যান্য API-এর থেকে আলাদা যা ব্যবহারকারীর যাচাইকরণ ব্যবহার করে, যার জন্য সাধারণত অ্যাপকে স্পেসের মেম্বার হতে হয় না।
replaceCardsপদ্ধতিটি কার্ড প্রতিস্থাপন এবং অপসারণ সমর্থন করে এবং আপনি সেগুলি প্রতিস্থাপন করার সময় অতিরিক্ত কার্ড যোগ করতে পারেন, তবে আপনি এমন একটি মেসেজে কার্ড যোগ করতে পারবেন না যাতে আগে থেকেই কার্ড নেই।Chat অ্যাপ শুধুমাত্র সেইসব কার্ডই বদলাতে পারে যেগুলি এটি কোনও মেসেজের সাথে অ্যাটাচ করেছে, অন্য Chat অ্যাপের অ্যাটাচ করা কার্ড নয়।
কোনও ব্যবহারকারী মেসেজের টেক্সট এডিট করলে, Chat অ্যাপের মালিকানাধীন কার্ড সরিয়ে দেওয়া হয় এবং আপনি আর সেগুলি আপডেট করতে পারবেন না।