@warriorteam/redai-zalo-sdk 1.21.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +109 -0
- package/dist/services/promotion.service.d.ts +62 -4
- package/dist/services/promotion.service.d.ts.map +1 -1
- package/dist/services/promotion.service.js +391 -44
- package/dist/services/promotion.service.js.map +1 -1
- package/dist/types/message.d.ts +78 -22
- package/dist/types/message.d.ts.map +1 -1
- package/docs/MULTIPLE_USERS_PROMOTION.md +210 -0
- package/docs/PROMOTION_API_V3_MIGRATION.md +320 -0
- package/examples/promotion-examples.ts +407 -0
- package/package.json +18 -3
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { PromotionService } from "../src/services/promotion.service";
|
|
2
|
+
import { ZaloClient } from "../src/clients/zalo-client";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Examples sử dụng Promotion Service với API v3.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Khởi tạo service
|
|
9
|
+
const client = new ZaloClient();
|
|
10
|
+
const promotionService = new PromotionService(client);
|
|
11
|
+
|
|
12
|
+
const ACCESS_TOKEN = "your_access_token_here";
|
|
13
|
+
const USER_ID = "4356639876691778517";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Example 1: Gửi promotion message theo format chuẩn docs v3.0
|
|
17
|
+
*/
|
|
18
|
+
export async function sendStandardPromotion() {
|
|
19
|
+
try {
|
|
20
|
+
const result = await promotionService.sendCustomPromotion(
|
|
21
|
+
ACCESS_TOKEN,
|
|
22
|
+
{ user_id: USER_ID },
|
|
23
|
+
{
|
|
24
|
+
attachmentId: "aERC3A0iYGgQxim8fYIK6fxzsXkaFfq7ZFRB3RCyZH6RyziRis3RNydebK3iSPCJX_cJ3k1nW1EQufjN_pUL1f6Ypq3rTef5nxp6H_HnXKFDiyD5y762HS-baqRpQe5FdA376lTfq1sRyPr8ypd74ecbaLyA-tGmuJ-97W",
|
|
25
|
+
headerContent: "💥💥Ưu đãi thành viên Platinum💥💥",
|
|
26
|
+
textContent: "Ưu đãi dành riêng cho khách hàng Nguyen Van A hạng thẻ Platinum<br>Voucher trị giá 150$",
|
|
27
|
+
tableData: [
|
|
28
|
+
{
|
|
29
|
+
key: "Voucher",
|
|
30
|
+
value: "VC09279222"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
key: "Hạn sử dụng",
|
|
34
|
+
value: "30/12/2023"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
footerText: "Áp dụng tất cả cửa hàng trên toàn quốc",
|
|
38
|
+
buttons: [
|
|
39
|
+
{
|
|
40
|
+
title: "Tham khảo chương trình",
|
|
41
|
+
imageIcon: "",
|
|
42
|
+
type: "oa.open.url",
|
|
43
|
+
payload: {
|
|
44
|
+
url: "https://oa.zalo.me/home"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
title: "Liên hệ chăm sóc viên",
|
|
49
|
+
imageIcon: "aeqg9SYn3nIUYYeWohGI1fYRF3V9f0GHceig8Ckq4WQVcpmWb-9SL8JLPt-6gX0QbTCfSuQv40UEst1imAm53CwFPsQ1jq9MsOnlQe6rIrZOYcrlWBTAKy_UQsV9vnfGozCuOvFfIbN5rcXddFKM4sSYVM0D50I9eWy3",
|
|
50
|
+
type: "oa.query.hide",
|
|
51
|
+
payload: "#tuvan" // String trực tiếp theo docs
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
language: "VI"
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
console.log("Promotion sent successfully:", result);
|
|
59
|
+
return result;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error("Failed to send promotion:", error);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Example 2: Gửi product promotion
|
|
68
|
+
*/
|
|
69
|
+
export async function sendProductPromotion() {
|
|
70
|
+
try {
|
|
71
|
+
const result = await promotionService.sendProductPromotion(
|
|
72
|
+
ACCESS_TOKEN,
|
|
73
|
+
{ user_id: USER_ID },
|
|
74
|
+
{
|
|
75
|
+
title: "iPhone 15 Pro Max",
|
|
76
|
+
description: "Siêu phẩm công nghệ mới nhất từ Apple với chip A17 Pro mạnh mẽ",
|
|
77
|
+
attachmentId: "your_product_image_attachment_id",
|
|
78
|
+
originalPrice: 34990000,
|
|
79
|
+
discountPrice: 29990000,
|
|
80
|
+
discountPercent: 14,
|
|
81
|
+
validUntil: "31/12/2024",
|
|
82
|
+
productUrl: "https://example.com/iphone-15-pro-max"
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
console.log("Product promotion sent successfully:", result);
|
|
87
|
+
return result;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error("Failed to send product promotion:", error);
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Example 3: Gửi event notification
|
|
96
|
+
*/
|
|
97
|
+
export async function sendEventNotification() {
|
|
98
|
+
try {
|
|
99
|
+
const result = await promotionService.sendEventNotification(
|
|
100
|
+
ACCESS_TOKEN,
|
|
101
|
+
{ user_id: USER_ID },
|
|
102
|
+
{
|
|
103
|
+
title: "Hội thảo Digital Marketing 2024",
|
|
104
|
+
description: "Khám phá xu hướng marketing số mới nhất và chiến lược tăng trưởng bền vững",
|
|
105
|
+
attachmentId: "your_event_image_attachment_id",
|
|
106
|
+
eventDate: "15/01/2024 - 9:00 AM",
|
|
107
|
+
location: "Trung tâm Hội nghị Quốc gia, Hà Nội",
|
|
108
|
+
registrationUrl: "https://example.com/register-event"
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
console.log("Event notification sent successfully:", result);
|
|
113
|
+
return result;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.error("Failed to send event notification:", error);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Example 4: Gửi newsletter
|
|
122
|
+
*/
|
|
123
|
+
export async function sendNewsletter() {
|
|
124
|
+
try {
|
|
125
|
+
const result = await promotionService.sendNewsletter(
|
|
126
|
+
ACCESS_TOKEN,
|
|
127
|
+
{ user_id: USER_ID },
|
|
128
|
+
{
|
|
129
|
+
title: "Bản tin công nghệ tuần 52/2024",
|
|
130
|
+
summary: "Cập nhật những tin tức công nghệ hot nhất trong tuần qua",
|
|
131
|
+
attachmentId: "your_newsletter_image_attachment_id",
|
|
132
|
+
articles: [
|
|
133
|
+
{
|
|
134
|
+
title: "AI và tương lai của lập trình",
|
|
135
|
+
url: "https://example.com/ai-programming-future"
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
title: "Blockchain trong fintech",
|
|
139
|
+
url: "https://example.com/blockchain-fintech"
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
title: "IoT và smart city",
|
|
143
|
+
url: "https://example.com/iot-smart-city"
|
|
144
|
+
}
|
|
145
|
+
],
|
|
146
|
+
unsubscribeUrl: "https://example.com/unsubscribe"
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
console.log("Newsletter sent successfully:", result);
|
|
151
|
+
return result;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error("Failed to send newsletter:", error);
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Example 5: Demo tất cả button types
|
|
160
|
+
*/
|
|
161
|
+
export async function sendAllButtonTypes() {
|
|
162
|
+
try {
|
|
163
|
+
const result = await promotionService.sendCustomPromotion(
|
|
164
|
+
ACCESS_TOKEN,
|
|
165
|
+
{ user_id: USER_ID },
|
|
166
|
+
{
|
|
167
|
+
attachmentId: "your_attachment_id",
|
|
168
|
+
headerContent: "🔘 Demo tất cả button types",
|
|
169
|
+
textContent: "Thử nghiệm các loại button được hỗ trợ trong Zalo API v3.0",
|
|
170
|
+
tableData: [
|
|
171
|
+
{ key: "Button types", value: "5 loại" },
|
|
172
|
+
{ key: "Tính năng", value: "Chỉ trên Mobile" }
|
|
173
|
+
],
|
|
174
|
+
buttons: [
|
|
175
|
+
{
|
|
176
|
+
title: "Mở URL",
|
|
177
|
+
type: "oa.open.url",
|
|
178
|
+
payload: { url: "https://developers.zalo.me/" }
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
title: "Query Show",
|
|
182
|
+
type: "oa.query.show",
|
|
183
|
+
payload: "#callback_show"
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
title: "Query Hide",
|
|
187
|
+
type: "oa.query.hide",
|
|
188
|
+
payload: "#callback_hide"
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
title: "Gửi SMS",
|
|
192
|
+
type: "oa.open.sms",
|
|
193
|
+
payload: {
|
|
194
|
+
content: "Xin chào từ Zalo OA",
|
|
195
|
+
phone_code: "84919018791"
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
title: "Gọi điện",
|
|
200
|
+
type: "oa.open.phone",
|
|
201
|
+
payload: { phone_code: "84919018791" }
|
|
202
|
+
}
|
|
203
|
+
],
|
|
204
|
+
language: "VI"
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
console.log("All button types demo sent successfully:", result);
|
|
209
|
+
return result;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
console.error("Failed to send all button types demo:", error);
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Example 6: Gửi promotion đến nhiều users với tracking
|
|
218
|
+
*/
|
|
219
|
+
export async function sendPromotionToMultipleUsers() {
|
|
220
|
+
try {
|
|
221
|
+
const userIds = [
|
|
222
|
+
"4356639876691778517",
|
|
223
|
+
"1234567890123456789",
|
|
224
|
+
"9876543210987654321",
|
|
225
|
+
"5555666677778888999"
|
|
226
|
+
];
|
|
227
|
+
|
|
228
|
+
console.log(`🚀 Bắt đầu gửi promotion đến ${userIds.length} users...`);
|
|
229
|
+
|
|
230
|
+
const result = await promotionService.sendCustomPromotionToMultipleUsers(
|
|
231
|
+
ACCESS_TOKEN,
|
|
232
|
+
userIds,
|
|
233
|
+
{
|
|
234
|
+
attachmentId: "your_attachment_id",
|
|
235
|
+
headerContent: "🎉 Khuyến mãi đặc biệt cho bạn!",
|
|
236
|
+
textContent: "Ưu đãi độc quyền dành riêng cho khách hàng VIP. Nhanh tay đặt hàng để nhận ngay voucher giảm giá!",
|
|
237
|
+
tableData: [
|
|
238
|
+
{ key: "Mã giảm giá", value: "SAVE50" },
|
|
239
|
+
{ key: "Giảm giá", value: "50%" },
|
|
240
|
+
{ key: "Hạn sử dụng", value: "31/12/2024" },
|
|
241
|
+
{ key: "Áp dụng", value: "Tất cả sản phẩm" }
|
|
242
|
+
],
|
|
243
|
+
footerText: "⏰ Ưu đãi có thời hạn - Đặt hàng ngay!",
|
|
244
|
+
buttons: [
|
|
245
|
+
{
|
|
246
|
+
title: "Mua ngay",
|
|
247
|
+
type: "oa.open.url",
|
|
248
|
+
payload: { url: "https://shop.example.com/sale" }
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
title: "Xem thêm ưu đãi",
|
|
252
|
+
type: "oa.query.hide",
|
|
253
|
+
payload: "#xemthem"
|
|
254
|
+
}
|
|
255
|
+
],
|
|
256
|
+
language: "VI"
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
mode: "sequential", // Gửi tuần tự để tránh rate limit
|
|
260
|
+
delayMs: 2000, // Delay 2 giây giữa các tin
|
|
261
|
+
continueOnError: true, // Tiếp tục khi có lỗi
|
|
262
|
+
|
|
263
|
+
// Callback tracking progress
|
|
264
|
+
onProgress: (progress) => {
|
|
265
|
+
const percent = Math.round((progress.completed / progress.total) * 100);
|
|
266
|
+
const eta = progress.estimatedTimeRemaining
|
|
267
|
+
? Math.round(progress.estimatedTimeRemaining / 1000)
|
|
268
|
+
: 0;
|
|
269
|
+
|
|
270
|
+
console.log(`📊 Progress: ${percent}% (${progress.completed}/${progress.total})`);
|
|
271
|
+
console.log(`✅ Thành công: ${progress.successful} | ❌ Thất bại: ${progress.failed}`);
|
|
272
|
+
|
|
273
|
+
if (progress.currentUserId) {
|
|
274
|
+
console.log(`🔄 Đang gửi đến user: ${progress.currentUserId}`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (eta > 0) {
|
|
278
|
+
console.log(`⏱️ Thời gian còn lại: ~${eta}s`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
console.log("---");
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
// Callback cho từng user hoàn thành
|
|
285
|
+
onUserComplete: (userResult) => {
|
|
286
|
+
if (userResult.success) {
|
|
287
|
+
console.log(`✅ Gửi thành công đến user ${userResult.userId}`);
|
|
288
|
+
console.log(` Message ID: ${userResult.result?.message_id}`);
|
|
289
|
+
} else {
|
|
290
|
+
console.log(`❌ Gửi thất bại đến user ${userResult.userId}`);
|
|
291
|
+
console.log(` Lỗi: ${userResult.error?.message}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Kết quả cuối cùng
|
|
298
|
+
console.log("\n🎯 KẾT QUẢ CUỐI CÙNG:");
|
|
299
|
+
console.log(`📊 Tổng số users: ${result.total}`);
|
|
300
|
+
console.log(`✅ Thành công: ${result.successful}`);
|
|
301
|
+
console.log(`❌ Thất bại: ${result.failed}`);
|
|
302
|
+
console.log(`📈 Tỷ lệ thành công: ${result.successRate.toFixed(1)}%`);
|
|
303
|
+
console.log(`⏱️ Thời gian thực hiện: ${(result.executionTime / 1000).toFixed(1)}s`);
|
|
304
|
+
console.log(`🔄 Chế độ gửi: ${result.mode}`);
|
|
305
|
+
|
|
306
|
+
// Chi tiết lỗi (nếu có)
|
|
307
|
+
const failedResults = result.results.filter(r => !r.success);
|
|
308
|
+
if (failedResults.length > 0) {
|
|
309
|
+
console.log("\n❌ CHI TIẾT LỖI:");
|
|
310
|
+
failedResults.forEach(failed => {
|
|
311
|
+
console.log(`- User ${failed.userId}: ${failed.error?.message}`);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return result;
|
|
316
|
+
} catch (error) {
|
|
317
|
+
console.error("Failed to send promotion to multiple users:", error);
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Example 7: Gửi promotion song song (parallel mode)
|
|
324
|
+
*/
|
|
325
|
+
export async function sendPromotionParallel() {
|
|
326
|
+
try {
|
|
327
|
+
const userIds = ["user1", "user2", "user3"];
|
|
328
|
+
|
|
329
|
+
const result = await promotionService.sendCustomPromotionToMultipleUsers(
|
|
330
|
+
ACCESS_TOKEN,
|
|
331
|
+
userIds,
|
|
332
|
+
{
|
|
333
|
+
attachmentId: "your_attachment_id",
|
|
334
|
+
headerContent: "⚡ Flash Sale - Chỉ 1 giờ!",
|
|
335
|
+
textContent: "Giảm giá sốc 70% cho 100 khách hàng đầu tiên!",
|
|
336
|
+
tableData: [
|
|
337
|
+
{ key: "Giảm giá", value: "70%" },
|
|
338
|
+
{ key: "Thời gian", value: "1 giờ" }
|
|
339
|
+
],
|
|
340
|
+
buttons: [
|
|
341
|
+
{
|
|
342
|
+
title: "Mua ngay",
|
|
343
|
+
type: "oa.open.url",
|
|
344
|
+
payload: { url: "https://flash-sale.example.com" }
|
|
345
|
+
}
|
|
346
|
+
]
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
mode: "parallel", // Gửi song song để tối ưu tốc độ
|
|
350
|
+
continueOnError: true,
|
|
351
|
+
onProgress: (progress) => {
|
|
352
|
+
console.log(`⚡ Parallel sending: ${progress.completed}/${progress.total} completed`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
console.log(`⚡ Parallel sending completed in ${result.executionTime}ms`);
|
|
358
|
+
return result;
|
|
359
|
+
} catch (error) {
|
|
360
|
+
console.error("Failed to send parallel promotion:", error);
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Chạy tất cả examples
|
|
367
|
+
*/
|
|
368
|
+
export async function runAllExamples() {
|
|
369
|
+
console.log("=== Zalo Promotion API v3.0 Examples ===\n");
|
|
370
|
+
|
|
371
|
+
try {
|
|
372
|
+
console.log("1. Sending standard promotion...");
|
|
373
|
+
await sendStandardPromotion();
|
|
374
|
+
console.log("✅ Standard promotion sent\n");
|
|
375
|
+
|
|
376
|
+
console.log("2. Sending product promotion...");
|
|
377
|
+
await sendProductPromotion();
|
|
378
|
+
console.log("✅ Product promotion sent\n");
|
|
379
|
+
|
|
380
|
+
console.log("3. Sending event notification...");
|
|
381
|
+
await sendEventNotification();
|
|
382
|
+
console.log("✅ Event notification sent\n");
|
|
383
|
+
|
|
384
|
+
console.log("4. Sending newsletter...");
|
|
385
|
+
await sendNewsletter();
|
|
386
|
+
console.log("✅ Newsletter sent\n");
|
|
387
|
+
|
|
388
|
+
console.log("5. Sending all button types demo...");
|
|
389
|
+
await sendAllButtonTypes();
|
|
390
|
+
console.log("✅ All button types demo sent\n");
|
|
391
|
+
|
|
392
|
+
console.log("6. Sending promotion to multiple users...");
|
|
393
|
+
await sendPromotionToMultipleUsers();
|
|
394
|
+
console.log("✅ Multiple users promotion sent\n");
|
|
395
|
+
|
|
396
|
+
console.log("7. Sending promotion in parallel mode...");
|
|
397
|
+
await sendPromotionParallel();
|
|
398
|
+
console.log("✅ Parallel promotion sent\n");
|
|
399
|
+
|
|
400
|
+
console.log("🎉 All examples completed successfully!");
|
|
401
|
+
} catch (error) {
|
|
402
|
+
console.error("❌ Error running examples:", error);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Uncomment để chạy examples
|
|
407
|
+
// runAllExamples();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@warriorteam/redai-zalo-sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account, ZNS, Consultation Service, Broadcast Service, Group Messaging with List APIs, Social APIs, Enhanced Article Management, and Message Sequence APIs with Real-time Tracking",
|
|
3
|
+
"version": "1.23.0",
|
|
4
|
+
"description": "Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account v3.0, ZNS, Consultation Service, Broadcast Service, Group Messaging with List APIs, Social APIs, Enhanced Article Management, Promotion Service v3.0 with Multiple Users Support, and Message Sequence APIs with Real-time Tracking",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -48,7 +48,22 @@
|
|
|
48
48
|
"bulk-group-messaging",
|
|
49
49
|
"group-notifications",
|
|
50
50
|
"team-communication",
|
|
51
|
-
"group-automation"
|
|
51
|
+
"group-automation",
|
|
52
|
+
"promotion-service",
|
|
53
|
+
"promotion-v3",
|
|
54
|
+
"zalo-v3",
|
|
55
|
+
"banner-element",
|
|
56
|
+
"table-element",
|
|
57
|
+
"multi-language",
|
|
58
|
+
"button-actions",
|
|
59
|
+
"multiple-users",
|
|
60
|
+
"bulk-promotion",
|
|
61
|
+
"real-time-tracking",
|
|
62
|
+
"progress-callback",
|
|
63
|
+
"sequential-sending",
|
|
64
|
+
"parallel-sending",
|
|
65
|
+
"campaign-management",
|
|
66
|
+
"error-isolation"
|
|
52
67
|
],
|
|
53
68
|
"author": "RedAI Team",
|
|
54
69
|
"license": "MIT",
|