@vvlad1973/telegram-bot-client 1.2.0 → 2.0.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.
- package/.claude/settings.local.json +7 -0
- package/README.md +300 -273
- package/dist/agents/LongPollingManager.d.ts +5 -6
- package/dist/agents/LongPollingManager.js +48 -46
- package/dist/agents/LongPollingManager.js.map +1 -1
- package/dist/agents/types/polling-manager.types.d.ts +4 -4
- package/dist/agents/workers/WorkerClientFactory.js +3 -3
- package/dist/agents/workers/WorkerClientFactory.js.map +1 -1
- package/dist/agents/workers/long-polling-worker-test.js +2 -1
- package/dist/agents/workers/long-polling-worker-test.js.map +1 -1
- package/dist/api/BaseTelegramApi.generated.js +481 -0
- package/dist/api/BaseTelegramApi.generated.js.map +1 -1
- package/dist/client/TelegramBotClient.d.ts +15 -4
- package/dist/client/TelegramBotClient.js +17 -4
- package/dist/client/TelegramBotClient.js.map +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/managers/PollingIntegrationManager.d.ts +160 -0
- package/dist/client/managers/PollingIntegrationManager.js +274 -0
- package/dist/client/managers/PollingIntegrationManager.js.map +1 -0
- package/dist/client/managers/RouteConfigManager.d.ts +186 -0
- package/dist/client/managers/RouteConfigManager.js +395 -0
- package/dist/client/managers/RouteConfigManager.js.map +1 -0
- package/dist/client/managers/RouteLifecycleManager.d.ts +146 -0
- package/dist/client/managers/RouteLifecycleManager.js +402 -0
- package/dist/client/managers/RouteLifecycleManager.js.map +1 -0
- package/dist/client/managers/index.d.ts +11 -0
- package/dist/client/managers/index.js +10 -0
- package/dist/client/managers/index.js.map +1 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +2 -0
- package/dist/helpers/index.js.map +1 -1
- package/dist/helpers/logger-helpers.d.ts +2 -5
- package/dist/helpers/logger-helpers.js +2 -10
- package/dist/helpers/logger-helpers.js.map +1 -1
- package/dist/helpers/logging-decorators.d.ts +43 -0
- package/dist/helpers/logging-decorators.js +156 -0
- package/dist/helpers/logging-decorators.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/queue/InMemoryQueueProvider.d.ts +2 -2
- package/dist/queue/InMemoryQueueProvider.js +2 -2
- package/dist/queue/InMemoryQueueProvider.js.map +1 -1
- package/dist/transport/TelegramHttpClient.d.ts +2 -2
- package/dist/transport/TelegramHttpClient.js +2 -2
- package/dist/transport/TelegramHttpClient.js.map +1 -1
- package/dist/transport/TelegramHttpTransport.d.ts +2 -2
- package/dist/transport/TelegramHttpTransport.js +3 -4
- package/dist/transport/TelegramHttpTransport.js.map +1 -1
- package/dist/transport/TelegramTransport.d.ts +2 -2
- package/dist/transport/TelegramTransport.js +23 -7
- package/dist/transport/TelegramTransport.js.map +1 -1
- package/dist/transport/TokensManager.d.ts +2 -2
- package/dist/transport/TokensManager.js +6 -6
- package/dist/transport/TokensManager.js.map +1 -1
- package/dist/transport/index.d.ts +0 -1
- package/dist/transport/index.js +1 -3
- package/dist/transport/index.js.map +1 -1
- package/dist/types/logger.types.d.ts +3 -9
- package/dist/types/route.types.d.ts +110 -0
- package/dist/types/route.types.js +19 -0
- package/dist/types/route.types.js.map +1 -0
- package/dist/types/transport.types.d.ts +3 -1
- package/package.json +1 -1
- package/test_output.txt +256 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
TypeScript library for Telegram Bot API with type-safe methods, automatic validation, high-level bot framework, and Native Fetch HTTP client.
|
|
4
4
|
|
|
5
|
-
**Version:** 1.
|
|
5
|
+
**Version:** 1.4.0
|
|
6
6
|
**Supported Telegram Bot API Version:** 9.2
|
|
7
7
|
**Node.js:** >=18.0.0
|
|
8
8
|
|
|
@@ -19,25 +19,18 @@ TypeScript library for Telegram Bot API with type-safe methods, automatic valida
|
|
|
19
19
|
- Comprehensive error handling with typed errors
|
|
20
20
|
- Full TypeScript support
|
|
21
21
|
|
|
22
|
-
### High-Level Bot Framework
|
|
22
|
+
### High-Level Bot Framework
|
|
23
23
|
|
|
24
|
-
- **TelegramBotClient** -
|
|
24
|
+
- **TelegramBotClient** - Complete bot framework with transport-based architecture
|
|
25
|
+
- **Built-in Long Polling** - Integrated multi-route polling with automatic management
|
|
26
|
+
- **Route Configuration** - Multi-token support with per-route settings
|
|
25
27
|
- **Event System** - Subscribe to specific update types with flexible filters
|
|
26
28
|
- **Filter System** - Advanced filtering with RegExp support for all update fields
|
|
27
29
|
- **Middleware** - Chain of responsibility pattern for pre-processing
|
|
28
30
|
- **Wrapper Classes** - Enhanced Message and CallbackQuery objects with helper methods
|
|
29
31
|
- **Keyboard Builders** - Fluent API for building inline and reply keyboards
|
|
30
32
|
- **Bot Commands Builder** - Manage bot commands easily
|
|
31
|
-
|
|
32
|
-
### Long Polling Agent (New!)
|
|
33
|
-
|
|
34
|
-
- **LongPollingManager** - Multi-token long polling with worker threads
|
|
35
|
-
- Independent workers for each bot token
|
|
36
|
-
- Automatic restart on failures with exponential backoff
|
|
37
|
-
- Offset persistence per token
|
|
38
|
-
- Event-driven architecture
|
|
39
|
-
- Individual worker management
|
|
40
|
-
- Webhook management (disable/restore)
|
|
33
|
+
- **Automatic TRACE Logging** - All API methods logged via decorators
|
|
41
34
|
|
|
42
35
|
### Transport Layer
|
|
43
36
|
|
|
@@ -53,99 +46,115 @@ npm install @vvlad1973/telegram-bot-client
|
|
|
53
46
|
|
|
54
47
|
## Quick Start
|
|
55
48
|
|
|
56
|
-
###
|
|
49
|
+
### Simple Bot Example
|
|
57
50
|
|
|
58
51
|
```typescript
|
|
59
|
-
import {
|
|
60
|
-
TelegramBotClient,
|
|
61
|
-
TelegramHttpClient,
|
|
62
|
-
type Update,
|
|
63
|
-
} from '@vvlad1973/telegram-bot-client';
|
|
64
|
-
|
|
65
|
-
class MyBot extends TelegramBotClient {
|
|
66
|
-
private httpClient: TelegramHttpClient;
|
|
67
|
-
|
|
68
|
-
constructor(token: string) {
|
|
69
|
-
super();
|
|
70
|
-
this.httpClient = new TelegramHttpClient({ token });
|
|
71
|
-
this.setupHandlers();
|
|
72
|
-
}
|
|
52
|
+
import { TelegramBotClient, TelegramTransport, RouteMode } from '@vvlad1973/telegram-bot-client';
|
|
73
53
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
54
|
+
// Create transport
|
|
55
|
+
const transport = new TelegramTransport({
|
|
56
|
+
tokens: { default: process.env.BOT_TOKEN }
|
|
57
|
+
});
|
|
78
58
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
59
|
+
// Create client with built-in long polling
|
|
60
|
+
const bot = new TelegramBotClient({
|
|
61
|
+
transport,
|
|
62
|
+
routes: [
|
|
63
|
+
{
|
|
64
|
+
id: 'default',
|
|
65
|
+
token: process.env.BOT_TOKEN,
|
|
66
|
+
mode: RouteMode.LongPolling,
|
|
67
|
+
longPolling: { timeout: 30, limit: 100 }
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
defaultRouteId: 'default'
|
|
71
|
+
});
|
|
95
72
|
|
|
96
|
-
|
|
73
|
+
// Handle messages
|
|
74
|
+
bot.on('message', async (message) => {
|
|
75
|
+
await bot.sendMessage({
|
|
76
|
+
chat_id: message.chat.id,
|
|
77
|
+
text: `You said: ${message.text}`
|
|
78
|
+
});
|
|
79
|
+
});
|
|
97
80
|
|
|
98
|
-
//
|
|
99
|
-
await bot.
|
|
81
|
+
// Start long polling
|
|
82
|
+
await bot.startLongPolling();
|
|
83
|
+
console.log('Bot is running...');
|
|
100
84
|
```
|
|
101
85
|
|
|
102
|
-
###
|
|
86
|
+
### Multi-Token Bot Example
|
|
103
87
|
|
|
104
88
|
```typescript
|
|
105
|
-
import {
|
|
89
|
+
import { TelegramBotClient, TelegramTransport, RouteMode } from '@vvlad1973/telegram-bot-client';
|
|
106
90
|
|
|
107
|
-
const
|
|
108
|
-
|
|
91
|
+
const transport = new TelegramTransport({
|
|
92
|
+
tokens: {
|
|
93
|
+
bot1: process.env.BOT1_TOKEN,
|
|
94
|
+
bot2: process.env.BOT2_TOKEN
|
|
95
|
+
}
|
|
96
|
+
});
|
|
109
97
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
98
|
+
const bot = new TelegramBotClient({
|
|
99
|
+
transport,
|
|
100
|
+
routes: [
|
|
101
|
+
{
|
|
102
|
+
id: 'bot1',
|
|
103
|
+
token: process.env.BOT1_TOKEN,
|
|
104
|
+
mode: RouteMode.LongPolling
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: 'bot2',
|
|
108
|
+
token: process.env.BOT2_TOKEN,
|
|
109
|
+
mode: RouteMode.LongPolling
|
|
110
|
+
}
|
|
111
|
+
]
|
|
113
112
|
});
|
|
113
|
+
|
|
114
|
+
// Handle messages from all bots
|
|
115
|
+
bot.on('message', async (message) => {
|
|
116
|
+
console.log('Message from:', message.chat.id);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Start polling for all routes
|
|
120
|
+
await bot.startLongPolling();
|
|
114
121
|
```
|
|
115
122
|
|
|
116
123
|
## High-Level Features
|
|
117
124
|
|
|
118
125
|
### TelegramBotClient
|
|
119
126
|
|
|
120
|
-
|
|
127
|
+
Concrete class providing complete bot framework:
|
|
121
128
|
|
|
122
129
|
```typescript
|
|
123
|
-
|
|
124
|
-
// Implement callApi method
|
|
125
|
-
protected async callApi(method: string, params?: any) { ... }
|
|
126
|
-
|
|
127
|
-
// Setup event handlers
|
|
128
|
-
private setupHandlers() {
|
|
129
|
-
// Commands
|
|
130
|
-
this.on('start', (params, wrapper) => { ... });
|
|
131
|
-
this.on('help', (params, wrapper) => { ... });
|
|
132
|
-
|
|
133
|
-
// Messages
|
|
134
|
-
this.on('message.text', (wrapper) => { ... });
|
|
135
|
-
this.on('message.photo', (wrapper) => { ... });
|
|
136
|
-
|
|
137
|
-
// Callbacks
|
|
138
|
-
this.on('callback_query', (wrapper) => { ... });
|
|
139
|
-
}
|
|
130
|
+
import { TelegramBotClient, TelegramTransport } from '@vvlad1973/telegram-bot-client';
|
|
140
131
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
132
|
+
// Create transport
|
|
133
|
+
const transport = new TelegramTransport({
|
|
134
|
+
tokens: { default: process.env.BOT_TOKEN }
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Create client
|
|
138
|
+
const bot = new TelegramBotClient({ transport });
|
|
139
|
+
|
|
140
|
+
// Setup event handlers
|
|
141
|
+
bot.on('message.text', (message) => {
|
|
142
|
+
console.log('Text message:', message.text);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
bot.on('message.photo', (message) => {
|
|
146
|
+
console.log('Photo received');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
bot.on('callback_query', (query) => {
|
|
150
|
+
console.log('Callback query:', query.data);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Setup middleware
|
|
154
|
+
bot.use((update, next) => {
|
|
155
|
+
console.log('Processing update:', update.update_id);
|
|
156
|
+
next();
|
|
157
|
+
});
|
|
149
158
|
```
|
|
150
159
|
|
|
151
160
|
### Event System
|
|
@@ -154,180 +163,109 @@ Subscribe to specific update types:
|
|
|
154
163
|
|
|
155
164
|
```typescript
|
|
156
165
|
// Text messages
|
|
157
|
-
bot.on('message
|
|
158
|
-
console.log('
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// Commands
|
|
162
|
-
bot.on('start', (params, wrapper) => {
|
|
163
|
-
wrapper.replyMessage('Hello!');
|
|
166
|
+
bot.on('message', (message) => {
|
|
167
|
+
console.log('Message:', message.text);
|
|
164
168
|
});
|
|
165
169
|
|
|
166
|
-
//
|
|
167
|
-
bot.on('
|
|
168
|
-
|
|
170
|
+
// Callback queries
|
|
171
|
+
bot.on('callback_query', (query) => {
|
|
172
|
+
console.log('Callback data:', query.data);
|
|
169
173
|
});
|
|
170
174
|
|
|
171
175
|
// All updates
|
|
172
|
-
bot.on('*', (
|
|
173
|
-
console.log('
|
|
176
|
+
bot.on('*', (update) => {
|
|
177
|
+
console.log('Update:', update.update_id);
|
|
174
178
|
});
|
|
175
179
|
```
|
|
176
180
|
|
|
177
|
-
## Long Polling
|
|
181
|
+
## Built-in Long Polling
|
|
178
182
|
|
|
179
|
-
|
|
183
|
+
TelegramBotClient has integrated long polling support with multi-route management:
|
|
180
184
|
|
|
181
185
|
```typescript
|
|
182
|
-
import {
|
|
183
|
-
LongPollingManager,
|
|
184
|
-
TelegramTransport,
|
|
185
|
-
TelegramBotClient,
|
|
186
|
-
TokensManager,
|
|
187
|
-
} from '@vvlad1973/telegram-bot-client';
|
|
186
|
+
import { TelegramBotClient, TelegramTransport, RouteMode } from '@vvlad1973/telegram-bot-client';
|
|
188
187
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const transport = new TelegramTransport({ tokens: tokensManager });
|
|
196
|
-
const client = new MyBotClient(transport);
|
|
197
|
-
|
|
198
|
-
// Create long polling manager
|
|
199
|
-
const manager = new LongPollingManager({
|
|
200
|
-
tokensManager,
|
|
188
|
+
const transport = new TelegramTransport({
|
|
189
|
+
tokens: {
|
|
190
|
+
bot1: process.env.BOT1_TOKEN,
|
|
191
|
+
bot2: process.env.BOT2_TOKEN
|
|
192
|
+
}
|
|
193
|
+
});
|
|
201
194
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
{
|
|
195
|
+
const bot = new TelegramBotClient({
|
|
196
|
+
transport,
|
|
197
|
+
routes: [
|
|
198
|
+
{
|
|
199
|
+
id: 'bot1',
|
|
200
|
+
token: process.env.BOT1_TOKEN,
|
|
201
|
+
mode: RouteMode.LongPolling,
|
|
202
|
+
longPolling: {
|
|
203
|
+
timeout: 30,
|
|
204
|
+
limit: 100,
|
|
205
|
+
allowedUpdates: ['message', 'callback_query']
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
id: 'bot2',
|
|
210
|
+
token: process.env.BOT2_TOKEN,
|
|
211
|
+
mode: RouteMode.Inactive // Manually controlled
|
|
212
|
+
}
|
|
206
213
|
],
|
|
207
|
-
|
|
208
|
-
// Update handler
|
|
209
|
-
onUpdate: async (update, routeId) => {
|
|
210
|
-
await client.processUpdate(update, { routeId });
|
|
211
|
-
},
|
|
212
|
-
|
|
213
|
-
// Error handler
|
|
214
|
-
onError: (error, routeId) => {
|
|
215
|
-
console.error(`[${routeId}] Error:`, error);
|
|
216
|
-
},
|
|
214
|
+
defaultRouteId: 'bot1'
|
|
217
215
|
});
|
|
218
216
|
|
|
219
|
-
//
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
// Get statistics
|
|
223
|
-
const stats = manager.getStats();
|
|
224
|
-
console.log(`Running workers: ${stats.runningWorkers}`);
|
|
225
|
-
|
|
226
|
-
// Individual worker management
|
|
227
|
-
const isRunning = manager.isWorkerRunning('bot1');
|
|
228
|
-
const offset = manager.getOffset('bot1');
|
|
229
|
-
manager.setOffset('bot1', 12345);
|
|
230
|
-
|
|
231
|
-
// Graceful shutdown
|
|
232
|
-
process.on('SIGINT', async () => {
|
|
233
|
-
await manager.stop();
|
|
234
|
-
await transport.close();
|
|
217
|
+
// Handle updates
|
|
218
|
+
bot.on('message', async (message) => {
|
|
219
|
+
console.log('New message:', message.text);
|
|
235
220
|
});
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
**Key Features:**
|
|
239
|
-
|
|
240
|
-
- Each token runs in separate worker thread
|
|
241
|
-
- Automatic restart on crashes (with exponential backoff)
|
|
242
|
-
- Independent offset tracking per token
|
|
243
|
-
- RouteId passed to processUpdate for multi-token handling
|
|
244
|
-
- Webhook management (disable before polling, restore after stop)
|
|
245
|
-
- Event-driven: `started`, `stopped`, `worker-started`, `worker-stopped`, `worker-error`, `update`
|
|
246
221
|
|
|
247
|
-
|
|
222
|
+
// Start long polling for all active routes
|
|
223
|
+
await bot.startLongPolling();
|
|
248
224
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const state = manager.getWorkerState('bot1');
|
|
253
|
-
|
|
254
|
-
// Get and set offsets
|
|
255
|
-
const offset = manager.getOffset('bot1');
|
|
256
|
-
manager.setOffset('bot1', 12345);
|
|
225
|
+
// Route management
|
|
226
|
+
await bot.routeLifecycle.activateRoute('bot2', RouteMode.LongPolling);
|
|
227
|
+
await bot.routeLifecycle.deactivateRoute('bot2');
|
|
257
228
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
await manager.stopWorker('bot2');
|
|
229
|
+
const status = bot.routeLifecycle.getRouteStatus('bot1');
|
|
230
|
+
console.log('Bot1 status:', status);
|
|
261
231
|
|
|
262
|
-
//
|
|
263
|
-
|
|
264
|
-
console.log(`Active: ${stats.runningWorkers}/${stats.totalWorkers}`);
|
|
232
|
+
// Stop long polling
|
|
233
|
+
await bot.stopLongPolling();
|
|
265
234
|
```
|
|
266
235
|
|
|
267
|
-
**
|
|
268
|
-
|
|
269
|
-
```typescript
|
|
270
|
-
manager.on('started', () => console.log('Manager started'));
|
|
271
|
-
manager.on('stopped', () => console.log('Manager stopped'));
|
|
272
|
-
manager.on('worker-started', (tokenId) => console.log(`Worker ${tokenId} started`));
|
|
273
|
-
manager.on('worker-stopped', (tokenId) => console.log(`Worker ${tokenId} stopped`));
|
|
274
|
-
manager.on('worker-error', (tokenId, error) => console.error(`Worker ${tokenId} error:`, error));
|
|
275
|
-
manager.on('update', (update, tokenId) => console.log(`Update from ${tokenId}`));
|
|
276
|
-
```
|
|
236
|
+
**Key Features:**
|
|
277
237
|
|
|
278
|
-
|
|
238
|
+
- Integrated into TelegramBotClient
|
|
239
|
+
- Multi-route support with independent configurations
|
|
240
|
+
- Automatic update processing
|
|
241
|
+
- Route lifecycle management (activate/deactivate)
|
|
242
|
+
- Per-route polling configuration
|
|
243
|
+
- Event-driven architecture
|
|
279
244
|
|
|
280
|
-
###
|
|
245
|
+
### Event Filtering
|
|
281
246
|
|
|
282
|
-
Filter
|
|
247
|
+
Filter events by update types:
|
|
283
248
|
|
|
284
249
|
```typescript
|
|
285
|
-
//
|
|
286
|
-
bot.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
// Filter with RegExp
|
|
294
|
-
bot.onFilter({
|
|
295
|
-
type: 'message.text',
|
|
296
|
-
contents: /^\/start/,
|
|
297
|
-
chat: 'private'
|
|
298
|
-
}, (wrapper) => {
|
|
299
|
-
// Start command in private chat
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
// Admin commands
|
|
303
|
-
bot.onFilter({
|
|
304
|
-
type: 'command',
|
|
305
|
-
contents: /^(ban|kick)$/,
|
|
306
|
-
user: /^(111111|222222)$/
|
|
307
|
-
}, (wrapper) => {
|
|
308
|
-
// Admin-only commands
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
// Language detection
|
|
312
|
-
bot.onFilter({
|
|
313
|
-
type: 'message.text',
|
|
314
|
-
contents: /^[А-Яа-я]+$/
|
|
315
|
-
}, (wrapper) => {
|
|
316
|
-
// Russian text only
|
|
250
|
+
// Listen to specific message types
|
|
251
|
+
bot.on('message', (message) => {
|
|
252
|
+
if (message.text) {
|
|
253
|
+
console.log('Text message:', message.text);
|
|
254
|
+
}
|
|
255
|
+
if (message.photo) {
|
|
256
|
+
console.log('Photo message');
|
|
257
|
+
}
|
|
317
258
|
});
|
|
318
|
-
```
|
|
319
259
|
|
|
320
|
-
|
|
260
|
+
// Handle callback queries
|
|
261
|
+
bot.on('callback_query', (query) => {
|
|
262
|
+
console.log('Callback data:', query.data);
|
|
263
|
+
});
|
|
321
264
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
params?: string | RegExp; // file_id for media, id for callback
|
|
327
|
-
chat?: string | RegExp | number; // Chat ID or 'private'
|
|
328
|
-
chat_type?: string | RegExp; // 'private', 'group', 'supergroup'
|
|
329
|
-
user?: string | RegExp | number; // User ID
|
|
330
|
-
}
|
|
265
|
+
// Handle all update types
|
|
266
|
+
bot.on('*', (update) => {
|
|
267
|
+
console.log('Any update:', update.update_id);
|
|
268
|
+
});
|
|
331
269
|
```
|
|
332
270
|
|
|
333
271
|
### Middleware
|
|
@@ -357,24 +295,44 @@ bot.use(async (update, next) => {
|
|
|
357
295
|
|
|
358
296
|
### Wrapper Classes
|
|
359
297
|
|
|
360
|
-
Enhanced objects with helper methods:
|
|
298
|
+
Enhanced message and callback query objects with helper methods:
|
|
361
299
|
|
|
362
300
|
```typescript
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
wrapper
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
wrapper.
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
wrapper.
|
|
376
|
-
|
|
377
|
-
|
|
301
|
+
import { MessageWrapper, CallbackQueryWrapper } from '@vvlad1973/telegram-bot-client';
|
|
302
|
+
|
|
303
|
+
bot.on('message', async (message) => {
|
|
304
|
+
const wrapper = new MessageWrapper(message, bot);
|
|
305
|
+
|
|
306
|
+
// Reply to the message
|
|
307
|
+
await wrapper.reply('Hello!');
|
|
308
|
+
|
|
309
|
+
// Forward the message
|
|
310
|
+
await wrapper.forward(targetChatId);
|
|
311
|
+
|
|
312
|
+
// Delete the message
|
|
313
|
+
await wrapper.delete();
|
|
314
|
+
|
|
315
|
+
// Check message type
|
|
316
|
+
if (wrapper.isText()) {
|
|
317
|
+
console.log('Text:', wrapper.text);
|
|
318
|
+
}
|
|
319
|
+
if (wrapper.isPhoto()) {
|
|
320
|
+
console.log('Photo message');
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
bot.on('callback_query', async (query) => {
|
|
325
|
+
const wrapper = new CallbackQueryWrapper(query, bot);
|
|
326
|
+
|
|
327
|
+
// Answer the callback query
|
|
328
|
+
await wrapper.answer('Processing...');
|
|
329
|
+
|
|
330
|
+
// Edit the message text
|
|
331
|
+
await wrapper.editText('Updated!');
|
|
332
|
+
|
|
333
|
+
// Delete the message
|
|
334
|
+
await wrapper.deleteMessage();
|
|
335
|
+
});
|
|
378
336
|
```
|
|
379
337
|
|
|
380
338
|
### Keyboard Builders
|
|
@@ -454,15 +412,15 @@ await bot.setMyCommands({ commands });
|
|
|
454
412
|
|
|
455
413
|
**High-Level Client:**
|
|
456
414
|
|
|
457
|
-
- `TelegramBotClient` -
|
|
415
|
+
- `TelegramBotClient` - Complete bot framework with transport, events, and middleware
|
|
458
416
|
- `MessageWrapper` - Enhanced Message with helper methods
|
|
459
417
|
- `CallbackQueryWrapper` - Enhanced CallbackQuery with helper methods
|
|
460
418
|
|
|
461
|
-
**
|
|
419
|
+
**Client Managers:**
|
|
462
420
|
|
|
463
|
-
- `
|
|
464
|
-
- `
|
|
465
|
-
- `
|
|
421
|
+
- `RouteConfigManager` - Route configuration management
|
|
422
|
+
- `RouteLifecycleManager` - Route activation and lifecycle
|
|
423
|
+
- `PollingIntegrationManager` - Long polling integration
|
|
466
424
|
|
|
467
425
|
**Builders:**
|
|
468
426
|
|
|
@@ -483,6 +441,11 @@ await bot.setMyCommands({ commands });
|
|
|
483
441
|
- `downloadFileBuffer()` - Download any file as Buffer
|
|
484
442
|
- `downloadFileStream()` - Download any file as Stream
|
|
485
443
|
|
|
444
|
+
**Decorators:**
|
|
445
|
+
|
|
446
|
+
- `@LogMethod` - Automatic method entry/exit logging with parameter sanitization
|
|
447
|
+
- `@LogEntry` - Method entry-only logging
|
|
448
|
+
|
|
486
449
|
**Constants:**
|
|
487
450
|
|
|
488
451
|
- `DiceEmoji` - Emoji for sendDice method
|
|
@@ -675,44 +638,98 @@ Run examples:
|
|
|
675
638
|
BOT_TOKEN=your_token ts-node examples/simple-bot.ts
|
|
676
639
|
```
|
|
677
640
|
|
|
678
|
-
##
|
|
641
|
+
## Logger Integration
|
|
679
642
|
|
|
680
|
-
|
|
643
|
+
The library supports comprehensive logging with automatic TRACE logging for all API methods:
|
|
681
644
|
|
|
682
645
|
```typescript
|
|
683
|
-
import {
|
|
646
|
+
import { TelegramBotClient, TelegramTransport, LoggerTree, LoggerBinder } from '@vvlad1973/telegram-bot-client';
|
|
647
|
+
|
|
648
|
+
// Create logger tree
|
|
649
|
+
const loggerTree = new LoggerTree({
|
|
650
|
+
level: 'debug',
|
|
651
|
+
prettyPrint: true
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// Create logger binder
|
|
655
|
+
const loggerBinder = new LoggerBinder(loggerTree);
|
|
656
|
+
|
|
657
|
+
// Create transport with logger
|
|
658
|
+
const transport = new TelegramTransport({
|
|
659
|
+
tokens: { default: process.env.BOT_TOKEN },
|
|
660
|
+
loggerBinder,
|
|
661
|
+
loggerName: 'TelegramBot'
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
// Create client with logger
|
|
665
|
+
const bot = new TelegramBotClient({
|
|
666
|
+
transport,
|
|
667
|
+
loggerBinder,
|
|
668
|
+
loggerName: 'TelegramBotClient'
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
// All API method calls are automatically logged at TRACE level
|
|
672
|
+
await bot.sendMessage({
|
|
673
|
+
chat_id: 123,
|
|
674
|
+
text: 'This will be logged'
|
|
675
|
+
});
|
|
676
|
+
```
|
|
684
677
|
|
|
685
|
-
|
|
686
|
-
|
|
678
|
+
**Automatic Method Logging:**
|
|
679
|
+
|
|
680
|
+
All API methods are decorated with `@LogMethod` decorator that provides:
|
|
681
|
+
|
|
682
|
+
- Method entry logging with sanitized parameters (tokens are masked)
|
|
683
|
+
- Method exit logging for sync operations
|
|
684
|
+
- Async method completion logging
|
|
685
|
+
- Complete visibility at TRACE level
|
|
686
|
+
|
|
687
|
+
**Log Levels:**
|
|
688
|
+
|
|
689
|
+
- **trace**: Method entry/exit, request/response details
|
|
690
|
+
- **debug**: Update processing, middleware execution, event emission
|
|
691
|
+
- **info**: Lifecycle events, long polling start/stop, route management
|
|
692
|
+
- **warn**: Recoverable issues, rate limiting, missing tokens
|
|
693
|
+
- **error**: API errors, failed retries, critical failures
|
|
694
|
+
|
|
695
|
+
## Low-Level Transport Usage
|
|
696
|
+
|
|
697
|
+
For custom implementations, you can use TelegramTransport directly:
|
|
698
|
+
|
|
699
|
+
```typescript
|
|
700
|
+
import { TelegramTransport, InMemoryQueueProvider } from '@vvlad1973/telegram-bot-client';
|
|
701
|
+
|
|
702
|
+
// Create queue provider (optional)
|
|
703
|
+
const queueProvider = new InMemoryQueueProvider({
|
|
704
|
+
globalLimit: 30,
|
|
705
|
+
queueLimit: 1,
|
|
706
|
+
concurrency: 1
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
// Create transport
|
|
710
|
+
const transport = new TelegramTransport({
|
|
711
|
+
tokens: { default: process.env.BOT_TOKEN },
|
|
712
|
+
queueProvider, // Optional
|
|
687
713
|
httpClientOptions: {
|
|
688
714
|
timeout: 30000,
|
|
689
715
|
retries: 3,
|
|
690
716
|
exponentialBackoff: true
|
|
691
|
-
},
|
|
692
|
-
methodPriorities: {
|
|
693
|
-
high: 10,
|
|
694
|
-
medium: 5,
|
|
695
|
-
low: 2
|
|
696
717
|
}
|
|
697
718
|
});
|
|
698
719
|
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
// Send message
|
|
702
|
-
await bot.sendMessage({
|
|
720
|
+
// Call API methods directly
|
|
721
|
+
await transport.sendMessage({
|
|
703
722
|
chat_id: 123,
|
|
704
723
|
text: 'Hello!'
|
|
705
724
|
});
|
|
706
725
|
|
|
707
|
-
|
|
708
|
-
await bot.sendPhoto({
|
|
726
|
+
await transport.sendPhoto({
|
|
709
727
|
chat_id: 123,
|
|
710
728
|
photo: 'https://example.com/photo.jpg',
|
|
711
|
-
caption: 'Photo
|
|
729
|
+
caption: 'Photo'
|
|
712
730
|
});
|
|
713
731
|
|
|
714
|
-
|
|
715
|
-
const updates = await bot.getUpdates({
|
|
732
|
+
const updates = await transport.getUpdates({
|
|
716
733
|
offset: 0,
|
|
717
734
|
timeout: 30
|
|
718
735
|
});
|
|
@@ -722,18 +739,28 @@ const updates = await bot.getUpdates({
|
|
|
722
739
|
|
|
723
740
|
```text
|
|
724
741
|
High-Level Framework:
|
|
725
|
-
TelegramBotClient (
|
|
742
|
+
TelegramBotClient (concrete)
|
|
743
|
+
├── Transport Layer (TelegramTransport)
|
|
744
|
+
├── Route Management
|
|
745
|
+
│ ├── RouteConfigManager (Configuration)
|
|
746
|
+
│ ├── RouteLifecycleManager (Activation/deactivation)
|
|
747
|
+
│ └── PollingIntegrationManager (Long polling)
|
|
726
748
|
├── Event System (EventEmitter-based)
|
|
727
|
-
├── Filter System (JSON filters with RegExp)
|
|
728
749
|
├── Middleware (Chain of responsibility)
|
|
729
750
|
└── Wrappers (MessageWrapper, CallbackQueryWrapper)
|
|
730
751
|
|
|
731
752
|
Transport Layer:
|
|
732
|
-
|
|
733
|
-
├── BaseTelegramApi (Generated API methods)
|
|
753
|
+
TelegramTransport
|
|
734
754
|
├── TelegramHttpClient (Native Fetch)
|
|
735
|
-
├── TokensManager (Multi-
|
|
736
|
-
|
|
755
|
+
├── TokensManager (Multi-token support)
|
|
756
|
+
├── QueueProvider (Optional, for rate limiting)
|
|
757
|
+
└── BaseTelegramApi (Generated, 100+ API methods with @LogMethod decorators)
|
|
758
|
+
|
|
759
|
+
Logging System:
|
|
760
|
+
@LogMethod Decorator
|
|
761
|
+
├── Automatic method entry/exit logging
|
|
762
|
+
├── Parameter sanitization (token masking)
|
|
763
|
+
└── TRACE level logging for all API calls
|
|
737
764
|
```
|
|
738
765
|
|
|
739
766
|
## Code Generation
|