@warriorteam/redai-zalo-sdk
Version:
Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account v3.0, ZNS with Full Type Safety, Consultation Service, Broadcast Service, Group Messaging with List APIs, Social APIs, Enhanced Article Management, Promotion Service v3.0 with Multip
363 lines (333 loc) • 10 kB
text/typescript
/**
* Ví dụ sử dụng ZNS Service với type validation rõ ràng
*
* File này minh họa cách sử dụng các type cụ thể thay vì `any`
* trong ZNS Service để có type safety tốt hơn.
*/
import { ZNSService } from "../src/services/zns.service";
import { ZaloClient } from "../src/clients/zalo-client";
import {
ZNSCreateTemplateRequest,
ZNSValidationComponent,
ZNSTitleComponent,
ZNSParagraphComponent,
ZNSLogoComponent,
ZNSButtonsComponent,
ZNSPaymentComponent,
ZNSVoucherComponent,
ZNSRatingComponent,
ZNSAttachment,
} from "../src/types/zns";
// Khởi tạo service
const zaloClient = new ZaloClient("your-app-id", "your-app-secret");
const znsService = new ZNSService(zaloClient);
// ===== VÍ DỤ 1: ZNS TÙY CHỈNH (Type 1) =====
async function createCustomTemplate() {
// Định nghĩa các component với type cụ thể
const logoComponent: ZNSValidationComponent = {
LOGO: {
light: {
type: "IMAGE",
media_id: "logo-light-media-id"
},
dark: {
type: "IMAGE",
media_id: "logo-dark-media-id"
}
}
};
const titleComponent: ZNSValidationComponent = {
TITLE: {
value: "Xin chào {{customer_name}}, đơn hàng của bạn đã được xác nhận!"
}
};
const paragraphComponent: ZNSValidationComponent = {
PARAGRAPH: {
value: "Cảm ơn bạn đã đặt hàng tại {{shop_name}}. Đơn hàng {{order_id}} sẽ được giao trong {{delivery_time}}."
}
};
const buttonsComponent: ZNSValidationComponent = {
BUTTONS: {
items: [
{
type: 1, // URL
title: "Xem đơn hàng",
content: "https://shop.com/orders/{{order_id}}"
},
{
type: 2, // Phone
title: "Liên hệ",
content: "0123456789"
}
]
}
};
// Layout với type safety
const layout: ZNSValidationComponent[] = [
logoComponent,
titleComponent,
paragraphComponent,
buttonsComponent
];
// Template request với type cụ thể
const templateRequest: ZNSCreateTemplateRequest = {
template_name: "Thông báo xác nhận đơn hàng",
template_type: 1, // ZNS tùy chỉnh
tag: "1", // Transaction
layout: layout,
params: [
{
name: "customer_name",
type: "1", // Tên khách hàng
sample_value: "Nguyễn Văn A"
},
{
name: "shop_name",
type: "5", // Nhãn tùy chỉnh
sample_value: "Shop ABC"
},
{
name: "order_id",
type: "4", // Mã số
sample_value: "ORD123456"
},
{
name: "delivery_time",
type: "11", // Thời gian
sample_value: "2-3 ngày"
}
],
note: "Template thông báo xác nhận đơn hàng cho khách hàng",
tracking_id: "custom-template-001"
};
try {
const result = await znsService.createTemplate("access-token", templateRequest);
console.log("Template created successfully:", result);
} catch (error) {
console.error("Error creating template:", error);
}
}
// ===== VÍ DỤ 2: ZNS PAYMENT (Type 3) =====
async function createPaymentTemplate() {
const logoComponent: ZNSValidationComponent = {
LOGO: {
light: { type: "IMAGE", media_id: "logo-light" },
dark: { type: "IMAGE", media_id: "logo-dark" }
}
};
const titleComponent: ZNSValidationComponent = {
TITLE: {
value: "Thông báo thanh toán hóa đơn {{invoice_id}}"
}
};
const paymentComponent: ZNSValidationComponent = {
PAYMENT: {
bank_code: "970425", // VietinBank
account_name: "CÔNG TY ABC",
bank_account: "1234567890",
amount: "{{amount}}",
note: "Thanh toan hoa don {{invoice_id}}"
}
};
const layout: ZNSValidationComponent[] = [
logoComponent,
titleComponent,
paymentComponent
];
const templateRequest: ZNSCreateTemplateRequest = {
template_name: "Thông báo yêu cầu thanh toán",
template_type: 3, // ZNS yêu cầu thanh toán
tag: "1", // Transaction
layout: layout,
params: [
{
name: "invoice_id",
type: "4", // Mã số
sample_value: "INV001"
},
{
name: "amount",
type: "14", // Tiền tệ VNĐ
sample_value: "500000"
}
],
tracking_id: "payment-template-001"
};
try {
const result = await znsService.createTemplate("access-token", templateRequest);
console.log("Payment template created:", result);
} catch (error) {
console.error("Error creating payment template:", error);
}
}
// ===== VÍ DỤ 3: ZNS VOUCHER (Type 4) =====
async function createVoucherTemplate() {
const imageComponent: ZNSValidationComponent = {
IMAGES: {
items: [
{
type: "IMAGE",
media_id: "voucher-image-media-id"
}
]
}
};
const titleComponent: ZNSValidationComponent = {
TITLE: {
value: "🎉 Voucher giảm giá {{discount_percent}}% dành cho bạn!"
}
};
const voucherComponent: ZNSValidationComponent = {
VOUCHER: {
name: "Voucher giảm giá {{discount_percent}}%",
condition: "Áp dụng cho đơn hàng từ {{min_order}}đ",
end_date: "{{expiry_date}}",
voucher_code: "{{voucher_code}}",
display_code: 1 // QR code
}
};
const buttonsComponent: ZNSValidationComponent = {
BUTTONS: {
items: [
{
type: 1, // URL
title: "Sử dụng ngay",
content: "https://shop.com/voucher/{{voucher_code}}"
}
]
}
};
const layout: ZNSValidationComponent[] = [
imageComponent,
titleComponent,
voucherComponent,
buttonsComponent
];
const templateRequest: ZNSCreateTemplateRequest = {
template_name: "Thông báo voucher giảm giá",
template_type: 4, // ZNS voucher
tag: "3", // Promotion
layout: layout,
params: [
{
name: "discount_percent",
type: "10", // Số lượng/Số tiền
sample_value: "20"
},
{
name: "min_order",
type: "14", // Tiền tệ VNĐ
sample_value: "200000"
},
{
name: "expiry_date",
type: "11", // Thời gian
sample_value: "31/12/2024"
},
{
name: "voucher_code",
type: "4", // Mã số
sample_value: "SAVE20"
}
],
tracking_id: "voucher-template-001"
};
try {
const result = await znsService.createTemplate("access-token", templateRequest);
console.log("Voucher template created:", result);
} catch (error) {
console.error("Error creating voucher template:", error);
}
}
// ===== VÍ DỤ 4: ZNS RATING (Type 5) =====
async function createRatingTemplate() {
const logoComponent: ZNSValidationComponent = {
LOGO: {
light: { type: "IMAGE", media_id: "logo-light" },
dark: { type: "IMAGE", media_id: "logo-dark" }
}
};
const titleComponent: ZNSValidationComponent = {
TITLE: {
value: "Đánh giá trải nghiệm dịch vụ của {{service_name}}"
}
};
const ratingComponent: ZNSValidationComponent = {
RATING: {
items: [
{
star: 1,
title: "Rất không hài lòng",
question: "Chúng tôi có thể cải thiện điều gì?",
answers: ["Chất lượng dịch vụ", "Thái độ nhân viên", "Thời gian phục vụ"],
thanks: "Cảm ơn bạn đã góp ý!",
description: "Chúng tôi sẽ cải thiện dịch vụ tốt hơn"
},
{
star: 2,
title: "Không hài lòng",
question: "Điều gì khiến bạn chưa hài lòng?",
answers: ["Chất lượng dịch vụ", "Giá cả", "Thời gian chờ"],
thanks: "Cảm ơn phản hồi của bạn!",
description: "Chúng tôi sẽ khắc phục những vấn đề này"
},
{
star: 3,
title: "Bình thường",
question: "Bạn mong muốn điều gì tốt hơn?",
answers: ["Dịch vụ nhanh hơn", "Chất lượng tốt hơn", "Giá cả hợp lý hơn"],
thanks: "Cảm ơn bạn đã đánh giá!",
description: "Chúng tôi sẽ nỗ lực cải thiện"
},
{
star: 4,
title: "Hài lòng",
question: "Điều gì bạn thích nhất?",
answers: ["Dịch vụ tốt", "Nhân viên thân thiện", "Chất lượng ổn"],
thanks: "Cảm ơn bạn đã tin tưởng!",
description: "Chúng tôi sẽ duy trì chất lượng này"
},
{
star: 5,
title: "Rất hài lòng",
question: "Bạn có muốn giới thiệu cho bạn bè?",
answers: ["Có, chắc chắn", "Có thể", "Sẽ suy nghĩ"],
thanks: "Cảm ơn bạn rất nhiều!",
description: "Chúng tôi rất vui khi được phục vụ bạn"
}
]
}
};
const layout: ZNSValidationComponent[] = [
logoComponent,
titleComponent,
ratingComponent
];
const templateRequest: ZNSCreateTemplateRequest = {
template_name: "Đánh giá dịch vụ khách hàng",
template_type: 5, // ZNS đánh giá dịch vụ
tag: "2", // Customer care
layout: layout,
params: [
{
name: "service_name",
type: "5", // Nhãn tùy chỉnh
sample_value: "Dịch vụ giao hàng"
}
],
tracking_id: "rating-template-001"
};
try {
const result = await znsService.createTemplate("access-token", templateRequest);
console.log("Rating template created:", result);
} catch (error) {
console.error("Error creating rating template:", error);
}
}
// Export các function để sử dụng
export {
createCustomTemplate,
createPaymentTemplate,
createVoucherTemplate,
createRatingTemplate
};