يوضّح هذا الدليل كيفية استخدام Google Chat API لإنشاء رسائل تتضمّن بطاقات تفاعلية نيابةً عن المستخدمين، وكيفية تعديل هذه البطاقات بشكل غير متزامن.
يكون إنشاء البطاقات وتعديلها مفيدًا عندما تريد إجراء ما يلي:
- نشر بطاقة تمثّل مهمة أو مرجعًا خارجيًا نيابةً عن مستخدم
- تعديل حالة البطاقة (مثلاً، من "قيد التقدّم" إلى "مكتملة") استنادًا إلى أحداث خارجية، بدون انتظار تفاعل المستخدم
- تعديل محتوى بطاقة في رسالة مستخدم، مثل معاينة رابط
خارج برنامج الاستخدام المبكر للمطورين، لا يمكن أن تحتوي الرسائل التي تم إنشاؤها باستخدام مصادقة المستخدم إلا على نص.
المتطلبات الأساسية
Node.js
- حساب Google Workspace من إصدار Business أو Enterprise مع إذن الوصول إلى Google Chat
- إعداد البيئة:
- أنشئ مشروعًا على Google Cloud.
- ضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth
- فعِّل واجهة Google Chat API وأعِدّها من خلال إضافة اسم ورمز ووصف لتطبيق محادثات.
- ثبِّت مكتبة برامج Google API لنظام Node.js.
- أنشئ بيانات اعتماد الوصول استنادًا إلى الطريقة التي تريد استخدامها للمصادقة في طلب Google Chat API:
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
credentials.jsonفي الدليل المحلي. - لإجراء المصادقة بصفتك تطبيق محادثات،
أنشئ بيانات اعتماد
لحساب الخدمة واحفظها كملف JSON باسم
credentials.json.
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
- اختَر نطاق تفويض استنادًا إلى ما إذا كنت تريد المصادقة كمستخدم أو كتطبيق محادثات.
Python
- حساب Google Workspace من إصدار Business أو Enterprise مع إذن الوصول إلى Google Chat
- إعداد البيئة:
- أنشئ مشروعًا على Google Cloud.
- ضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth
- فعِّل واجهة Google Chat API وأعِدّها من خلال إضافة اسم ورمز ووصف لتطبيق محادثات.
- ثبِّت مكتبة برامج Google API للغة Python.
- أنشئ بيانات اعتماد الوصول استنادًا إلى الطريقة التي تريد استخدامها للمصادقة في طلب Google Chat API:
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
credentials.jsonفي الدليل المحلي. - لإجراء المصادقة بصفتك تطبيق محادثات،
أنشئ بيانات اعتماد
لحساب الخدمة واحفظها كملف JSON باسم
credentials.json.
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
- اختَر نطاق تفويض استنادًا إلى ما إذا كنت تريد المصادقة كمستخدم أو كتطبيق محادثات.
جافا
- حساب Google Workspace من إصدار Business أو Enterprise مع إذن الوصول إلى Google Chat
- إعداد البيئة:
- أنشئ مشروعًا على Google Cloud.
- ضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth
- فعِّل واجهة Google Chat API وأعِدّها من خلال إضافة اسم ورمز ووصف لتطبيق محادثات.
- ثبِّت مكتبة برامج Google API للغة Java.
- أنشئ بيانات اعتماد الوصول استنادًا إلى الطريقة التي تريد استخدامها للمصادقة في طلب Google Chat API:
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
credentials.jsonفي الدليل المحلي. - لإجراء المصادقة بصفتك تطبيق محادثات،
أنشئ بيانات اعتماد
لحساب الخدمة واحفظها كملف JSON باسم
credentials.json.
- لإجراء المصادقة كمستخدم في Chat،
أنشئ بيانات اعتماد معرّف عميل OAuthواحفظها كملف JSON باسم
- اختَر نطاق تفويض استنادًا إلى ما إذا كنت تريد المصادقة كمستخدم أو كتطبيق محادثات.
برمجة التطبيقات
- حساب Google Workspace من إصدار Business أو Enterprise مع إذن الوصول إلى Google Chat
- إعداد البيئة:
- أنشئ مشروعًا على Google Cloud.
- ضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth
- فعِّل واجهة Google Chat API وأعِدّها من خلال إضافة اسم ورمز ووصف لتطبيق محادثات.
- أنشئ مشروعًا مستقلاً في "برمجة تطبيقات Google"، وفعِّل خدمة Chat المتقدّمة.
- في هذا الدليل، يجب استخدام مصادقة المستخدم أو التطبيق. للمصادقة كتطبيق محادثات، أنشئ بيانات اعتماد حساب خدمة. للاطّلاع على الخطوات، يُرجى قراءة مقالة المصادقة والتفويض كتطبيق Google Chat.
- اختَر نطاق تفويض استنادًا إلى ما إذا كنت تريد المصادقة كمستخدم أو كتطبيق محادثات.
إنشاء مفتاح واجهة برمجة تطبيقات للوصول إلى ميزات معاينة المطور
لاستدعاء إحدى طرق واجهة برمجة التطبيقات في "معاينة المطوّر"، يجب استخدام إصدار غير علني من مستند اكتشاف واجهة برمجة التطبيقات في "معاينة المطوّر". للمصادقة على الطلب، يجب إدخال مفتاح واجهة برمجة التطبيقات.
لإنشاء مفتاح واجهة برمجة التطبيقات، افتح مشروع تطبيقك على Google Cloud واتّبِع الخطوات التالية:
- في Google Cloud Console، انتقِل إلى "القائمة" > واجهات برمجة التطبيقات والخدمات > بيانات الاعتماد.
- انقر على إنشاء بيانات اعتماد > مفتاح واجهة برمجة التطبيقات.
- يظهر مفتاح واجهة برمجة التطبيقات الجديد.
- انقر على "نسخ" لنسخ مفتاح واجهة برمجة التطبيقات واستخدامه في رمز تطبيقك. يمكن العثور على مفتاح واجهة برمجة التطبيقات أيضًا في قسم "مفاتيح واجهة برمجة التطبيقات" ضمن بيانات اعتماد مشروعك.
- لمنع الاستخدام غير المصرّح به، ننصحك بفرض قيود على الأماكن التي يمكن فيها استخدام مفتاح واجهة برمجة التطبيقات وعلى واجهات برمجة التطبيقات التي يمكن استخدامه معها. لمزيد من التفاصيل، يُرجى الاطّلاع على إضافة قيود على واجهة برمجة التطبيقات.
إنشاء رسالة بطاقة نيابةً عن مستخدم
لإنشاء رسالة تتضمّن بطاقات بالنيابة عن مستخدم، استخدِم مصادقة المستخدم.
لإنشاء الرسالة، حدِّد ما يلي في طلبك:
- نطاق الإذن
chat.messages.createأوchat.messages - حقل
cardsV2في مرجعMessageالذي يحتوي على بيانات البطاقة 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 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);
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 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الجديدة يؤدي هذا الإجراء إلى استبدال جميع البطاقات الحالية في الرسالة. إذا قدّمت قائمة فارغة، ستتم إزالة البطاقات.
يوضّح المثال التالي كيفية تعديل بطاقات الرسالة:
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 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);
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 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);
}
}
القيود
عند إنشاء رسائل تتضمّن بطاقات نيابةً عن مستخدم أو تعديل البطاقات، يجب أن يكون تطبيق محادثات عضوًا في المساحة. ينطبق هذا الشرط في الحالات التالية:
- إنشاء رسالة تتضمّن بطاقات بالنيابة عن مستخدم
- استبدال بطاقات الرسالة أو تعديلها
يختلف هذا الشرط عن واجهات برمجة التطبيقات الأخرى التي تستخدم مصادقة المستخدم، والتي لا تتطلّب عادةً أن يكون التطبيق عضوًا في مساحة العمل.
تتيح طريقة
replaceCardsاستبدال البطاقات وإزالتها، ويمكنك إضافة بطاقات إضافية أثناء استبدالها، ولكن لا يمكنك إضافة بطاقات إلى رسالة لا تتضمّن بطاقات.يمكن لتطبيق محادثات استبدال البطاقات التي أرفقها برسالة فقط، وليس البطاقات التي أرفقتها تطبيقات محادثات أخرى.
إذا عدّل المستخدم نص الرسالة، ستتم إزالة البطاقات التي يملكها تطبيق Chat، ولن تتمكّن من تعديلها بعد ذلك.