@warriorteam/redai-zalo-sdk 1.26.1 → 1.27.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.
@@ -1,317 +0,0 @@
1
- /**
2
- * Example: Chỉnh sửa ZNS Template theo chuẩn Zalo API
3
- *
4
- * Ví dụ này minh họa cách sử dụng SDK để chỉnh sửa template ZNS
5
- * theo đúng cấu trúc API của Zalo
6
- */
7
-
8
- import {
9
- ZNSService,
10
- ZNSUpdateTemplateRequest,
11
- ZNSTemplateType,
12
- ZNSTemplateTag,
13
- ZNSButtonType,
14
- ZNSParamType,
15
- ZNS_TEMPLATE_TYPES,
16
- ZNS_TEMPLATE_TAGS,
17
- ZNSValidation
18
- } from '../src/index';
19
-
20
- // Khởi tạo ZNS Service
21
- const znsService = new ZNSService({
22
- appId: 'your_app_id',
23
- appSecret: 'your_app_secret'
24
- });
25
-
26
- /**
27
- * Ví dụ 1: Chỉnh sửa template ZNS tùy chỉnh
28
- */
29
- async function editCustomTemplate() {
30
- const accessToken = 'your_access_token';
31
-
32
- // Dữ liệu template theo đúng chuẩn Zalo API
33
- const templateData: ZNSUpdateTemplateRequest = {
34
- template_id: "234567",
35
- template_name: "Xác nhận đơn hàng - Cập nhật",
36
- template_type: ZNSTemplateType.CUSTOM,
37
- tag: ZNSTemplateTag.TRANSACTION,
38
- layout: {
39
- header: {
40
- components: [
41
- {
42
- LOGO: {
43
- light: {
44
- type: "IMAGE",
45
- media_id: "1ashjkdbkj"
46
- },
47
- dark: {
48
- type: "IMAGE",
49
- media_id: "1ashjkdbkj"
50
- }
51
- }
52
- }
53
- ]
54
- },
55
- body: {
56
- components: [
57
- {
58
- TITLE: {
59
- value: "Xác nhận đơn hàng"
60
- }
61
- },
62
- {
63
- PARAGRAPH: {
64
- value: "Cảm ơn <name> đã mua hàng tại cửa hàng. Đơn hàng của bạn đã được xác nhận với chi tiết như sau:"
65
- }
66
- },
67
- {
68
- TABLE: {
69
- rows: [
70
- {
71
- value: "<order_code>",
72
- title: "Mã đơn",
73
- row_type: 1
74
- },
75
- {
76
- value: "<phone_number>",
77
- title: "Điện thoại"
78
- },
79
- {
80
- value: "<price>",
81
- title: "Giá tiền"
82
- },
83
- {
84
- value: "<status>",
85
- title: "Trạng thái"
86
- },
87
- {
88
- value: "<date>",
89
- title: "Ngày đặt hàng"
90
- }
91
- ]
92
- }
93
- }
94
- ]
95
- },
96
- footer: {
97
- components: [
98
- {
99
- BUTTONS: {
100
- items: [
101
- {
102
- content: "https://example.com/order",
103
- type: ZNSButtonType.URL,
104
- title: "Xem đơn hàng"
105
- }
106
- ]
107
- }
108
- }
109
- ]
110
- }
111
- },
112
- params: [
113
- {
114
- type: ZNSParamType.CUSTOMER_NAME,
115
- name: "name",
116
- sample_value: "Nguyễn Văn A"
117
- },
118
- {
119
- type: ZNSParamType.CODE,
120
- name: "order_code",
121
- sample_value: "ABC123"
122
- },
123
- {
124
- type: ZNSParamType.PHONE_NUMBER,
125
- name: "phone_number",
126
- sample_value: "0123456789"
127
- },
128
- {
129
- type: ZNSParamType.CURRENCY,
130
- name: "price",
131
- sample_value: "100000"
132
- },
133
- {
134
- type: ZNSParamType.TRANSACTION_STATUS,
135
- name: "status",
136
- sample_value: "CONFIRMED"
137
- },
138
- {
139
- type: ZNSParamType.TIME,
140
- name: "date",
141
- sample_value: "19/01/2024"
142
- }
143
- ],
144
- note: "Cập nhật template theo yêu cầu kiểm duyệt",
145
- tracking_id: "edit_template_001"
146
- };
147
-
148
- try {
149
- // Validate trước khi gửi
150
- if (!ZNSValidation.isValidTemplateName(templateData.template_name)) {
151
- throw new Error('Tên template phải từ 10-60 ký tự');
152
- }
153
-
154
- if (!ZNSValidation.isTagCompatibleWithType(templateData.template_type, templateData.tag)) {
155
- throw new Error('Tag không tương thích với loại template');
156
- }
157
-
158
- if (templateData.note && !ZNSValidation.isValidNote(templateData.note)) {
159
- throw new Error('Ghi chú phải từ 1-400 ký tự');
160
- }
161
-
162
- // Gửi request chỉnh sửa template
163
- const result = await znsService.updateTemplate(accessToken, templateData);
164
-
165
- console.log('✅ Chỉnh sửa template thành công:', {
166
- templateId: result.data.template_id,
167
- templateName: result.data.template_name,
168
- status: result.data.status,
169
- previewUrl: result.data.preview_url
170
- });
171
-
172
- return result;
173
- } catch (error) {
174
- console.error('❌ Lỗi chỉnh sửa template:', error);
175
- throw error;
176
- }
177
- }
178
-
179
- /**
180
- * Ví dụ 2: Chỉnh sửa template ZNS voucher
181
- */
182
- async function editVoucherTemplate() {
183
- const accessToken = 'your_access_token';
184
-
185
- const templateData: ZNSUpdateTemplateRequest = {
186
- template_id: "345678",
187
- template_name: "Voucher khuyến mãi đặc biệt",
188
- template_type: ZNSTemplateType.VOUCHER,
189
- tag: ZNSTemplateTag.PROMOTION,
190
- layout: {
191
- body: {
192
- components: [
193
- {
194
- TITLE: {
195
- value: "🎉 Voucher Khuyến Mãi Đặc Biệt"
196
- }
197
- },
198
- {
199
- PARAGRAPH: {
200
- value: "Chúc mừng <customer_name>! Bạn nhận được voucher giảm giá đặc biệt:"
201
- }
202
- },
203
- {
204
- VOUCHER: {
205
- name: "Giảm 50%",
206
- condition: "Cho đơn hàng trên 500K",
207
- voucher_code: "<voucher_code>",
208
- start_date: "<start_date>",
209
- end_date: "<end_date>",
210
- display_code: 1
211
- }
212
- }
213
- ]
214
- },
215
- footer: {
216
- components: [
217
- {
218
- BUTTONS: {
219
- items: [
220
- {
221
- content: "https://shop.example.com",
222
- type: ZNSButtonType.URL,
223
- title: "Mua ngay"
224
- }
225
- ]
226
- }
227
- }
228
- ]
229
- }
230
- },
231
- params: [
232
- {
233
- type: ZNSParamType.CUSTOMER_NAME,
234
- name: "customer_name",
235
- sample_value: "Trần Thị B"
236
- },
237
- {
238
- type: ZNSParamType.CODE,
239
- name: "voucher_code",
240
- sample_value: "SAVE50"
241
- },
242
- {
243
- type: ZNSParamType.TIME,
244
- name: "start_date",
245
- sample_value: "01/02/2024"
246
- },
247
- {
248
- type: ZNSParamType.TIME,
249
- name: "end_date",
250
- sample_value: "29/02/2024"
251
- }
252
- ],
253
- note: "Template voucher cho chiến dịch tháng 2",
254
- tracking_id: "voucher_template_002"
255
- };
256
-
257
- try {
258
- const result = await znsService.updateTemplate(accessToken, templateData);
259
- console.log('✅ Chỉnh sửa voucher template thành công:', result.data);
260
- return result;
261
- } catch (error) {
262
- console.error('❌ Lỗi chỉnh sửa voucher template:', error);
263
- throw error;
264
- }
265
- }
266
-
267
- /**
268
- * Ví dụ 3: Helper functions để tạo template data
269
- */
270
- export const ZNSTemplateHelpers = {
271
- /**
272
- * Tạo component TITLE
273
- */
274
- createTitleComponent: (value: string) => ({
275
- TITLE: { value }
276
- }),
277
-
278
- /**
279
- * Tạo component PARAGRAPH
280
- */
281
- createParagraphComponent: (value: string) => ({
282
- PARAGRAPH: { value }
283
- }),
284
-
285
- /**
286
- * Tạo component BUTTON
287
- */
288
- createButtonComponent: (buttons: Array<{ title: string; url: string }>) => ({
289
- BUTTONS: {
290
- items: buttons.map(btn => ({
291
- content: btn.url,
292
- type: ZNSButtonType.URL,
293
- title: btn.title
294
- }))
295
- }
296
- }),
297
-
298
- /**
299
- * Tạo param với validation
300
- */
301
- createParam: (type: ZNSParamType, name: string, sampleValue: string) => {
302
- if (!ZNSValidation.isValidParamValue(type, sampleValue)) {
303
- throw new Error(`Sample value "${sampleValue}" vượt quá độ dài cho phép của param type ${type}`);
304
- }
305
- return {
306
- type,
307
- name,
308
- sample_value: sampleValue
309
- };
310
- }
311
- };
312
-
313
- // Export examples
314
- export {
315
- editCustomTemplate,
316
- editVoucherTemplate
317
- };