@warriorteam/redai-zalo-sdk 1.22.0 → 1.24.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.
@@ -39,7 +39,7 @@ export async function sendStandardPromotion() {
39
39
  {
40
40
  title: "Tham khảo chương trình",
41
41
  imageIcon: "",
42
- type: "oa.open.url",
42
+ type: "oa.open.url" as const,
43
43
  payload: {
44
44
  url: "https://oa.zalo.me/home"
45
45
  }
@@ -47,7 +47,7 @@ export async function sendStandardPromotion() {
47
47
  {
48
48
  title: "Liên hệ chăm sóc viên",
49
49
  imageIcon: "aeqg9SYn3nIUYYeWohGI1fYRF3V9f0GHceig8Ckq4WQVcpmWb-9SL8JLPt-6gX0QbTCfSuQv40UEst1imAm53CwFPsQ1jq9MsOnlQe6rIrZOYcrlWBTAKy_UQsV9vnfGozCuOvFfIbN5rcXddFKM4sSYVM0D50I9eWy3",
50
- type: "oa.query.hide",
50
+ type: "oa.query.hide" as const,
51
51
  payload: "#tuvan" // String trực tiếp theo docs
52
52
  }
53
53
  ],
@@ -174,22 +174,22 @@ export async function sendAllButtonTypes() {
174
174
  buttons: [
175
175
  {
176
176
  title: "Mở URL",
177
- type: "oa.open.url",
177
+ type: "oa.open.url" as const,
178
178
  payload: { url: "https://developers.zalo.me/" }
179
179
  },
180
180
  {
181
181
  title: "Query Show",
182
- type: "oa.query.show",
182
+ type: "oa.query.show" as const,
183
183
  payload: "#callback_show"
184
184
  },
185
185
  {
186
186
  title: "Query Hide",
187
- type: "oa.query.hide",
187
+ type: "oa.query.hide" as const,
188
188
  payload: "#callback_hide"
189
189
  },
190
190
  {
191
191
  title: "Gửi SMS",
192
- type: "oa.open.sms",
192
+ type: "oa.open.sms" as const,
193
193
  payload: {
194
194
  content: "Xin chào từ Zalo OA",
195
195
  phone_code: "84919018791"
@@ -197,7 +197,7 @@ export async function sendAllButtonTypes() {
197
197
  },
198
198
  {
199
199
  title: "Gọi điện",
200
- type: "oa.open.phone",
200
+ type: "oa.open.phone" as const,
201
201
  payload: { phone_code: "84919018791" }
202
202
  }
203
203
  ],
@@ -213,6 +213,155 @@ export async function sendAllButtonTypes() {
213
213
  }
214
214
  }
215
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" as const,
248
+ payload: { url: "https://shop.example.com/sale" }
249
+ },
250
+ {
251
+ title: "Xem thêm ưu đãi",
252
+ type: "oa.query.hide" as const,
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" as const,
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
+
216
365
  /**
217
366
  * Chạy tất cả examples
218
367
  */
@@ -240,6 +389,14 @@ export async function runAllExamples() {
240
389
  await sendAllButtonTypes();
241
390
  console.log("✅ All button types demo sent\n");
242
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
+
243
400
  console.log("🎉 All examples completed successfully!");
244
401
  } catch (error) {
245
402
  console.error("❌ Error running examples:", error);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@warriorteam/redai-zalo-sdk",
3
- "version": "1.22.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, and Message Sequence APIs with Real-time Tracking",
3
+ "version": "1.24.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, Complete Button Types System, and Message Sequence APIs with Real-time Tracking",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
@@ -55,7 +55,24 @@
55
55
  "banner-element",
56
56
  "table-element",
57
57
  "multi-language",
58
- "button-actions"
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",
67
+ "button-types",
68
+ "typescript-support",
69
+ "type-safety",
70
+ "auto-conversion",
71
+ "payload-validation",
72
+ "query-buttons",
73
+ "url-buttons",
74
+ "sms-buttons",
75
+ "phone-buttons"
59
76
  ],
60
77
  "author": "RedAI Team",
61
78
  "license": "MIT",