@warriorteam/redai-zalo-sdk 1.26.1 → 1.26.2
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/dist/services/consultation.service.d.ts +50 -17
- package/dist/services/consultation.service.d.ts.map +1 -1
- package/dist/services/consultation.service.js +120 -26
- package/dist/services/consultation.service.js.map +1 -1
- package/package.json +1 -1
- package/examples/article-status-update.ts +0 -178
- package/examples/broadcast-example.ts +0 -201
- package/examples/consultation-service-example.ts +0 -390
- package/examples/get-all-articles-example.ts +0 -171
- package/examples/group-message-list.ts +0 -288
- package/examples/oa-auth-with-pkce.ts +0 -179
- package/examples/promotion-examples.ts +0 -514
- package/examples/send-message-sequence.ts +0 -200
- package/examples/user-list-post-example.ts +0 -186
- package/examples/video-upload-combined.example.ts +0 -228
- package/examples/webhook-message-classification.ts +0 -285
- package/examples/zns-template-edit.example.ts +0 -317
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
import { PromotionService } from "../src/services/promotion.service";
|
|
2
|
-
import { ZaloClient } from "../src/clients/zalo-client";
|
|
3
|
-
import { PromotionButtonInput } from "../src/types/promotion";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Examples sử dụng Promotion Service với API v3.0
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// Khởi tạo service
|
|
10
|
-
const client = new ZaloClient();
|
|
11
|
-
const promotionService = new PromotionService(client);
|
|
12
|
-
|
|
13
|
-
const ACCESS_TOKEN = "your_access_token_here";
|
|
14
|
-
const USER_ID = "4356639876691778517";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Example 1: Gửi promotion message theo format chuẩn docs v3.0
|
|
18
|
-
*/
|
|
19
|
-
export async function sendStandardPromotion() {
|
|
20
|
-
try {
|
|
21
|
-
const result = await promotionService.sendCustomPromotion(
|
|
22
|
-
ACCESS_TOKEN,
|
|
23
|
-
{ user_id: USER_ID },
|
|
24
|
-
{
|
|
25
|
-
attachmentId: "aERC3A0iYGgQxim8fYIK6fxzsXkaFfq7ZFRB3RCyZH6RyziRis3RNydebK3iSPCJX_cJ3k1nW1EQufjN_pUL1f6Ypq3rTef5nxp6H_HnXKFDiyD5y762HS-baqRpQe5FdA376lTfq1sRyPr8ypd74ecbaLyA-tGmuJ-97W",
|
|
26
|
-
headerContent: "💥💥Ưu đãi thành viên Platinum💥💥",
|
|
27
|
-
textContent: "Ưu đãi dành riêng cho khách hàng Nguyen Van A hạng thẻ Platinum<br>Voucher trị giá 150$",
|
|
28
|
-
tableData: [
|
|
29
|
-
{
|
|
30
|
-
key: "Voucher",
|
|
31
|
-
value: "VC09279222"
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
key: "Hạn sử dụng",
|
|
35
|
-
value: "30/12/2023"
|
|
36
|
-
}
|
|
37
|
-
],
|
|
38
|
-
footerText: "Áp dụng tất cả cửa hàng trên toàn quốc",
|
|
39
|
-
buttons: [
|
|
40
|
-
{
|
|
41
|
-
title: "Tham khảo chương trình",
|
|
42
|
-
imageIcon: "",
|
|
43
|
-
type: "oa.open.url" as const,
|
|
44
|
-
payload: {
|
|
45
|
-
url: "https://oa.zalo.me/home"
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
title: "Liên hệ chăm sóc viên",
|
|
50
|
-
imageIcon: "aeqg9SYn3nIUYYeWohGI1fYRF3V9f0GHceig8Ckq4WQVcpmWb-9SL8JLPt-6gX0QbTCfSuQv40UEst1imAm53CwFPsQ1jq9MsOnlQe6rIrZOYcrlWBTAKy_UQsV9vnfGozCuOvFfIbN5rcXddFKM4sSYVM0D50I9eWy3",
|
|
51
|
-
type: "oa.query.hide" as const,
|
|
52
|
-
payload: "#tuvan" // String trực tiếp theo docs
|
|
53
|
-
}
|
|
54
|
-
],
|
|
55
|
-
language: "VI"
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
console.log("Promotion sent successfully:", result);
|
|
60
|
-
return result;
|
|
61
|
-
} catch (error) {
|
|
62
|
-
console.error("Failed to send promotion:", error);
|
|
63
|
-
throw error;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Example 2: Gửi product promotion
|
|
69
|
-
*/
|
|
70
|
-
export async function sendProductPromotion() {
|
|
71
|
-
try {
|
|
72
|
-
const result = await promotionService.sendProductPromotion(
|
|
73
|
-
ACCESS_TOKEN,
|
|
74
|
-
{ user_id: USER_ID },
|
|
75
|
-
{
|
|
76
|
-
title: "iPhone 15 Pro Max",
|
|
77
|
-
description: "Siêu phẩm công nghệ mới nhất từ Apple với chip A17 Pro mạnh mẽ",
|
|
78
|
-
attachmentId: "your_product_image_attachment_id",
|
|
79
|
-
originalPrice: 34990000,
|
|
80
|
-
discountPrice: 29990000,
|
|
81
|
-
discountPercent: 14,
|
|
82
|
-
validUntil: "31/12/2024",
|
|
83
|
-
productUrl: "https://example.com/iphone-15-pro-max"
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
console.log("Product promotion sent successfully:", result);
|
|
88
|
-
return result;
|
|
89
|
-
} catch (error) {
|
|
90
|
-
console.error("Failed to send product promotion:", error);
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Example 3: Gửi event notification
|
|
97
|
-
*/
|
|
98
|
-
export async function sendEventNotification() {
|
|
99
|
-
try {
|
|
100
|
-
const result = await promotionService.sendEventNotification(
|
|
101
|
-
ACCESS_TOKEN,
|
|
102
|
-
{ user_id: USER_ID },
|
|
103
|
-
{
|
|
104
|
-
title: "Hội thảo Digital Marketing 2024",
|
|
105
|
-
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",
|
|
106
|
-
attachmentId: "your_event_image_attachment_id",
|
|
107
|
-
eventDate: "15/01/2024 - 9:00 AM",
|
|
108
|
-
location: "Trung tâm Hội nghị Quốc gia, Hà Nội",
|
|
109
|
-
registrationUrl: "https://example.com/register-event"
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
console.log("Event notification sent successfully:", result);
|
|
114
|
-
return result;
|
|
115
|
-
} catch (error) {
|
|
116
|
-
console.error("Failed to send event notification:", error);
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Example 4: Gửi newsletter
|
|
123
|
-
*/
|
|
124
|
-
export async function sendNewsletter() {
|
|
125
|
-
try {
|
|
126
|
-
const result = await promotionService.sendNewsletter(
|
|
127
|
-
ACCESS_TOKEN,
|
|
128
|
-
{ user_id: USER_ID },
|
|
129
|
-
{
|
|
130
|
-
title: "Bản tin công nghệ tuần 52/2024",
|
|
131
|
-
summary: "Cập nhật những tin tức công nghệ hot nhất trong tuần qua",
|
|
132
|
-
attachmentId: "your_newsletter_image_attachment_id",
|
|
133
|
-
articles: [
|
|
134
|
-
{
|
|
135
|
-
title: "AI và tương lai của lập trình",
|
|
136
|
-
url: "https://example.com/ai-programming-future"
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
title: "Blockchain trong fintech",
|
|
140
|
-
url: "https://example.com/blockchain-fintech"
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
title: "IoT và smart city",
|
|
144
|
-
url: "https://example.com/iot-smart-city"
|
|
145
|
-
}
|
|
146
|
-
],
|
|
147
|
-
unsubscribeUrl: "https://example.com/unsubscribe"
|
|
148
|
-
}
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
console.log("Newsletter sent successfully:", result);
|
|
152
|
-
return result;
|
|
153
|
-
} catch (error) {
|
|
154
|
-
console.error("Failed to send newsletter:", error);
|
|
155
|
-
throw error;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Example 5: Demo tất cả button types
|
|
161
|
-
*/
|
|
162
|
-
export async function sendAllButtonTypes() {
|
|
163
|
-
try {
|
|
164
|
-
const result = await promotionService.sendCustomPromotion(
|
|
165
|
-
ACCESS_TOKEN,
|
|
166
|
-
{ user_id: USER_ID },
|
|
167
|
-
{
|
|
168
|
-
attachmentId: "your_attachment_id",
|
|
169
|
-
headerContent: "🔘 Demo tất cả button types",
|
|
170
|
-
textContent: "Thử nghiệm các loại button được hỗ trợ trong Zalo API v3.0",
|
|
171
|
-
tableData: [
|
|
172
|
-
{ key: "Button types", value: "5 loại" },
|
|
173
|
-
{ key: "Tính năng", value: "Chỉ trên Mobile" }
|
|
174
|
-
],
|
|
175
|
-
buttons: [
|
|
176
|
-
{
|
|
177
|
-
title: "Mở URL",
|
|
178
|
-
type: "oa.open.url" as const,
|
|
179
|
-
payload: { url: "https://developers.zalo.me/" }
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
title: "Query Show",
|
|
183
|
-
type: "oa.query.show" as const,
|
|
184
|
-
payload: "#callback_show"
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
title: "Query Hide",
|
|
188
|
-
type: "oa.query.hide" as const,
|
|
189
|
-
payload: "#callback_hide"
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
title: "Gửi SMS",
|
|
193
|
-
type: "oa.open.sms" as const,
|
|
194
|
-
payload: {
|
|
195
|
-
content: "Xin chào từ Zalo OA",
|
|
196
|
-
phone_code: "84919018791"
|
|
197
|
-
}
|
|
198
|
-
},
|
|
199
|
-
{
|
|
200
|
-
title: "Gọi điện",
|
|
201
|
-
type: "oa.open.phone" as const,
|
|
202
|
-
payload: { phone_code: "84919018791" }
|
|
203
|
-
}
|
|
204
|
-
],
|
|
205
|
-
language: "VI"
|
|
206
|
-
}
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
console.log("All button types demo sent successfully:", result);
|
|
210
|
-
return result;
|
|
211
|
-
} catch (error) {
|
|
212
|
-
console.error("Failed to send all button types demo:", error);
|
|
213
|
-
throw error;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Example 6: Gửi promotion đến nhiều users với tracking
|
|
219
|
-
*/
|
|
220
|
-
export async function sendPromotionToMultipleUsers() {
|
|
221
|
-
try {
|
|
222
|
-
const userIds = [
|
|
223
|
-
"4356639876691778517",
|
|
224
|
-
"1234567890123456789",
|
|
225
|
-
"9876543210987654321",
|
|
226
|
-
"5555666677778888999"
|
|
227
|
-
];
|
|
228
|
-
|
|
229
|
-
console.log(`🚀 Bắt đầu gửi promotion đến ${userIds.length} users...`);
|
|
230
|
-
|
|
231
|
-
const result = await promotionService.sendCustomPromotionToMultipleUsers(
|
|
232
|
-
ACCESS_TOKEN,
|
|
233
|
-
userIds,
|
|
234
|
-
{
|
|
235
|
-
attachmentId: "your_attachment_id",
|
|
236
|
-
headerContent: "🎉 Khuyến mãi đặc biệt cho bạn!",
|
|
237
|
-
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á!",
|
|
238
|
-
tableData: [
|
|
239
|
-
{ key: "Mã giảm giá", value: "SAVE50" },
|
|
240
|
-
{ key: "Giảm giá", value: "50%" },
|
|
241
|
-
{ key: "Hạn sử dụng", value: "31/12/2024" },
|
|
242
|
-
{ key: "Áp dụng", value: "Tất cả sản phẩm" }
|
|
243
|
-
],
|
|
244
|
-
footerText: "⏰ Ưu đãi có thời hạn - Đặt hàng ngay!",
|
|
245
|
-
buttons: [
|
|
246
|
-
{
|
|
247
|
-
title: "Mua ngay",
|
|
248
|
-
type: "oa.open.url" as const,
|
|
249
|
-
payload: { url: "https://shop.example.com/sale" }
|
|
250
|
-
},
|
|
251
|
-
{
|
|
252
|
-
title: "Xem thêm ưu đãi",
|
|
253
|
-
type: "oa.query.hide" as const,
|
|
254
|
-
payload: "#xemthem"
|
|
255
|
-
}
|
|
256
|
-
],
|
|
257
|
-
language: "VI"
|
|
258
|
-
},
|
|
259
|
-
{
|
|
260
|
-
mode: "sequential", // Gửi tuần tự để tránh rate limit
|
|
261
|
-
delayMs: 2000, // Delay 2 giây giữa các tin
|
|
262
|
-
continueOnError: true, // Tiếp tục khi có lỗi
|
|
263
|
-
|
|
264
|
-
// Callback tracking progress
|
|
265
|
-
onProgress: (progress) => {
|
|
266
|
-
const percent = Math.round((progress.completed / progress.total) * 100);
|
|
267
|
-
const eta = progress.estimatedTimeRemaining
|
|
268
|
-
? Math.round(progress.estimatedTimeRemaining / 1000)
|
|
269
|
-
: 0;
|
|
270
|
-
|
|
271
|
-
console.log(`📊 Progress: ${percent}% (${progress.completed}/${progress.total})`);
|
|
272
|
-
console.log(`✅ Thành công: ${progress.successful} | ❌ Thất bại: ${progress.failed}`);
|
|
273
|
-
|
|
274
|
-
if (progress.currentUserId) {
|
|
275
|
-
console.log(`🔄 Đang gửi đến user: ${progress.currentUserId}`);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (eta > 0) {
|
|
279
|
-
console.log(`⏱️ Thời gian còn lại: ~${eta}s`);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
console.log("---");
|
|
283
|
-
},
|
|
284
|
-
|
|
285
|
-
// Callback cho từng user hoàn thành
|
|
286
|
-
onUserComplete: (userResult) => {
|
|
287
|
-
if (userResult.success) {
|
|
288
|
-
console.log(`✅ Gửi thành công đến user ${userResult.userId}`);
|
|
289
|
-
console.log(` Message ID: ${userResult.result?.message_id}`);
|
|
290
|
-
} else {
|
|
291
|
-
console.log(`❌ Gửi thất bại đến user ${userResult.userId}`);
|
|
292
|
-
console.log(` Lỗi: ${userResult.error?.message}`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
);
|
|
297
|
-
|
|
298
|
-
// Kết quả cuối cùng
|
|
299
|
-
console.log("\n🎯 KẾT QUẢ CUỐI CÙNG:");
|
|
300
|
-
console.log(`📊 Tổng số users: ${result.total}`);
|
|
301
|
-
console.log(`✅ Thành công: ${result.successful}`);
|
|
302
|
-
console.log(`❌ Thất bại: ${result.failed}`);
|
|
303
|
-
console.log(`📈 Tỷ lệ thành công: ${result.successRate.toFixed(1)}%`);
|
|
304
|
-
console.log(`⏱️ Thời gian thực hiện: ${(result.executionTime / 1000).toFixed(1)}s`);
|
|
305
|
-
console.log(`🔄 Chế độ gửi: ${result.mode}`);
|
|
306
|
-
|
|
307
|
-
// Chi tiết lỗi (nếu có)
|
|
308
|
-
const failedResults = result.results.filter(r => !r.success);
|
|
309
|
-
if (failedResults.length > 0) {
|
|
310
|
-
console.log("\n❌ CHI TIẾT LỖI:");
|
|
311
|
-
failedResults.forEach(failed => {
|
|
312
|
-
console.log(`- User ${failed.userId}: ${failed.error?.message}`);
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return result;
|
|
317
|
-
} catch (error) {
|
|
318
|
-
console.error("Failed to send promotion to multiple users:", error);
|
|
319
|
-
throw error;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Example 7: Gửi promotion song song (parallel mode)
|
|
325
|
-
*/
|
|
326
|
-
export async function sendPromotionParallel() {
|
|
327
|
-
try {
|
|
328
|
-
const userIds = ["user1", "user2", "user3"];
|
|
329
|
-
|
|
330
|
-
const result = await promotionService.sendCustomPromotionToMultipleUsers(
|
|
331
|
-
ACCESS_TOKEN,
|
|
332
|
-
userIds,
|
|
333
|
-
{
|
|
334
|
-
attachmentId: "your_attachment_id",
|
|
335
|
-
headerContent: "⚡ Flash Sale - Chỉ 1 giờ!",
|
|
336
|
-
textContent: "Giảm giá sốc 70% cho 100 khách hàng đầu tiên!",
|
|
337
|
-
tableData: [
|
|
338
|
-
{ key: "Giảm giá", value: "70%" },
|
|
339
|
-
{ key: "Thời gian", value: "1 giờ" }
|
|
340
|
-
],
|
|
341
|
-
buttons: [
|
|
342
|
-
{
|
|
343
|
-
title: "Mua ngay",
|
|
344
|
-
type: "oa.open.url" as const,
|
|
345
|
-
payload: { url: "https://flash-sale.example.com" }
|
|
346
|
-
}
|
|
347
|
-
]
|
|
348
|
-
},
|
|
349
|
-
{
|
|
350
|
-
mode: "parallel", // Gửi song song để tối ưu tốc độ
|
|
351
|
-
continueOnError: true,
|
|
352
|
-
onProgress: (progress) => {
|
|
353
|
-
console.log(`⚡ Parallel sending: ${progress.completed}/${progress.total} completed`);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
);
|
|
357
|
-
|
|
358
|
-
console.log(`⚡ Parallel sending completed in ${result.executionTime}ms`);
|
|
359
|
-
return result;
|
|
360
|
-
} catch (error) {
|
|
361
|
-
console.error("Failed to send parallel promotion:", error);
|
|
362
|
-
throw error;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Example 8: Demo proper TypeScript typing với discriminated union
|
|
368
|
-
*/
|
|
369
|
-
export async function sendProperTypedButtons() {
|
|
370
|
-
try {
|
|
371
|
-
// TypeScript sẽ enforce đúng payload type cho từng button
|
|
372
|
-
const result = await promotionService.sendCustomPromotion(
|
|
373
|
-
ACCESS_TOKEN,
|
|
374
|
-
{ user_id: USER_ID },
|
|
375
|
-
{
|
|
376
|
-
attachmentId: "your_attachment_id",
|
|
377
|
-
headerContent: "🎯 TypeScript Typed Buttons Demo",
|
|
378
|
-
textContent: "Demo các button types với proper TypeScript typing",
|
|
379
|
-
tableData: [
|
|
380
|
-
{ key: "Feature", value: "Type Safety" },
|
|
381
|
-
{ key: "Support", value: "Full IntelliSense" }
|
|
382
|
-
],
|
|
383
|
-
buttons: [
|
|
384
|
-
// URL Button - TypeScript enforces { url: string }
|
|
385
|
-
{
|
|
386
|
-
title: "Open Website",
|
|
387
|
-
type: "oa.open.url",
|
|
388
|
-
payload: { url: "https://typescript.example.com" }
|
|
389
|
-
// payload: "invalid" // ❌ TypeScript error!
|
|
390
|
-
},
|
|
391
|
-
|
|
392
|
-
// Query Hide - TypeScript enforces string payload
|
|
393
|
-
{
|
|
394
|
-
title: "Hidden Query",
|
|
395
|
-
type: "oa.query.hide",
|
|
396
|
-
payload: "#hidden_query"
|
|
397
|
-
// payload: { invalid: true } // ❌ TypeScript error!
|
|
398
|
-
},
|
|
399
|
-
|
|
400
|
-
// SMS Button - TypeScript enforces proper structure
|
|
401
|
-
{
|
|
402
|
-
title: "Send SMS",
|
|
403
|
-
type: "oa.open.sms",
|
|
404
|
-
payload: {
|
|
405
|
-
content: "Hello from typed button!",
|
|
406
|
-
phone_code: "84919018791"
|
|
407
|
-
// phoneCode: "84919018791" // ✅ Also supported (camelCase)
|
|
408
|
-
}
|
|
409
|
-
},
|
|
410
|
-
|
|
411
|
-
// Phone Button - Support both object and string
|
|
412
|
-
{
|
|
413
|
-
title: "Call Phone",
|
|
414
|
-
type: "oa.open.phone",
|
|
415
|
-
payload: { phone_code: "84919018791" }
|
|
416
|
-
// payload: "84919018791" // ✅ Also supported (string)
|
|
417
|
-
}
|
|
418
|
-
],
|
|
419
|
-
language: "VI"
|
|
420
|
-
}
|
|
421
|
-
);
|
|
422
|
-
|
|
423
|
-
console.log("✅ Properly typed buttons sent successfully:", result);
|
|
424
|
-
return result;
|
|
425
|
-
} catch (error) {
|
|
426
|
-
console.error("❌ Failed to send properly typed buttons:", error);
|
|
427
|
-
throw error;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Example 9: Demo type safety - compile errors
|
|
433
|
-
*/
|
|
434
|
-
export function demoTypeSafety() {
|
|
435
|
-
// These examples show TypeScript compile-time errors
|
|
436
|
-
|
|
437
|
-
/* ❌ This will cause TypeScript error:
|
|
438
|
-
const invalidButtons: PromotionButtonInput[] = [
|
|
439
|
-
{
|
|
440
|
-
title: "Invalid URL",
|
|
441
|
-
type: "oa.open.url",
|
|
442
|
-
payload: "just-a-string" // Error: string not assignable to { url: string }
|
|
443
|
-
},
|
|
444
|
-
{
|
|
445
|
-
title: "Invalid SMS",
|
|
446
|
-
type: "oa.open.sms",
|
|
447
|
-
payload: { url: "wrong-property" } // Error: missing content and phone_code
|
|
448
|
-
}
|
|
449
|
-
];
|
|
450
|
-
*/
|
|
451
|
-
|
|
452
|
-
// ✅ Correct typing:
|
|
453
|
-
const validButtons: PromotionButtonInput[] = [
|
|
454
|
-
{
|
|
455
|
-
title: "Valid URL",
|
|
456
|
-
type: "oa.open.url",
|
|
457
|
-
payload: { url: "https://example.com" }
|
|
458
|
-
},
|
|
459
|
-
{
|
|
460
|
-
title: "Valid SMS",
|
|
461
|
-
type: "oa.open.sms",
|
|
462
|
-
payload: {
|
|
463
|
-
content: "Valid message",
|
|
464
|
-
phone_code: "84919018791"
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
];
|
|
468
|
-
|
|
469
|
-
console.log("✅ Valid buttons with proper typing:", validButtons);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* Chạy tất cả examples
|
|
474
|
-
*/
|
|
475
|
-
export async function runAllExamples() {
|
|
476
|
-
console.log("=== Zalo Promotion API v3.0 Examples ===\n");
|
|
477
|
-
|
|
478
|
-
try {
|
|
479
|
-
console.log("1. Sending standard promotion...");
|
|
480
|
-
await sendStandardPromotion();
|
|
481
|
-
console.log("✅ Standard promotion sent\n");
|
|
482
|
-
|
|
483
|
-
console.log("2. Sending product promotion...");
|
|
484
|
-
await sendProductPromotion();
|
|
485
|
-
console.log("✅ Product promotion sent\n");
|
|
486
|
-
|
|
487
|
-
console.log("3. Sending event notification...");
|
|
488
|
-
await sendEventNotification();
|
|
489
|
-
console.log("✅ Event notification sent\n");
|
|
490
|
-
|
|
491
|
-
console.log("4. Sending newsletter...");
|
|
492
|
-
await sendNewsletter();
|
|
493
|
-
console.log("✅ Newsletter sent\n");
|
|
494
|
-
|
|
495
|
-
console.log("5. Sending all button types demo...");
|
|
496
|
-
await sendAllButtonTypes();
|
|
497
|
-
console.log("✅ All button types demo sent\n");
|
|
498
|
-
|
|
499
|
-
console.log("6. Sending promotion to multiple users...");
|
|
500
|
-
await sendPromotionToMultipleUsers();
|
|
501
|
-
console.log("✅ Multiple users promotion sent\n");
|
|
502
|
-
|
|
503
|
-
console.log("7. Sending promotion in parallel mode...");
|
|
504
|
-
await sendPromotionParallel();
|
|
505
|
-
console.log("✅ Parallel promotion sent\n");
|
|
506
|
-
|
|
507
|
-
console.log("🎉 All examples completed successfully!");
|
|
508
|
-
} catch (error) {
|
|
509
|
-
console.error("❌ Error running examples:", error);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// Uncomment để chạy examples
|
|
514
|
-
// runAllExamples();
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { ConsultationService, MessageItem, SendMessageSequenceRequest } from "../src/services/consultation.service";
|
|
2
|
-
import { ZaloClient } from "../src/clients/zalo-client";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Ví dụ sử dụng API gửi chuỗi tin nhắn tư vấn
|
|
6
|
-
*/
|
|
7
|
-
async function sendMessageSequenceExample() {
|
|
8
|
-
// Khởi tạo Zalo client và consultation service
|
|
9
|
-
const zaloClient = new ZaloClient();
|
|
10
|
-
const consultationService = new ConsultationService(zaloClient);
|
|
11
|
-
|
|
12
|
-
// Thông tin cấu hình
|
|
13
|
-
const accessToken = "YOUR_ACCESS_TOKEN";
|
|
14
|
-
const userId = "USER_ID_TO_SEND_TO";
|
|
15
|
-
|
|
16
|
-
// Định nghĩa chuỗi tin nhắn
|
|
17
|
-
const messages: MessageItem[] = [
|
|
18
|
-
// 1. Tin nhắn chào hỏi
|
|
19
|
-
{
|
|
20
|
-
type: "text",
|
|
21
|
-
text: "Xin chào! Cảm ơn bạn đã liên hệ với chúng tôi 👋",
|
|
22
|
-
delay: 2000, // Delay 2 giây sau tin nhắn này
|
|
23
|
-
},
|
|
24
|
-
|
|
25
|
-
// 2. Gửi sticker
|
|
26
|
-
{
|
|
27
|
-
type: "sticker",
|
|
28
|
-
stickerAttachmentId: "STICKER_ID",
|
|
29
|
-
delay: 1500, // Delay 1.5 giây
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
// 3. Tin nhắn giới thiệu dịch vụ
|
|
33
|
-
{
|
|
34
|
-
type: "text",
|
|
35
|
-
text: "Chúng tôi cung cấp các dịch vụ tư vấn chuyên nghiệp. Dưới đây là một số thông tin chi tiết:",
|
|
36
|
-
delay: 3000, // Delay 3 giây
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
// 4. Gửi hình ảnh minh họa
|
|
40
|
-
{
|
|
41
|
-
type: "image",
|
|
42
|
-
imageUrl: "https://example.com/service-image.jpg",
|
|
43
|
-
text: "Dịch vụ tư vấn của chúng tôi",
|
|
44
|
-
delay: 2000,
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
// 5. Gửi file tài liệu
|
|
48
|
-
{
|
|
49
|
-
type: "file",
|
|
50
|
-
fileToken: "FILE_TOKEN_FROM_UPLOAD_API",
|
|
51
|
-
delay: 1000,
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
// 6. Gửi GIF
|
|
55
|
-
{
|
|
56
|
-
type: "gif",
|
|
57
|
-
gifUrl: "https://example.com/animation.gif",
|
|
58
|
-
width: 300,
|
|
59
|
-
height: 200,
|
|
60
|
-
text: "Quy trình làm việc của chúng tôi",
|
|
61
|
-
delay: 2500,
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
// 7. Yêu cầu thông tin người dùng
|
|
65
|
-
{
|
|
66
|
-
type: "request_user_info",
|
|
67
|
-
title: "Đăng ký tư vấn miễn phí",
|
|
68
|
-
subtitle: "Vui lòng cung cấp thông tin để chúng tôi hỗ trợ bạn tốt nhất",
|
|
69
|
-
imageUrl: "https://example.com/consultation-form.jpg",
|
|
70
|
-
delay: 1000,
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
// 8. Tin nhắn kết thúc (không có delay vì là tin cuối)
|
|
74
|
-
{
|
|
75
|
-
type: "text",
|
|
76
|
-
text: "Cảm ơn bạn! Chúng tôi sẽ liên hệ lại trong thời gian sớm nhất. 🙏",
|
|
77
|
-
},
|
|
78
|
-
];
|
|
79
|
-
|
|
80
|
-
// Tạo request
|
|
81
|
-
const request: SendMessageSequenceRequest = {
|
|
82
|
-
accessToken,
|
|
83
|
-
userId,
|
|
84
|
-
messages,
|
|
85
|
-
defaultDelay: 1000, // Delay mặc định 1 giây nếu tin nhắn không có delay riêng
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
console.log("🚀 Bắt đầu gửi chuỗi tin nhắn...");
|
|
90
|
-
console.log(`📝 Tổng số tin nhắn: ${messages.length}`);
|
|
91
|
-
|
|
92
|
-
// Gửi chuỗi tin nhắn
|
|
93
|
-
const result = await consultationService.sendMessageSequence(request);
|
|
94
|
-
|
|
95
|
-
console.log("\n✅ Kết quả gửi tin nhắn:");
|
|
96
|
-
console.log(`✅ Thành công: ${result.successCount}/${messages.length}`);
|
|
97
|
-
console.log(`❌ Thất bại: ${result.failureCount}/${messages.length}`);
|
|
98
|
-
console.log(`⏱️ Tổng thời gian: ${result.totalDuration}ms`);
|
|
99
|
-
|
|
100
|
-
// In chi tiết kết quả từng tin nhắn
|
|
101
|
-
console.log("\n📊 Chi tiết từng tin nhắn:");
|
|
102
|
-
result.results.forEach((messageResult, index) => {
|
|
103
|
-
const status = messageResult.success ? "✅" : "❌";
|
|
104
|
-
const time = new Date(messageResult.timestamp).toLocaleTimeString();
|
|
105
|
-
|
|
106
|
-
console.log(`${status} [${index + 1}] ${messageResult.type} - ${time}`);
|
|
107
|
-
|
|
108
|
-
if (messageResult.success && messageResult.response) {
|
|
109
|
-
console.log(` 📧 Message ID: ${messageResult.response.message_id}`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!messageResult.success && messageResult.error) {
|
|
113
|
-
console.log(` ❌ Lỗi: ${messageResult.error}`);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
} catch (error) {
|
|
118
|
-
console.error("❌ Lỗi khi gửi chuỗi tin nhắn:", error);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Ví dụ gửi chuỗi tin nhắn đơn giản chỉ có text
|
|
124
|
-
*/
|
|
125
|
-
async function sendSimpleTextSequence() {
|
|
126
|
-
const zaloClient = new ZaloClient();
|
|
127
|
-
const consultationService = new ConsultationService(zaloClient);
|
|
128
|
-
|
|
129
|
-
const accessToken = "YOUR_ACCESS_TOKEN";
|
|
130
|
-
const userId = "USER_ID_TO_SEND_TO";
|
|
131
|
-
|
|
132
|
-
const messages: MessageItem[] = [
|
|
133
|
-
{
|
|
134
|
-
type: "text",
|
|
135
|
-
text: "Tin nhắn đầu tiên",
|
|
136
|
-
delay: 1000,
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
type: "text",
|
|
140
|
-
text: "Tin nhắn thứ hai",
|
|
141
|
-
delay: 1500,
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
type: "text",
|
|
145
|
-
text: "Tin nhắn cuối cùng",
|
|
146
|
-
},
|
|
147
|
-
];
|
|
148
|
-
|
|
149
|
-
const request: SendMessageSequenceRequest = {
|
|
150
|
-
accessToken,
|
|
151
|
-
userId,
|
|
152
|
-
messages,
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
const result = await consultationService.sendMessageSequence(request);
|
|
157
|
-
console.log("Kết quả:", result);
|
|
158
|
-
} catch (error) {
|
|
159
|
-
console.error("Lỗi:", error);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Ví dụ gửi chuỗi tin nhắn với delay mặc định
|
|
165
|
-
*/
|
|
166
|
-
async function sendSequenceWithDefaultDelay() {
|
|
167
|
-
const zaloClient = new ZaloClient();
|
|
168
|
-
const consultationService = new ConsultationService(zaloClient);
|
|
169
|
-
|
|
170
|
-
const accessToken = "YOUR_ACCESS_TOKEN";
|
|
171
|
-
const userId = "USER_ID_TO_SEND_TO";
|
|
172
|
-
|
|
173
|
-
// Các tin nhắn không có delay riêng sẽ sử dụng defaultDelay
|
|
174
|
-
const messages: MessageItem[] = [
|
|
175
|
-
{ type: "text", text: "Tin nhắn 1" },
|
|
176
|
-
{ type: "text", text: "Tin nhắn 2" },
|
|
177
|
-
{ type: "text", text: "Tin nhắn 3" },
|
|
178
|
-
];
|
|
179
|
-
|
|
180
|
-
const request: SendMessageSequenceRequest = {
|
|
181
|
-
accessToken,
|
|
182
|
-
userId,
|
|
183
|
-
messages,
|
|
184
|
-
defaultDelay: 2000, // Delay 2 giây giữa các tin nhắn
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
const result = await consultationService.sendMessageSequence(request);
|
|
189
|
-
console.log("Kết quả:", result);
|
|
190
|
-
} catch (error) {
|
|
191
|
-
console.error("Lỗi:", error);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Export các function để sử dụng
|
|
196
|
-
export {
|
|
197
|
-
sendMessageSequenceExample,
|
|
198
|
-
sendSimpleTextSequence,
|
|
199
|
-
sendSequenceWithDefaultDelay,
|
|
200
|
-
};
|