इस गाइड में, Google Chat API का इस्तेमाल करके ऐसे मैसेज बनाने का तरीका बताया गया है जिनमें उपयोगकर्ताओं की ओर से इंटरैक्टिव कार्ड शामिल हों. साथ ही, इसमें मौजूदा मैसेज में कार्ड अटैच करने और उन कार्ड को एसिंक्रोनस तरीके से अपडेट करने का तरीका भी बताया गया है.
कार्ड बनाने और उन्हें अपडेट करने से, ये काम किए जा सकते हैं:
- किसी उपयोगकर्ता की ओर से, टास्क या बाहरी संसाधन को दिखाने वाला कार्ड पोस्ट करें.
- इंटरैक्टिव कॉन्टेक्स्ट या वर्कफ़्लो देने के लिए, किसी मौजूदा उपयोगकर्ता के मैसेज में कार्ड अटैच करें.
- उपयोगकर्ता के इंटरैक्शन का इंतज़ार किए बिना, बाहरी इवेंट के आधार पर कार्ड की स्थिति अपडेट करें. उदाहरण के लिए, "जारी है" से "पूरा हो गया" पर अपडेट करें.
- उपयोगकर्ता के मैसेज में मौजूद कार्ड के कॉन्टेंट को रीफ़्रेश करना. जैसे, लिंक की झलक.
ज़रूरी शर्तें
Node.js
- Business या Enterprise वर्शन वाला Google Workspace खाता, जिसमें Google Chat का ऐक्सेस हो.
- अपना एनवायरमेंट सेट अप करें:
- Google Cloud प्रोजेक्ट बनाएं.
- उस स्क्रीन को कॉन्फ़िगर करें जहां OAuth के लिए सहमति दी जाती है.
- अपने Chat ऐप्लिकेशन के लिए, Google Chat API चालू करें और उसे कॉन्फ़िगर करें. इसके लिए, Chat ऐप्लिकेशन का नाम, आइकॉन, और जानकारी दें.
- Node.js Google API क्लाइंट लाइब्रेरी इंस्टॉल करें.
- Google Chat API के अनुरोध में पुष्टि करने के तरीके के आधार पर, ऐक्सेस क्रेडेंशियल बनाएं:
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें. - Chat ऐप्लिकेशन के तौर पर पुष्टि करने के लिए, सेवा खाते के क्रेडेंशियल बनाएं और क्रेडेंशियल को
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें.
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
- अनुमति का स्कोप चुनें. यह इस बात पर निर्भर करता है कि आपको उपयोगकर्ता के तौर पर पुष्टि करनी है या Chat ऐप्लिकेशन के तौर पर.
Python
- Business या Enterprise वर्शन वाला Google Workspace खाता, जिसमें Google Chat का ऐक्सेस हो.
- अपना एनवायरमेंट सेट अप करें:
- Google Cloud प्रोजेक्ट बनाएं.
- उस स्क्रीन को कॉन्फ़िगर करें जहां OAuth के लिए सहमति दी जाती है.
- अपने Chat ऐप्लिकेशन के लिए, Google Chat API चालू करें और उसे कॉन्फ़िगर करें. इसके लिए, Chat ऐप्लिकेशन का नाम, आइकॉन, और जानकारी दें.
- Python Google API क्लाइंट लाइब्रेरी इंस्टॉल करें.
- Google Chat API के अनुरोध में पुष्टि करने के तरीके के आधार पर, ऐक्सेस क्रेडेंशियल बनाएं:
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें. - Chat ऐप्लिकेशन के तौर पर पुष्टि करने के लिए, सेवा खाते के क्रेडेंशियल बनाएं और क्रेडेंशियल को
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें.
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
- अनुमति का स्कोप चुनें. यह इस बात पर निर्भर करता है कि आपको उपयोगकर्ता के तौर पर पुष्टि करनी है या Chat ऐप्लिकेशन के तौर पर.
Java
- Business या Enterprise वर्शन वाला Google Workspace खाता, जिसमें Google Chat का ऐक्सेस हो.
- अपना एनवायरमेंट सेट अप करें:
- Google Cloud प्रोजेक्ट बनाएं.
- उस स्क्रीन को कॉन्फ़िगर करें जहां OAuth के लिए सहमति दी जाती है.
- अपने Chat ऐप्लिकेशन के लिए, Google Chat API चालू करें और उसे कॉन्फ़िगर करें. इसके लिए, Chat ऐप्लिकेशन का नाम, आइकॉन, और जानकारी दें.
- Java Google API Client Library इंस्टॉल करें.
- Google Chat API के अनुरोध में पुष्टि करने के तरीके के आधार पर, ऐक्सेस क्रेडेंशियल बनाएं:
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें. - Chat ऐप्लिकेशन के तौर पर पुष्टि करने के लिए, सेवा खाते के क्रेडेंशियल बनाएं और क्रेडेंशियल को
credentials.jsonनाम की JSON फ़ाइल के तौर पर सेव करें.
- Chat उपयोगकर्ता के तौर पर पुष्टि करने के लिए, OAuth क्लाइंट आईडी क्रेडेंशियल बनाएं. इसके बाद, क्रेडेंशियल को अपनी लोकल डायरेक्ट्री में
- अनुमति का स्कोप चुनें. यह इस बात पर निर्भर करता है कि आपको उपयोगकर्ता के तौर पर पुष्टि करनी है या Chat ऐप्लिकेशन के तौर पर.
Apps Script
- Business या Enterprise वर्शन वाला Google Workspace खाता, जिसमें Google Chat का ऐक्सेस हो.
- अपना एनवायरमेंट सेट अप करें:
- Google Cloud प्रोजेक्ट बनाएं.
- उस स्क्रीन को कॉन्फ़िगर करें जहां OAuth के लिए सहमति दी जाती है.
- अपने Chat ऐप्लिकेशन के लिए, Google Chat API चालू करें और उसे कॉन्फ़िगर करें. इसके लिए, Chat ऐप्लिकेशन का नाम, आइकॉन, और जानकारी दें.
- Apps Script का स्टैंडअलोन प्रोजेक्ट बनाएं और ऐडवांस चैट सेवा चालू करें.
- इस गाइड में, आपको उपयोगकर्ता या ऐप्लिकेशन की पुष्टि का इस्तेमाल करना होगा. 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);
}
}
किसी उपयोगकर्ता की ओर से, किसी मौजूदा मैसेज में कार्ड अटैच करना
किसी उपयोगकर्ता की ओर से, किसी मौजूदा मैसेज में कार्ड अटैच करने के लिए, उपयोगकर्ता की पुष्टि का इस्तेमाल करके, patch तरीके को कॉल करें.
किसी मैसेज में कार्ड अटैच करने के लिए, अपने अनुरोध में यह जानकारी शामिल करें:
chat.messagesके लिए अनुमति का दायरा.- अपडेट किए जाने वाले मैसेज का
name,spaces/{space}/messages/{message}फ़ॉर्मैट में. updateMaskकोcards_v2पर सेट करें. उपयोगकर्ता की पुष्टि करने की सुविधा के साथ मैसेज अपडेट करते समय, अपडेट मास्क में सिर्फ़cards_v2फ़ील्ड होना चाहिए. एक ही अनुरोध में, मैसेजtextऔरcards_v2को अपडेट नहीं किया जा सकता.Messageसंसाधन में मौजूदcardsV2फ़ील्ड में कार्ड का डेटा होता है.- हर कार्ड के लिए
cardId. इसकी ज़रूरत, बाद में एसिंक्रोनस अपडेट के लिए होती है.
यहां दिए गए उदाहरण में, किसी उपयोगकर्ता की ओर से मौजूदा मैसेज में कार्ड अटैच करने का तरीका बताया गया है:
Node.js
/**
* This sample shows how to attach a card to an existing message on behalf of a user.
*/
const {google} = require('googleapis');
const {auth} = require('google-auth-library');
async function main() {
// Create a client with user credentials
const authClient = await auth.getClient({
scopes: ['https://www.googleapis.com/auth/chat.messages']
});
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,
updateMask: 'cards_v2',
requestBody: {
cardsV2: [{
cardId: 'unique-card-id',
card: {
header: {
title: 'Card Title',
subtitle: 'Card Subtitle'
},
sections: [{
widgets: [{
textParagraph: {
text: 'This card was attached to an existing user message.'
}
}]
}]
}
}]
}
};
// Call the API
const response = await chat.spaces.messages.patch(request);
// Handle the response
console.log(response.data);
}
main().catch(console.error);
Python
"""
This sample shows how to attach a card to an existing message on behalf of a user.
"""
from googleapiclient.discovery import build
import google.auth
def attach_card_to_message():
# Create a client with user credentials
scopes = ["https://www.googleapis.com/auth/chat.messages"]
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().patch(
name=message_name,
updateMask="cards_v2",
body={
'cardsV2': [{
'cardId': 'unique-card-id',
'card': {
'header': {
'title': 'Card Title',
'subtitle': 'Card Subtitle'
},
'sections': [{
'widgets': [{
'textParagraph': {
'text': 'This card was attached to an existing user message.'
}
}]
}]
}
}]
}
).execute()
print(result)
if __name__ == "__main__":
attach_card_to_message()
Java
/**
* This sample shows how to attach a card to an existing message 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 AttachCardToMessage {
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"));
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 + "?updateMask=cards_v2");
// Construct the card body
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 was attached to an existing 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);
Map<String, Object> message = new HashMap<>();
message.put("cardsV2", Collections.singletonList(cardWithId));
HttpRequest request = requestFactory.buildPatchRequest(url, new JsonHttpContent(jsonFactory, message));
System.out.println(request.execute().parseAsString());
}
}
Apps Script
/**
* This sample shows how to attach a card to an existing message on behalf of a user.
*/
function attachCardToMessage() {
const messageName = 'spaces/SPACE_NAME/messages/MESSAGE_ID';
const url = `https://chat.googleapis.com/v1/${messageName}?updateMask=cards_v2`;
const message = {
cardsV2: [{
cardId: 'unique-card-id',
card: {
header: {
title: 'Card Title',
subtitle: 'Card Subtitle'
},
sections: [{
widgets: [{
textParagraph: {
text: 'This card was attached to an existing user message.'
}
}]
}]
}
}]
};
const options = {
method: 'patch',
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 attach card: ' + err.message);
}
}
कार्ड की जानकारी को एसिंक्रोनस तरीके से अपडेट करना
कार्ड वाला मैसेज बनाने या किसी मौजूदा मैसेज में कार्ड अटैच करने के बाद, ऐप्लिकेशन की पुष्टि का इस्तेमाल करके, कार्ड को एसिंक्रोनस तरीके से अपडेट किया जा सकता है. इससे आपका ऐप्लिकेशन, उपयोगकर्ता के इंटरैक्शन के बिना कार्ड के कॉन्टेंट को रीफ़्रेश कर सकता है. कार्ड को सिर्फ़ वह Chat ऐप्लिकेशन बदल सकता है जिसने उसे उपयोगकर्ता के मैसेज में जोड़ा था. अगर कोई व्यक्ति मैसेज के टेक्स्ट में बदलाव करता है, तो ऐप्लिकेशन के मालिकाना हक वाले कार्ड हटा दिए जाते हैं. इसके बाद, आपका ऐप्लिकेशन उन्हें अपडेट नहीं कर सकता.
कार्ड को एसिंक्रोनस तरीके से अपडेट किए जाने पर, Google Chat, कार्ड के एट्रिब्यूशन के बगल में बदला गया इंडिकेटर दिखाता है. इससे लोगों को पता चलता है कि कार्ड का कॉन्टेंट, मैसेज से अलग तौर पर रीफ़्रेश किया गया था.
कार्ड अपडेट करने के लिए, replaceCards तरीके का इस्तेमाल करें. इसके लिए, यह तरीका अपनाएं:
chat.botके लिए अनुमति का दायरा.- अपडेट किए जाने वाले मैसेज का
name. - नई
cardsV2सूची. इससे मैसेज में मौजूद सभी मौजूदा कार्ड बदल जाते हैं. खाली सूची देने पर, कार्ड हटा दिए जाते हैं. सभी कार्ड हटाने पर, उन्हें वापस जोड़ने के लिएreplaceCardsका इस्तेमाल नहीं किया जा सकता. इसके बजाय, नए कार्ड जोड़ने के लिए, उपयोगकर्ता की पुष्टि करने के साथUpdateMessageका इस्तेमाल करें.
यहां दिए गए उदाहरण में, किसी मैसेज के कार्ड अपडेट करने का तरीका बताया गया है:
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 ऐप्लिकेशन का स्पेस का सदस्य होना ज़रूरी है. यह ज़रूरी शर्त तब लागू होती है, जब:
- किसी उपयोगकर्ता की ओर से कार्ड वाला मैसेज बनाना.
- उपयोगकर्ता की ओर से, किसी मौजूदा मैसेज में कार्ड अटैच करना.
- मैसेज के कार्ड बदलना या अपडेट करना.
यह ज़रूरी शर्त, उपयोगकर्ता की पुष्टि करने वाले अन्य एपीआई से अलग है. आम तौर पर, इनके लिए ऐप्लिकेशन का स्पेस का सदस्य होना ज़रूरी नहीं होता.
उपयोगकर्ता की पुष्टि करने वाले मैसेज को अपडेट करते समय, अगर
updateMaskमेंcards_v2को शामिल किया गया है, तो उसी अनुरोध में अन्य फ़ील्ड (जैसे किtext) को अपडेट नहीं किया जा सकता.replaceCardsतरीके से कार्ड बदले और हटाए जा सकते हैं. साथ ही, कार्ड बदलते समय अन्य कार्ड जोड़े जा सकते हैं. हालांकि, ऐसे मैसेज में कार्ड नहीं जोड़े जा सकते जिनमें पहले से कार्ड मौजूद नहीं हैं. अगर किसी मैसेज में कार्ड नहीं हैं, तो उनमें कार्ड अटैच करने के लिए, उपयोगकर्ता की पुष्टि के साथUpdateMessageका इस्तेमाल करें.कार्ड का एट्रिब्यूशन और मालिकाना हक:
- किसी उपयोगकर्ता के मैसेज से जुड़े कार्ड, उस Chat ऐप्लिकेशन के होते हैं जिसने उन्हें जोड़ा है. साथ ही, उनका मालिकाना हक भी उसी के पास होता है. इसकी जानकारी, सिर्फ़ आउटपुट के लिए उपलब्ध
CardWithId.ownerफ़ील्ड में दी जाती है. - Chat ऐप्लिकेशन सिर्फ़ उन कार्ड को बदल सकता है जिन्हें उसने किसी मैसेज में अटैच किया है. यह उन कार्ड को नहीं बदल सकता जिन्हें दूसरे Chat ऐप्लिकेशन ने अटैच किया है.
- किसी उपयोगकर्ता के मैसेज से जुड़े कार्ड, उस Chat ऐप्लिकेशन के होते हैं जिसने उन्हें जोड़ा है. साथ ही, उनका मालिकाना हक भी उसी के पास होता है. इसकी जानकारी, सिर्फ़ आउटपुट के लिए उपलब्ध
अगर कोई उपयोगकर्ता मैसेज के टेक्स्ट में बदलाव करता है, तो Chat ऐप्लिकेशन के कार्ड हटा दिए जाते हैं. इसके बाद, उन्हें अपडेट नहीं किया जा सकता.