@warriorteam/redai-zalo-sdk 1.1.0 → 1.1.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.
- package/ARCHITECTURE.md +265 -0
- package/README.md +59 -2
- package/SERVICES_ADDED.md +503 -0
- package/docs/ARTICLE_MANAGEMENT.md +336 -0
- package/docs/GROUP_MANAGEMENT.md +232 -0
- package/docs/WEBHOOK_EVENTS.md +806 -0
- package/package.json +5 -2
package/ARCHITECTURE.md
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# RedAI Zalo SDK Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
RedAI Zalo SDK là một TypeScript SDK hoàn chỉnh cho các API của Zalo, được thiết kế với kiến trúc modular và type-safe.
|
|
6
|
+
|
|
7
|
+
## Cấu trúc thư mục
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
redai-zalo-sdk/
|
|
11
|
+
├── src/
|
|
12
|
+
│ ├── types/ # Type definitions
|
|
13
|
+
│ │ ├── common.ts # Common types và interfaces
|
|
14
|
+
│ │ ├── auth.ts # Authentication types
|
|
15
|
+
│ │ ├── oa.ts # Official Account types
|
|
16
|
+
│ │ ├── message.ts # Message types
|
|
17
|
+
│ │ └── webhook.ts # Webhook types
|
|
18
|
+
│ ├── clients/ # HTTP clients
|
|
19
|
+
│ │ ├── base-client.ts # Base HTTP client
|
|
20
|
+
│ │ └── zalo-client.ts # Zalo-specific client
|
|
21
|
+
│ ├── services/ # Business logic services
|
|
22
|
+
│ │ ├── auth.service.ts # Authentication service
|
|
23
|
+
│ │ ├── oa.service.ts # Official Account service
|
|
24
|
+
│ │ └── message.service.ts # Message service
|
|
25
|
+
│ ├── utils/ # Utility functions
|
|
26
|
+
│ ├── index.ts # Main export file
|
|
27
|
+
│ └── zalo-sdk.ts # Main SDK class
|
|
28
|
+
├── examples/ # Usage examples
|
|
29
|
+
├── dist/ # Build output
|
|
30
|
+
├── package.json
|
|
31
|
+
├── tsconfig.json
|
|
32
|
+
└── README.md
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Kiến trúc chính
|
|
36
|
+
|
|
37
|
+
### 1. Layered Architecture
|
|
38
|
+
|
|
39
|
+
SDK được thiết kế theo kiến trúc phân lớp:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
┌─────────────────┐
|
|
43
|
+
│ Main SDK │ ← ZaloSDK class (public API)
|
|
44
|
+
├─────────────────┤
|
|
45
|
+
│ Services │ ← Business logic (AuthService, OAService, MessageService)
|
|
46
|
+
├─────────────────┤
|
|
47
|
+
│ Clients │ ← HTTP communication (BaseClient, ZaloClient)
|
|
48
|
+
├─────────────────┤
|
|
49
|
+
│ Types │ ← Type definitions và interfaces
|
|
50
|
+
└─────────────────┘
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 2. Core Components
|
|
54
|
+
|
|
55
|
+
#### ZaloSDK (Main Class)
|
|
56
|
+
- Entry point cho tất cả các chức năng
|
|
57
|
+
- Khởi tạo và quản lý các services
|
|
58
|
+
- Cung cấp quick methods cho các tác vụ phổ biến
|
|
59
|
+
- Quản lý configuration và lifecycle
|
|
60
|
+
|
|
61
|
+
#### BaseClient
|
|
62
|
+
- HTTP client cơ bản với axios
|
|
63
|
+
- Retry mechanism
|
|
64
|
+
- Error handling
|
|
65
|
+
- Request/response logging
|
|
66
|
+
- File upload support
|
|
67
|
+
|
|
68
|
+
#### ZaloClient
|
|
69
|
+
- Extends BaseClient
|
|
70
|
+
- Zalo-specific endpoints
|
|
71
|
+
- API versioning support
|
|
72
|
+
- OAuth và ZNS endpoint handling
|
|
73
|
+
|
|
74
|
+
#### Services
|
|
75
|
+
- **AuthService**: OAuth flows, token management
|
|
76
|
+
- **OAService**: Official Account operations
|
|
77
|
+
- **MessageService**: Message sending và file upload
|
|
78
|
+
|
|
79
|
+
### 3. Type System
|
|
80
|
+
|
|
81
|
+
#### Comprehensive Types
|
|
82
|
+
- Tất cả API responses được type-safe
|
|
83
|
+
- Union types cho message types
|
|
84
|
+
- Enum cho constants
|
|
85
|
+
- Generic types cho reusability
|
|
86
|
+
|
|
87
|
+
#### Error Handling
|
|
88
|
+
- Custom `ZaloSDKError` class
|
|
89
|
+
- Structured error information
|
|
90
|
+
- Error code mapping
|
|
91
|
+
|
|
92
|
+
### 4. Configuration
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
interface ZaloSDKConfig {
|
|
96
|
+
appId: string;
|
|
97
|
+
appSecret: string;
|
|
98
|
+
timeout?: number;
|
|
99
|
+
debug?: boolean;
|
|
100
|
+
apiBaseUrl?: string;
|
|
101
|
+
retry?: {
|
|
102
|
+
attempts?: number;
|
|
103
|
+
delay?: number;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Design Patterns
|
|
109
|
+
|
|
110
|
+
### 1. Builder Pattern
|
|
111
|
+
SDK configuration với default values và validation.
|
|
112
|
+
|
|
113
|
+
### 2. Service Pattern
|
|
114
|
+
Mỗi service chịu trách nhiệm cho một domain cụ thể.
|
|
115
|
+
|
|
116
|
+
### 3. Factory Pattern
|
|
117
|
+
Client creation và service initialization.
|
|
118
|
+
|
|
119
|
+
### 4. Strategy Pattern
|
|
120
|
+
Different authentication flows (OA vs Social).
|
|
121
|
+
|
|
122
|
+
### 5. Observer Pattern
|
|
123
|
+
Webhook event handling (planned).
|
|
124
|
+
|
|
125
|
+
## Key Features
|
|
126
|
+
|
|
127
|
+
### 1. Type Safety
|
|
128
|
+
- Full TypeScript support
|
|
129
|
+
- Comprehensive type definitions
|
|
130
|
+
- Generic types cho flexibility
|
|
131
|
+
- Union types cho message variants
|
|
132
|
+
|
|
133
|
+
### 2. Error Handling
|
|
134
|
+
- Structured error responses
|
|
135
|
+
- Retry mechanism
|
|
136
|
+
- Detailed error information
|
|
137
|
+
- Error code mapping
|
|
138
|
+
|
|
139
|
+
### 3. Authentication
|
|
140
|
+
- OAuth 2.0 flows
|
|
141
|
+
- PKCE support
|
|
142
|
+
- Token refresh
|
|
143
|
+
- Multiple auth scopes
|
|
144
|
+
|
|
145
|
+
### 4. Extensibility
|
|
146
|
+
- Modular architecture
|
|
147
|
+
- Easy to add new services
|
|
148
|
+
- Plugin-ready design
|
|
149
|
+
- Custom request support
|
|
150
|
+
|
|
151
|
+
### 5. Developer Experience
|
|
152
|
+
- IntelliSense support
|
|
153
|
+
- Comprehensive documentation
|
|
154
|
+
- Usage examples
|
|
155
|
+
- Debug logging
|
|
156
|
+
|
|
157
|
+
## API Design Principles
|
|
158
|
+
|
|
159
|
+
### 1. Consistency
|
|
160
|
+
- Consistent naming conventions
|
|
161
|
+
- Uniform error handling
|
|
162
|
+
- Standard response formats
|
|
163
|
+
|
|
164
|
+
### 2. Simplicity
|
|
165
|
+
- Easy-to-use public API
|
|
166
|
+
- Quick methods cho common tasks
|
|
167
|
+
- Sensible defaults
|
|
168
|
+
|
|
169
|
+
### 3. Flexibility
|
|
170
|
+
- Low-level access khi cần
|
|
171
|
+
- Custom request support
|
|
172
|
+
- Configurable behavior
|
|
173
|
+
|
|
174
|
+
### 4. Reliability
|
|
175
|
+
- Retry mechanisms
|
|
176
|
+
- Error recovery
|
|
177
|
+
- Connection testing
|
|
178
|
+
|
|
179
|
+
## Future Enhancements
|
|
180
|
+
|
|
181
|
+
### 1. Additional Services
|
|
182
|
+
- ZNS Service (Zalo Notification Service)
|
|
183
|
+
- User Management Service
|
|
184
|
+
- Group Management Service
|
|
185
|
+
- Webhook Service
|
|
186
|
+
|
|
187
|
+
### 2. Advanced Features
|
|
188
|
+
- Rate limiting
|
|
189
|
+
- Caching
|
|
190
|
+
- Batch operations
|
|
191
|
+
- Real-time subscriptions
|
|
192
|
+
|
|
193
|
+
### 3. Developer Tools
|
|
194
|
+
- CLI tools
|
|
195
|
+
- Testing utilities
|
|
196
|
+
- Mock server
|
|
197
|
+
- Documentation generator
|
|
198
|
+
|
|
199
|
+
## Testing Strategy
|
|
200
|
+
|
|
201
|
+
### 1. Unit Tests
|
|
202
|
+
- Service logic testing
|
|
203
|
+
- Type validation
|
|
204
|
+
- Error handling
|
|
205
|
+
|
|
206
|
+
### 2. Integration Tests
|
|
207
|
+
- API endpoint testing
|
|
208
|
+
- Authentication flows
|
|
209
|
+
- File upload/download
|
|
210
|
+
|
|
211
|
+
### 3. E2E Tests
|
|
212
|
+
- Complete workflows
|
|
213
|
+
- Real API interactions
|
|
214
|
+
- Error scenarios
|
|
215
|
+
|
|
216
|
+
## Performance Considerations
|
|
217
|
+
|
|
218
|
+
### 1. HTTP Optimization
|
|
219
|
+
- Connection pooling
|
|
220
|
+
- Request compression
|
|
221
|
+
- Timeout management
|
|
222
|
+
|
|
223
|
+
### 2. Memory Management
|
|
224
|
+
- Efficient data structures
|
|
225
|
+
- Stream processing cho large files
|
|
226
|
+
- Garbage collection friendly
|
|
227
|
+
|
|
228
|
+
### 3. Network Efficiency
|
|
229
|
+
- Batch requests
|
|
230
|
+
- Caching strategies
|
|
231
|
+
- Retry with backoff
|
|
232
|
+
|
|
233
|
+
## Security
|
|
234
|
+
|
|
235
|
+
### 1. Token Management
|
|
236
|
+
- Secure token storage
|
|
237
|
+
- Automatic refresh
|
|
238
|
+
- Token validation
|
|
239
|
+
|
|
240
|
+
### 2. Request Security
|
|
241
|
+
- HTTPS only
|
|
242
|
+
- Request signing
|
|
243
|
+
- Parameter validation
|
|
244
|
+
|
|
245
|
+
### 3. Error Information
|
|
246
|
+
- Sanitized error messages
|
|
247
|
+
- No sensitive data in logs
|
|
248
|
+
- Secure debugging
|
|
249
|
+
|
|
250
|
+
## Deployment
|
|
251
|
+
|
|
252
|
+
### 1. Package Distribution
|
|
253
|
+
- NPM package
|
|
254
|
+
- TypeScript declarations
|
|
255
|
+
- Source maps
|
|
256
|
+
|
|
257
|
+
### 2. Versioning
|
|
258
|
+
- Semantic versioning
|
|
259
|
+
- Backward compatibility
|
|
260
|
+
- Migration guides
|
|
261
|
+
|
|
262
|
+
### 3. Documentation
|
|
263
|
+
- API documentation
|
|
264
|
+
- Usage examples
|
|
265
|
+
- Migration guides
|
package/README.md
CHANGED
|
@@ -25,9 +25,17 @@ A comprehensive TypeScript/JavaScript SDK for Zalo APIs, providing easy-to-use i
|
|
|
25
25
|
## Installation
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
|
-
npm install redai-zalo-sdk
|
|
28
|
+
npm install @warriorteam/redai-zalo-sdk
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
## 📚 Documentation
|
|
32
|
+
|
|
33
|
+
- **[Webhook Events Guide](./docs/WEBHOOK_EVENTS.md)** - Complete guide for all 70+ webhook event types
|
|
34
|
+
- **[Architecture Overview](./ARCHITECTURE.md)** - SDK architecture and design patterns
|
|
35
|
+
- **[Services Added](./SERVICES_ADDED.md)** - Detailed breakdown of all services and features
|
|
36
|
+
- **[Group Management](./docs/GROUP_MANAGEMENT.md)** - Group messaging and management
|
|
37
|
+
- **[Article Management](./docs/ARTICLE_MANAGEMENT.md)** - Content and article management
|
|
38
|
+
|
|
31
39
|
## Development
|
|
32
40
|
|
|
33
41
|
### Build from source
|
|
@@ -52,7 +60,7 @@ npm run dev
|
|
|
52
60
|
### Initialize the SDK
|
|
53
61
|
|
|
54
62
|
```typescript
|
|
55
|
-
import { ZaloSDK } from "redai-zalo-sdk";
|
|
63
|
+
import { ZaloSDK } from "@warriorteam/redai-zalo-sdk";
|
|
56
64
|
|
|
57
65
|
const zalo = new ZaloSDK({
|
|
58
66
|
appId: "your-app-id",
|
|
@@ -430,6 +438,55 @@ const friends = await zalo.social.getFriendsList(accessToken);
|
|
|
430
438
|
await zalo.social.postToTimeline(accessToken, "Hello from Zalo SDK!");
|
|
431
439
|
```
|
|
432
440
|
|
|
441
|
+
## 🔔 Webhook Events
|
|
442
|
+
|
|
443
|
+
The SDK provides comprehensive webhook event handling with **70+ event types**:
|
|
444
|
+
|
|
445
|
+
```typescript
|
|
446
|
+
import { WebhookHandlers } from "@warriorteam/redai-zalo-sdk";
|
|
447
|
+
|
|
448
|
+
const handlers: WebhookHandlers = {
|
|
449
|
+
// User message events
|
|
450
|
+
user_send_text: async (event) => {
|
|
451
|
+
console.log("User sent:", event.message.text);
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
// Group events
|
|
455
|
+
user_send_group_audio: async (event) => {
|
|
456
|
+
console.log("Audio in group:", event.message.attachments[0].payload.url);
|
|
457
|
+
},
|
|
458
|
+
|
|
459
|
+
// ZNS events
|
|
460
|
+
change_template_status: async (event) => {
|
|
461
|
+
console.log("Template status:", event.status);
|
|
462
|
+
},
|
|
463
|
+
|
|
464
|
+
// Widget events
|
|
465
|
+
widget_interaction_accepted: async (event) => {
|
|
466
|
+
console.log("Widget accepted:", event.data.user_external_id);
|
|
467
|
+
},
|
|
468
|
+
|
|
469
|
+
// System events
|
|
470
|
+
permission_revoked: async (event) => {
|
|
471
|
+
console.log("Permission revoked by:", event.data.action_by);
|
|
472
|
+
},
|
|
473
|
+
};
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Event Categories:
|
|
477
|
+
|
|
478
|
+
- **User Message Events** (11 types): text, image, video, audio, file, sticker, gif, location, link, business card, reaction
|
|
479
|
+
- **OA Message Events** (11 types): All message types + anonymous + template
|
|
480
|
+
- **Group Events** (22 types): Create, join, admin, leave + all message types for both user and OA
|
|
481
|
+
- **ZNS Events** (8 types): Quota, template, journey, delivery, status changes
|
|
482
|
+
- **Widget Events** (2 types): Interaction accepted, sync failures
|
|
483
|
+
- **System Events** (4 types): Permission, extension, user info, tags
|
|
484
|
+
- **Call Events** (2 types): OA call user, user call OA
|
|
485
|
+
- **Anonymous Events** (4 types): Anonymous chat support
|
|
486
|
+
- **Shop Events** (1 type): Order management
|
|
487
|
+
|
|
488
|
+
For complete webhook documentation, see [Webhook Events Guide](./docs/WEBHOOK_EVENTS.md).
|
|
489
|
+
|
|
433
490
|
## Changelog
|
|
434
491
|
|
|
435
492
|
### v1.0.0
|