@warriorteam/redai-zalo-sdk 1.12.0 → 1.12.1

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,178 +1,178 @@
1
- /**
2
- * Example: Update Article Status
3
- *
4
- * Demonstrates how to update article status using the RedAI Zalo SDK
5
- */
6
-
7
- import { ZaloSDK, ArticleStatus, CommentStatus } from '../src';
8
-
9
- async function updateArticleStatusExample() {
10
- // Initialize SDK
11
- const sdk = new ZaloSDK();
12
-
13
- // Your access token
14
- const accessToken = 'YOUR_ACCESS_TOKEN';
15
-
16
- // Article ID to update
17
- const articleId = 'YOUR_ARTICLE_ID';
18
-
19
- try {
20
- console.log('šŸ”„ Updating article status...');
21
-
22
- // Example 1: Update status to 'show' (publish)
23
- const publishResult = await sdk.article.updateArticleStatus(
24
- accessToken,
25
- articleId,
26
- ArticleStatus.SHOW
27
- );
28
-
29
- console.log('āœ… Article published successfully!');
30
- console.log('Update token:', publishResult.token);
31
-
32
- // Wait a moment before next update
33
- await new Promise(resolve => setTimeout(resolve, 2000));
34
-
35
- // Example 2: Update status to 'hide' (unpublish) and disable comments
36
- const hideResult = await sdk.article.updateArticleStatus(
37
- accessToken,
38
- articleId,
39
- ArticleStatus.HIDE,
40
- CommentStatus.HIDE
41
- );
42
-
43
- console.log('āœ… Article hidden successfully!');
44
- console.log('Update token:', hideResult.token);
45
-
46
- // Example 3: Check update progress
47
- console.log('šŸ” Checking update progress...');
48
- const progress = await sdk.article.checkArticleProcess(
49
- accessToken,
50
- hideResult.token
51
- );
52
-
53
- console.log('Update progress:', progress);
54
-
55
- // Example 4: Get updated article details to verify changes
56
- console.log('šŸ“„ Getting updated article details...');
57
- const updatedArticle = await sdk.article.getArticleDetail(
58
- accessToken,
59
- articleId
60
- );
61
-
62
- console.log('Updated article status:', updatedArticle.status);
63
- console.log('Updated comment status:', updatedArticle.comment);
64
-
65
- } catch (error) {
66
- console.error('āŒ Error updating article status:', error);
67
-
68
- if (error instanceof Error) {
69
- console.error('Error message:', error.message);
70
- }
71
- }
72
- }
73
-
74
- // Advanced example: Batch status update
75
- async function batchUpdateArticleStatus() {
76
- const sdk = new ZaloSDK();
77
- const accessToken = 'YOUR_ACCESS_TOKEN';
78
-
79
- // List of article IDs to update
80
- const articleIds = ['ARTICLE_ID_1', 'ARTICLE_ID_2', 'ARTICLE_ID_3'];
81
-
82
- console.log('šŸ”„ Batch updating article statuses...');
83
-
84
- const results = [];
85
-
86
- for (const articleId of articleIds) {
87
- try {
88
- console.log(`Updating article ${articleId}...`);
89
-
90
- const result = await sdk.article.updateArticleStatus(
91
- accessToken,
92
- articleId,
93
- ArticleStatus.SHOW,
94
- CommentStatus.SHOW
95
- );
96
-
97
- results.push({
98
- articleId,
99
- success: true,
100
- token: result.token
101
- });
102
-
103
- console.log(`āœ… Article ${articleId} updated successfully`);
104
-
105
- // Add delay to avoid rate limiting
106
- await new Promise(resolve => setTimeout(resolve, 1000));
107
-
108
- } catch (error) {
109
- console.error(`āŒ Failed to update article ${articleId}:`, error);
110
-
111
- results.push({
112
- articleId,
113
- success: false,
114
- error: error instanceof Error ? error.message : 'Unknown error'
115
- });
116
- }
117
- }
118
-
119
- console.log('šŸ“Š Batch update results:', results);
120
- return results;
121
- }
122
-
123
- // Example with error handling and retry logic
124
- async function updateArticleStatusWithRetry() {
125
- const sdk = new ZaloSDK();
126
- const accessToken = 'YOUR_ACCESS_TOKEN';
127
- const articleId = 'YOUR_ARTICLE_ID';
128
- const maxRetries = 3;
129
-
130
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
131
- try {
132
- console.log(`šŸ”„ Attempt ${attempt}/${maxRetries}: Updating article status...`);
133
-
134
- const result = await sdk.article.updateArticleStatus(
135
- accessToken,
136
- articleId,
137
- ArticleStatus.SHOW
138
- );
139
-
140
- console.log('āœ… Article status updated successfully!');
141
- console.log('Update token:', result.token);
142
-
143
- return result;
144
-
145
- } catch (error) {
146
- console.error(`āŒ Attempt ${attempt} failed:`, error);
147
-
148
- if (attempt === maxRetries) {
149
- console.error('🚫 All retry attempts failed');
150
- throw error;
151
- }
152
-
153
- // Wait before retry (exponential backoff)
154
- const delay = Math.pow(2, attempt) * 1000;
155
- console.log(`ā³ Waiting ${delay}ms before retry...`);
156
- await new Promise(resolve => setTimeout(resolve, delay));
157
- }
158
- }
159
- }
160
-
161
- // Run examples
162
- if (require.main === module) {
163
- console.log('šŸš€ Running Article Status Update Examples...\n');
164
-
165
- // Uncomment the example you want to run:
166
-
167
- // updateArticleStatusExample();
168
- // batchUpdateArticleStatus();
169
- // updateArticleStatusWithRetry();
170
-
171
- console.log('\nšŸ’” Remember to replace YOUR_ACCESS_TOKEN and YOUR_ARTICLE_ID with actual values!');
172
- }
173
-
174
- export {
175
- updateArticleStatusExample,
176
- batchUpdateArticleStatus,
177
- updateArticleStatusWithRetry
178
- };
1
+ /**
2
+ * Example: Update Article Status
3
+ *
4
+ * Demonstrates how to update article status using the RedAI Zalo SDK
5
+ */
6
+
7
+ import { ZaloSDK, ArticleStatus, CommentStatus } from '../src';
8
+
9
+ async function updateArticleStatusExample() {
10
+ // Initialize SDK
11
+ const sdk = new ZaloSDK();
12
+
13
+ // Your access token
14
+ const accessToken = 'YOUR_ACCESS_TOKEN';
15
+
16
+ // Article ID to update
17
+ const articleId = 'YOUR_ARTICLE_ID';
18
+
19
+ try {
20
+ console.log('šŸ”„ Updating article status...');
21
+
22
+ // Example 1: Update status to 'show' (publish)
23
+ const publishResult = await sdk.article.updateArticleStatus(
24
+ accessToken,
25
+ articleId,
26
+ ArticleStatus.SHOW
27
+ );
28
+
29
+ console.log('āœ… Article published successfully!');
30
+ console.log('Update token:', publishResult.token);
31
+
32
+ // Wait a moment before next update
33
+ await new Promise(resolve => setTimeout(resolve, 2000));
34
+
35
+ // Example 2: Update status to 'hide' (unpublish) and disable comments
36
+ const hideResult = await sdk.article.updateArticleStatus(
37
+ accessToken,
38
+ articleId,
39
+ ArticleStatus.HIDE,
40
+ CommentStatus.HIDE
41
+ );
42
+
43
+ console.log('āœ… Article hidden successfully!');
44
+ console.log('Update token:', hideResult.token);
45
+
46
+ // Example 3: Check update progress
47
+ console.log('šŸ” Checking update progress...');
48
+ const progress = await sdk.article.checkArticleProcess(
49
+ accessToken,
50
+ hideResult.token
51
+ );
52
+
53
+ console.log('Update progress:', progress);
54
+
55
+ // Example 4: Get updated article details to verify changes
56
+ console.log('šŸ“„ Getting updated article details...');
57
+ const updatedArticle = await sdk.article.getArticleDetail(
58
+ accessToken,
59
+ articleId
60
+ );
61
+
62
+ console.log('Updated article status:', updatedArticle.status);
63
+ console.log('Updated comment status:', updatedArticle.comment);
64
+
65
+ } catch (error) {
66
+ console.error('āŒ Error updating article status:', error);
67
+
68
+ if (error instanceof Error) {
69
+ console.error('Error message:', error.message);
70
+ }
71
+ }
72
+ }
73
+
74
+ // Advanced example: Batch status update
75
+ async function batchUpdateArticleStatus() {
76
+ const sdk = new ZaloSDK();
77
+ const accessToken = 'YOUR_ACCESS_TOKEN';
78
+
79
+ // List of article IDs to update
80
+ const articleIds = ['ARTICLE_ID_1', 'ARTICLE_ID_2', 'ARTICLE_ID_3'];
81
+
82
+ console.log('šŸ”„ Batch updating article statuses...');
83
+
84
+ const results = [];
85
+
86
+ for (const articleId of articleIds) {
87
+ try {
88
+ console.log(`Updating article ${articleId}...`);
89
+
90
+ const result = await sdk.article.updateArticleStatus(
91
+ accessToken,
92
+ articleId,
93
+ ArticleStatus.SHOW,
94
+ CommentStatus.SHOW
95
+ );
96
+
97
+ results.push({
98
+ articleId,
99
+ success: true,
100
+ token: result.token
101
+ });
102
+
103
+ console.log(`āœ… Article ${articleId} updated successfully`);
104
+
105
+ // Add delay to avoid rate limiting
106
+ await new Promise(resolve => setTimeout(resolve, 1000));
107
+
108
+ } catch (error) {
109
+ console.error(`āŒ Failed to update article ${articleId}:`, error);
110
+
111
+ results.push({
112
+ articleId,
113
+ success: false,
114
+ error: error instanceof Error ? error.message : 'Unknown error'
115
+ });
116
+ }
117
+ }
118
+
119
+ console.log('šŸ“Š Batch update results:', results);
120
+ return results;
121
+ }
122
+
123
+ // Example with error handling and retry logic
124
+ async function updateArticleStatusWithRetry() {
125
+ const sdk = new ZaloSDK();
126
+ const accessToken = 'YOUR_ACCESS_TOKEN';
127
+ const articleId = 'YOUR_ARTICLE_ID';
128
+ const maxRetries = 3;
129
+
130
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
131
+ try {
132
+ console.log(`šŸ”„ Attempt ${attempt}/${maxRetries}: Updating article status...`);
133
+
134
+ const result = await sdk.article.updateArticleStatus(
135
+ accessToken,
136
+ articleId,
137
+ ArticleStatus.SHOW
138
+ );
139
+
140
+ console.log('āœ… Article status updated successfully!');
141
+ console.log('Update token:', result.token);
142
+
143
+ return result;
144
+
145
+ } catch (error) {
146
+ console.error(`āŒ Attempt ${attempt} failed:`, error);
147
+
148
+ if (attempt === maxRetries) {
149
+ console.error('🚫 All retry attempts failed');
150
+ throw error;
151
+ }
152
+
153
+ // Wait before retry (exponential backoff)
154
+ const delay = Math.pow(2, attempt) * 1000;
155
+ console.log(`ā³ Waiting ${delay}ms before retry...`);
156
+ await new Promise(resolve => setTimeout(resolve, delay));
157
+ }
158
+ }
159
+ }
160
+
161
+ // Run examples
162
+ if (require.main === module) {
163
+ console.log('šŸš€ Running Article Status Update Examples...\n');
164
+
165
+ // Uncomment the example you want to run:
166
+
167
+ // updateArticleStatusExample();
168
+ // batchUpdateArticleStatus();
169
+ // updateArticleStatusWithRetry();
170
+
171
+ console.log('\nšŸ’” Remember to replace YOUR_ACCESS_TOKEN and YOUR_ARTICLE_ID with actual values!');
172
+ }
173
+
174
+ export {
175
+ updateArticleStatusExample,
176
+ batchUpdateArticleStatus,
177
+ updateArticleStatusWithRetry
178
+ };