@warriorteam/redai-zalo-sdk 1.29.0 → 1.30.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.
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Types cho Consultation Service - Zalo OA API
3
+ *
4
+ * Consultation messages (tin nhắn tư vấn) là loại tin nhắn đặc biệt
5
+ * cho phép OA gửi tin nhắn chủ động đến người dùng trong khung thời gian nhất định
6
+ *
7
+ * API Endpoint: https://openapi.zalo.me/v3.0/oa/message/cs
8
+ * Method: POST
9
+ * Content-Type: application/json
10
+ */
11
+ /**
12
+ * Interface cho template action (default_action trong elements)
13
+ */
14
+ export interface TemplateAction {
15
+ /** Loại action */
16
+ type: "oa.open.url" | "oa.query.show" | "oa.query.hide" | "oa.open.sms" | "oa.open.phone";
17
+ /** URL (cho oa.open.url) */
18
+ url?: string;
19
+ /** Payload string (cho oa.query.show, oa.query.hide) hoặc object (cho oa.open.sms, oa.open.phone) */
20
+ payload?: string | {
21
+ content?: string;
22
+ phone_code?: string;
23
+ } | {
24
+ phone_code?: string;
25
+ };
26
+ }
27
+ /**
28
+ * Interface cho template element
29
+ *
30
+ * Quy tắc elements:
31
+ * - elements là mảng JSON, tối đa 5 phần tử
32
+ * - title: bắt buộc, ≤ 100 ký tự
33
+ * - subtitle: bắt buộc cho element đầu tiên, tùy chọn cho các element sau, ≤ 500 ký tự
34
+ * - image_url: URL ảnh (tùy chọn)
35
+ * - default_action: hành động khi click vào element (tùy chọn)
36
+ */
37
+ export interface TemplateElement {
38
+ /** Tiêu đề (bắt buộc, ≤ 100 ký tự) */
39
+ title: string;
40
+ /** Tiêu đề phụ (bắt buộc cho element đầu tiên, tùy chọn cho các element sau, ≤ 500 ký tự) */
41
+ subtitle?: string;
42
+ /** URL ảnh (tùy chọn) */
43
+ image_url?: string;
44
+ /** Hành động khi click vào element (tùy chọn) */
45
+ default_action?: TemplateAction;
46
+ }
47
+ /**
48
+ * Interface cho template button
49
+ *
50
+ * Quy tắc buttons:
51
+ * - buttons là mảng JSON, tối đa 5 phần tử
52
+ * - title: bắt buộc, ≤ 100 ký tự
53
+ * - type: loại action
54
+ * - payload: dữ liệu của action, phụ thuộc vào loại type
55
+ */
56
+ export interface TemplateButton {
57
+ /** Tiêu đề button (bắt buộc, ≤ 100 ký tự) */
58
+ title: string;
59
+ /** Loại button */
60
+ type: "oa.open.url" | "oa.query.show" | "oa.query.hide" | "oa.open.sms" | "oa.open.phone";
61
+ /** Payload của button, phụ thuộc vào type */
62
+ payload: string | {
63
+ url?: string;
64
+ } | {
65
+ content?: string;
66
+ phone_code?: string;
67
+ } | {
68
+ phone_code?: string;
69
+ };
70
+ }
71
+ /**
72
+ * Interface cho từng tin nhắn trong chuỗi tin nhắn
73
+ */
74
+ export interface MessageItem {
75
+ /** Loại tin nhắn */
76
+ type: "text" | "image" | "gif" | "file" | "sticker" | "request_user_info";
77
+ /** Nội dung tin nhắn (cho text message) */
78
+ text?: string;
79
+ /** URL hình ảnh (cho image/gif message) */
80
+ imageUrl?: string;
81
+ /** URL GIF (cho gif message) */
82
+ gifUrl?: string;
83
+ /** Chiều rộng GIF (bắt buộc cho gif) */
84
+ width?: number;
85
+ /** Chiều cao GIF (bắt buộc cho gif) */
86
+ height?: number;
87
+ /** Token file (cho file message) */
88
+ fileToken?: string;
89
+ /** Attachment ID (cho image/sticker message) */
90
+ attachmentId?: string;
91
+ /** Sticker attachment ID (cho sticker message) */
92
+ stickerAttachmentId?: string;
93
+ /** Tiêu đề (cho request_user_info message) */
94
+ title?: string;
95
+ /** Tiêu đề phụ (cho request_user_info message) */
96
+ subtitle?: string;
97
+ /** Delay sau khi gửi tin nhắn này (milliseconds) */
98
+ delay?: number;
99
+ }
100
+ /**
101
+ * Interface cho request gửi chuỗi tin nhắn
102
+ */
103
+ export interface SendMessageSequenceRequest {
104
+ /** Access token của Official Account */
105
+ accessToken: string;
106
+ /** ID người nhận */
107
+ userId: string;
108
+ /** Danh sách tin nhắn */
109
+ messages: MessageItem[];
110
+ /** Delay mặc định giữa các tin nhắn (milliseconds) - nếu không có delay riêng */
111
+ defaultDelay?: number;
112
+ }
113
+ /**
114
+ * Interface cho response gửi chuỗi tin nhắn
115
+ */
116
+ export interface SendMessageSequenceResponse {
117
+ /** Tổng số tin nhắn đã gửi thành công */
118
+ successCount: number;
119
+ /** Tổng số tin nhắn thất bại */
120
+ failureCount: number;
121
+ /** Chi tiết kết quả từng tin nhắn */
122
+ results: Array<{
123
+ /** Index của tin nhắn trong danh sách */
124
+ index: number;
125
+ /** Loại tin nhắn */
126
+ type: string;
127
+ /** Trạng thái gửi */
128
+ success: boolean;
129
+ /** Thông tin response nếu thành công */
130
+ response?: any;
131
+ /** Thông tin lỗi nếu thất bại */
132
+ error?: string;
133
+ /** Thời gian gửi */
134
+ timestamp: number;
135
+ }>;
136
+ /** Tổng thời gian thực hiện (milliseconds) */
137
+ totalDuration: number;
138
+ }
139
+ /**
140
+ * Interface cho request gửi chuỗi tin nhắn tới nhiều users
141
+ */
142
+ export interface SendMessageSequenceToMultipleUsersRequest {
143
+ /** Access token của Official Account */
144
+ accessToken: string;
145
+ /** Danh sách user IDs */
146
+ userIds: string[];
147
+ /** Danh sách tin nhắn */
148
+ messages: MessageItem[];
149
+ /** Delay mặc định giữa các tin nhắn (milliseconds) */
150
+ defaultDelay?: number;
151
+ /** Delay giữa các user (milliseconds) để tránh rate limit */
152
+ delayBetweenUsers?: number;
153
+ /** Callback function để tracking tiến trình */
154
+ onProgress?: (progress: UserProgressInfo) => void;
155
+ }
156
+ /**
157
+ * Interface cho thông tin tiến trình từng user
158
+ */
159
+ export interface UserProgressInfo {
160
+ /** User ID hiện tại */
161
+ userId: string;
162
+ /** Index của user trong danh sách (bắt đầu từ 0) */
163
+ userIndex: number;
164
+ /** Tổng số users */
165
+ totalUsers: number;
166
+ /** Trạng thái: 'started' | 'completed' | 'failed' */
167
+ status: 'started' | 'completed' | 'failed';
168
+ /** Kết quả gửi tin nhắn (nếu đã hoàn thành) */
169
+ result?: SendMessageSequenceResponse;
170
+ /** Thông tin lỗi (nếu thất bại) */
171
+ error?: string;
172
+ /** Thời gian bắt đầu */
173
+ startTime: number;
174
+ /** Thời gian kết thúc (nếu đã hoàn thành) */
175
+ endTime?: number;
176
+ }
177
+ /**
178
+ * Interface cho response gửi chuỗi tin nhắn tới nhiều users
179
+ */
180
+ export interface SendMessageSequenceToMultipleUsersResponse {
181
+ /** Tổng số users */
182
+ totalUsers: number;
183
+ /** Số users gửi thành công */
184
+ successfulUsers: number;
185
+ /** Số users gửi thất bại */
186
+ failedUsers: number;
187
+ /** Chi tiết kết quả từng user */
188
+ userResults: Array<{
189
+ /** User ID */
190
+ userId: string;
191
+ /** Index của user trong danh sách */
192
+ userIndex: number;
193
+ /** Trạng thái gửi cho user này */
194
+ success: boolean;
195
+ /** Kết quả chi tiết gửi tin nhắn */
196
+ messageSequenceResult?: SendMessageSequenceResponse;
197
+ /** Thông tin lỗi nếu thất bại */
198
+ error?: string;
199
+ /** Thời gian bắt đầu gửi */
200
+ startTime: number;
201
+ /** Thời gian kết thúc */
202
+ endTime: number;
203
+ /** Thời gian thực hiện (milliseconds) */
204
+ duration: number;
205
+ }>;
206
+ /** Tổng thời gian thực hiện (milliseconds) */
207
+ totalDuration: number;
208
+ /** Thống kê tin nhắn tổng cộng */
209
+ messageStats: {
210
+ /** Tổng số tin nhắn đã gửi thành công */
211
+ totalSuccessfulMessages: number;
212
+ /** Tổng số tin nhắn thất bại */
213
+ totalFailedMessages: number;
214
+ /** Tổng số tin nhắn */
215
+ totalMessages: number;
216
+ };
217
+ }
218
+ /**
219
+ * Các loại template action types
220
+ */
221
+ export type TemplateActionType = "oa.open.url" | "oa.query.show" | "oa.query.hide" | "oa.open.sms" | "oa.open.phone";
222
+ /**
223
+ * Các loại template button types (giống với action types)
224
+ */
225
+ export type TemplateButtonType = TemplateActionType;
226
+ /**
227
+ * Payload cho URL action/button
228
+ */
229
+ export interface UrlPayload {
230
+ url: string;
231
+ }
232
+ /**
233
+ * Payload cho SMS action/button
234
+ */
235
+ export interface SmsPayload {
236
+ phone_code: string;
237
+ content?: string;
238
+ }
239
+ /**
240
+ * Payload cho Phone action/button
241
+ */
242
+ export interface PhonePayload {
243
+ phone_code: string;
244
+ }
245
+ /**
246
+ * Union type cho tất cả các loại payload
247
+ */
248
+ export type ConsultationTemplatePayload = string | UrlPayload | SmsPayload | PhonePayload;
249
+ /**
250
+ * Template types được hỗ trợ
251
+ */
252
+ export type TemplateType = "request_user_info" | "media" | string;
253
+ //# sourceMappingURL=consultation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consultation.d.ts","sourceRoot":"","sources":["../../src/types/consultation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,kBAAkB;IAClB,IAAI,EAAE,aAAa,GAAG,eAAe,GAAG,eAAe,GAAG,aAAa,GAAG,eAAe,CAAC;IAC1F,4BAA4B;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,qGAAqG;IACrG,OAAO,CAAC,EAAE,MAAM,GAAG;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACxF;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe;IAC9B,sCAAsC;IACtC,KAAK,EAAE,MAAM,CAAC;IACd,6FAA6F;IAC7F,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yBAAyB;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,cAAc;IAC7B,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,kBAAkB;IAClB,IAAI,EAAE,aAAa,GAAG,eAA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M,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,2BAA2B,GAAG,MAAM,GAAG,UAAU,GAAG,UAAU,GAAG,YAAY,CAAC;AAE1F;;GAEG;AACH,MAAM,MAAM,YAAY,GACpB,mBAAmB,GACnB,OAAO,GACP,MAAM,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ /**
3
+ * Types cho Consultation Service - Zalo OA API
4
+ *
5
+ * Consultation messages (tin nhắn tư vấn) là loại tin nhắn đặc biệt
6
+ * cho phép OA gửi tin nhắn chủ động đến người dùng trong khung thời gian nhất định
7
+ *
8
+ * API Endpoint: https://openapi.zalo.me/v3.0/oa/message/cs
9
+ * Method: POST
10
+ * Content-Type: application/json
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ //# sourceMappingURL=consultation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consultation.js","sourceRoot":"","sources":["../../src/types/consultation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG"}
@@ -0,0 +1,324 @@
1
+ /**
2
+ * Ví dụ sử dụng ConsultationService.sendTemplateMessage()
3
+ * Gửi tin nhắn template tổng quát với elements và buttons
4
+ *
5
+ * API Endpoint: https://openapi.zalo.me/v3.0/oa/message/cs
6
+ * Method: POST
7
+ * Content-Type: application/json
8
+ */
9
+
10
+ import {
11
+ ConsultationService,
12
+ TemplateElement,
13
+ TemplateButton,
14
+ TemplateAction
15
+ } from '../src/services/consultation.service';
16
+ import { ZaloClient } from '../src/clients/zalo-client';
17
+
18
+ // Khởi tạo service
19
+ const zaloClient = new ZaloClient();
20
+ const consultationService = new ConsultationService(zaloClient);
21
+
22
+ const ACCESS_TOKEN = "your_access_token_here";
23
+ const USER_ID = "2468458835296117922";
24
+
25
+ /**
26
+ * Ví dụ 1: Template request_user_info cơ bản
27
+ */
28
+ async function example1_BasicRequestUserInfo() {
29
+ try {
30
+ // Tạo elements (tối đa 5 phần tử)
31
+ const elements: TemplateElement[] = [
32
+ // Element đầu tiên PHẢI có subtitle
33
+ consultationService.createTemplateElement(
34
+ "OA Chatbot (Testing)",
35
+ "Đang yêu cầu thông tin từ bạn",
36
+ "https://developers.zalo.me/web/static/zalo.png",
37
+ consultationService.createUrlAction("https://developers.zalo.me/")
38
+ ),
39
+ // Các element sau có thể không có subtitle
40
+ consultationService.createTemplateElement(
41
+ "Liên hệ hỗ trợ",
42
+ undefined, // không có subtitle
43
+ "https://developers.zalo.me/web/static/zalo.png",
44
+ consultationService.createPhoneAction("84919018791")
45
+ )
46
+ ];
47
+
48
+ // Tạo buttons (tối đa 5 phần tử)
49
+ const buttons: TemplateButton[] = [
50
+ consultationService.createUrlButton("OPEN URL", "https://developers.zalo.me/"),
51
+ consultationService.createQueryShowButton("QUERY SHOW", "#callback_data"),
52
+ consultationService.createQueryHideButton("QUERY HIDE", "#callback_data"),
53
+ consultationService.createSmsButton("OPEN SMS", "84919018791", "alo"),
54
+ consultationService.createPhoneButton("OPEN PHONE", "84919018791")
55
+ ];
56
+
57
+ // Gửi template message
58
+ const result = await consultationService.sendTemplateMessage(
59
+ ACCESS_TOKEN,
60
+ USER_ID,
61
+ "request_user_info",
62
+ elements,
63
+ buttons
64
+ );
65
+
66
+ console.log("✅ Template message sent successfully:", result);
67
+ return result;
68
+
69
+ } catch (error) {
70
+ console.error("❌ Error sending template message:", error);
71
+ throw error;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Ví dụ 2: Template đơn giản chỉ có 1 element và 2 buttons
77
+ */
78
+ async function example2_SimpleTemplate() {
79
+ try {
80
+ // Chỉ 1 element
81
+ const elements: TemplateElement[] = [
82
+ {
83
+ title: "Chào mừng bạn đến với dịch vụ của chúng tôi!",
84
+ subtitle: "Chúng tôi có thể hỗ trợ gì cho bạn?",
85
+ image_url: "https://example.com/welcome.png"
86
+ }
87
+ ];
88
+
89
+ // 2 buttons đơn giản
90
+ const buttons: TemplateButton[] = [
91
+ consultationService.createQueryShowButton("Tư vấn sản phẩm", "product_consultation"),
92
+ consultationService.createPhoneButton("Gọi hotline", "84919018791")
93
+ ];
94
+
95
+ const result = await consultationService.sendTemplateMessage(
96
+ ACCESS_TOKEN,
97
+ USER_ID,
98
+ "request_user_info",
99
+ elements,
100
+ buttons
101
+ );
102
+
103
+ console.log("✅ Simple template sent successfully:", result);
104
+ return result;
105
+
106
+ } catch (error) {
107
+ console.error("❌ Error sending simple template:", error);
108
+ throw error;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Ví dụ 3: Template không có buttons (chỉ elements)
114
+ */
115
+ async function example3_ElementsOnly() {
116
+ try {
117
+ const elements: TemplateElement[] = [
118
+ {
119
+ title: "Thông báo quan trọng",
120
+ subtitle: "Hệ thống sẽ bảo trì từ 2:00 - 4:00 sáng ngày mai",
121
+ image_url: "https://example.com/maintenance.png",
122
+ default_action: {
123
+ type: "oa.open.url",
124
+ url: "https://example.com/maintenance-info"
125
+ }
126
+ },
127
+ {
128
+ title: "Liên hệ hỗ trợ khẩn cấp",
129
+ // Không có subtitle cho element thứ 2
130
+ default_action: {
131
+ type: "oa.open.phone",
132
+ payload: { phone_code: "84919018791" }
133
+ }
134
+ }
135
+ ];
136
+
137
+ // Không có buttons
138
+ const result = await consultationService.sendTemplateMessage(
139
+ ACCESS_TOKEN,
140
+ USER_ID,
141
+ "request_user_info",
142
+ elements
143
+ // buttons parameter bỏ qua
144
+ );
145
+
146
+ console.log("✅ Elements-only template sent successfully:", result);
147
+ return result;
148
+
149
+ } catch (error) {
150
+ console.error("❌ Error sending elements-only template:", error);
151
+ throw error;
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Ví dụ 4: Template với tất cả loại buttons
157
+ */
158
+ async function example4_AllButtonTypes() {
159
+ try {
160
+ const elements: TemplateElement[] = [
161
+ {
162
+ title: "Trung tâm hỗ trợ khách hàng",
163
+ subtitle: "Chọn hình thức liên hệ phù hợp với bạn",
164
+ image_url: "https://example.com/support.png"
165
+ }
166
+ ];
167
+
168
+ const buttons: TemplateButton[] = [
169
+ // URL button
170
+ {
171
+ title: "Xem FAQ",
172
+ type: "oa.open.url",
173
+ payload: { url: "https://example.com/faq" }
174
+ },
175
+ // Query show button
176
+ {
177
+ title: "Chat với tư vấn viên",
178
+ type: "oa.query.show",
179
+ payload: "start_chat_with_agent"
180
+ },
181
+ // Query hide button
182
+ {
183
+ title: "Gửi phản hồi",
184
+ type: "oa.query.hide",
185
+ payload: "send_feedback"
186
+ },
187
+ // SMS button
188
+ {
189
+ title: "Gửi SMS hỗ trợ",
190
+ type: "oa.open.sms",
191
+ payload: {
192
+ phone_code: "84919018791",
193
+ content: "Tôi cần hỗ trợ"
194
+ }
195
+ },
196
+ // Phone button
197
+ {
198
+ title: "Gọi hotline",
199
+ type: "oa.open.phone",
200
+ payload: { phone_code: "84919018791" }
201
+ }
202
+ ];
203
+
204
+ const result = await consultationService.sendTemplateMessage(
205
+ ACCESS_TOKEN,
206
+ USER_ID,
207
+ "request_user_info",
208
+ elements,
209
+ buttons
210
+ );
211
+
212
+ console.log("✅ All button types template sent successfully:", result);
213
+ return result;
214
+
215
+ } catch (error) {
216
+ console.error("❌ Error sending all button types template:", error);
217
+ throw error;
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Ví dụ 5: Template với validation errors (để test)
223
+ */
224
+ async function example5_ValidationErrors() {
225
+ try {
226
+ // Element với title quá dài (> 100 ký tự)
227
+ const invalidElements: TemplateElement[] = [
228
+ {
229
+ title: "A".repeat(101), // 101 ký tự - sẽ báo lỗi
230
+ subtitle: "This will cause validation error"
231
+ }
232
+ ];
233
+
234
+ await consultationService.sendTemplateMessage(
235
+ ACCESS_TOKEN,
236
+ USER_ID,
237
+ "request_user_info",
238
+ invalidElements
239
+ );
240
+
241
+ } catch (error) {
242
+ console.log("✅ Expected validation error caught:", error.message);
243
+ }
244
+
245
+ try {
246
+ // Element đầu tiên không có subtitle
247
+ const invalidElements2: TemplateElement[] = [
248
+ {
249
+ title: "Valid title",
250
+ // Thiếu subtitle cho element đầu tiên - sẽ báo lỗi
251
+ }
252
+ ];
253
+
254
+ await consultationService.sendTemplateMessage(
255
+ ACCESS_TOKEN,
256
+ USER_ID,
257
+ "request_user_info",
258
+ invalidElements2
259
+ );
260
+
261
+ } catch (error) {
262
+ console.log("✅ Expected validation error caught:", error.message);
263
+ }
264
+
265
+ try {
266
+ // Quá nhiều elements (> 5)
267
+ const tooManyElements: TemplateElement[] = Array(6).fill(null).map((_, i) => ({
268
+ title: `Element ${i + 1}`,
269
+ subtitle: i === 0 ? "Required subtitle for first element" : undefined
270
+ }));
271
+
272
+ await consultationService.sendTemplateMessage(
273
+ ACCESS_TOKEN,
274
+ USER_ID,
275
+ "request_user_info",
276
+ tooManyElements
277
+ );
278
+
279
+ } catch (error) {
280
+ console.log("✅ Expected validation error caught:", error.message);
281
+ }
282
+ }
283
+
284
+ // Chạy các ví dụ
285
+ async function runExamples() {
286
+ console.log("🚀 Running Consultation Template Examples...\n");
287
+
288
+ try {
289
+ await example1_BasicRequestUserInfo();
290
+ console.log("\n");
291
+
292
+ await example2_SimpleTemplate();
293
+ console.log("\n");
294
+
295
+ await example3_ElementsOnly();
296
+ console.log("\n");
297
+
298
+ await example4_AllButtonTypes();
299
+ console.log("\n");
300
+
301
+ await example5_ValidationErrors();
302
+ console.log("\n");
303
+
304
+ console.log("✅ All examples completed!");
305
+
306
+ } catch (error) {
307
+ console.error("❌ Example failed:", error);
308
+ }
309
+ }
310
+
311
+ // Export để có thể import và sử dụng
312
+ export {
313
+ example1_BasicRequestUserInfo,
314
+ example2_SimpleTemplate,
315
+ example3_ElementsOnly,
316
+ example4_AllButtonTypes,
317
+ example5_ValidationErrors,
318
+ runExamples
319
+ };
320
+
321
+ // Chạy nếu file được execute trực tiếp
322
+ if (require.main === module) {
323
+ runExamples();
324
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warriorteam/redai-zalo-sdk",
3
- "version": "1.29.0",
3
+ "version": "1.30.0",
4
4
  "description": "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 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",