@warriorteam/redai-zalo-sdk
Version:
Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account, ZNS, Consultation Service, Group Messaging, and Social APIs
318 lines (299 loc) • 7.89 kB
text/typescript
/**
* Example: Chỉnh sửa ZNS Template theo chuẩn Zalo API
*
* Ví dụ này minh họa cách sử dụng SDK để chỉnh sửa template ZNS
* theo đúng cấu trúc API của Zalo
*/
import {
ZNSService,
ZNSUpdateTemplateRequest,
ZNSTemplateType,
ZNSTemplateTag,
ZNSButtonType,
ZNSParamType,
ZNS_TEMPLATE_TYPES,
ZNS_TEMPLATE_TAGS,
ZNSValidation
} from '../src/index';
// Khởi tạo ZNS Service
const znsService = new ZNSService({
appId: 'your_app_id',
appSecret: 'your_app_secret'
});
/**
* Ví dụ 1: Chỉnh sửa template ZNS tùy chỉnh
*/
async function editCustomTemplate() {
const accessToken = 'your_access_token';
// Dữ liệu template theo đúng chuẩn Zalo API
const templateData: ZNSUpdateTemplateRequest = {
template_id: "234567",
template_name: "Xác nhận đơn hàng - Cập nhật",
template_type: ZNSTemplateType.CUSTOM,
tag: ZNSTemplateTag.TRANSACTION,
layout: {
header: {
components: [
{
LOGO: {
light: {
type: "IMAGE",
media_id: "1ashjkdbkj"
},
dark: {
type: "IMAGE",
media_id: "1ashjkdbkj"
}
}
}
]
},
body: {
components: [
{
TITLE: {
value: "Xác nhận đơn hàng"
}
},
{
PARAGRAPH: {
value: "Cảm ơn <name> đã mua hàng tại cửa hàng. Đơn hàng của bạn đã được xác nhận với chi tiết như sau:"
}
},
{
TABLE: {
rows: [
{
value: "<order_code>",
title: "Mã đơn",
row_type: 1
},
{
value: "<phone_number>",
title: "Điện thoại"
},
{
value: "<price>",
title: "Giá tiền"
},
{
value: "<status>",
title: "Trạng thái"
},
{
value: "<date>",
title: "Ngày đặt hàng"
}
]
}
}
]
},
footer: {
components: [
{
BUTTONS: {
items: [
{
content: "https://example.com/order",
type: ZNSButtonType.URL,
title: "Xem đơn hàng"
}
]
}
}
]
}
},
params: [
{
type: ZNSParamType.CUSTOMER_NAME,
name: "name",
sample_value: "Nguyễn Văn A"
},
{
type: ZNSParamType.CODE,
name: "order_code",
sample_value: "ABC123"
},
{
type: ZNSParamType.PHONE_NUMBER,
name: "phone_number",
sample_value: "0123456789"
},
{
type: ZNSParamType.CURRENCY,
name: "price",
sample_value: "100000"
},
{
type: ZNSParamType.TRANSACTION_STATUS,
name: "status",
sample_value: "CONFIRMED"
},
{
type: ZNSParamType.TIME,
name: "date",
sample_value: "19/01/2024"
}
],
note: "Cập nhật template theo yêu cầu kiểm duyệt",
tracking_id: "edit_template_001"
};
try {
// Validate trước khi gửi
if (!ZNSValidation.isValidTemplateName(templateData.template_name)) {
throw new Error('Tên template phải từ 10-60 ký tự');
}
if (!ZNSValidation.isTagCompatibleWithType(templateData.template_type, templateData.tag)) {
throw new Error('Tag không tương thích với loại template');
}
if (templateData.note && !ZNSValidation.isValidNote(templateData.note)) {
throw new Error('Ghi chú phải từ 1-400 ký tự');
}
// Gửi request chỉnh sửa template
const result = await znsService.updateTemplate(accessToken, templateData);
console.log('✅ Chỉnh sửa template thành công:', {
templateId: result.data.template_id,
templateName: result.data.template_name,
status: result.data.status,
previewUrl: result.data.preview_url
});
return result;
} catch (error) {
console.error('❌ Lỗi chỉnh sửa template:', error);
throw error;
}
}
/**
* Ví dụ 2: Chỉnh sửa template ZNS voucher
*/
async function editVoucherTemplate() {
const accessToken = 'your_access_token';
const templateData: ZNSUpdateTemplateRequest = {
template_id: "345678",
template_name: "Voucher khuyến mãi đặc biệt",
template_type: ZNSTemplateType.VOUCHER,
tag: ZNSTemplateTag.PROMOTION,
layout: {
body: {
components: [
{
TITLE: {
value: "🎉 Voucher Khuyến Mãi Đặc Biệt"
}
},
{
PARAGRAPH: {
value: "Chúc mừng <customer_name>! Bạn nhận được voucher giảm giá đặc biệt:"
}
},
{
VOUCHER: {
name: "Giảm 50%",
condition: "Cho đơn hàng trên 500K",
voucher_code: "<voucher_code>",
start_date: "<start_date>",
end_date: "<end_date>",
display_code: 1
}
}
]
},
footer: {
components: [
{
BUTTONS: {
items: [
{
content: "https://shop.example.com",
type: ZNSButtonType.URL,
title: "Mua ngay"
}
]
}
}
]
}
},
params: [
{
type: ZNSParamType.CUSTOMER_NAME,
name: "customer_name",
sample_value: "Trần Thị B"
},
{
type: ZNSParamType.CODE,
name: "voucher_code",
sample_value: "SAVE50"
},
{
type: ZNSParamType.TIME,
name: "start_date",
sample_value: "01/02/2024"
},
{
type: ZNSParamType.TIME,
name: "end_date",
sample_value: "29/02/2024"
}
],
note: "Template voucher cho chiến dịch tháng 2",
tracking_id: "voucher_template_002"
};
try {
const result = await znsService.updateTemplate(accessToken, templateData);
console.log('✅ Chỉnh sửa voucher template thành công:', result.data);
return result;
} catch (error) {
console.error('❌ Lỗi chỉnh sửa voucher template:', error);
throw error;
}
}
/**
* Ví dụ 3: Helper functions để tạo template data
*/
export const ZNSTemplateHelpers = {
/**
* Tạo component TITLE
*/
createTitleComponent: (value: string) => ({
TITLE: { value }
}),
/**
* Tạo component PARAGRAPH
*/
createParagraphComponent: (value: string) => ({
PARAGRAPH: { value }
}),
/**
* Tạo component BUTTON
*/
createButtonComponent: (buttons: Array<{ title: string; url: string }>) => ({
BUTTONS: {
items: buttons.map(btn => ({
content: btn.url,
type: ZNSButtonType.URL,
title: btn.title
}))
}
}),
/**
* Tạo param với validation
*/
createParam: (type: ZNSParamType, name: string, sampleValue: string) => {
if (!ZNSValidation.isValidParamValue(type, sampleValue)) {
throw new Error(`Sample value "${sampleValue}" vượt quá độ dài cho phép của param type ${type}`);
}
return {
type,
name,
sample_value: sampleValue
};
}
};
// Export examples
export {
editCustomTemplate,
editVoucherTemplate
};