telegram-botbuilder 1.6.5 → 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/Changelog.md +81 -0
- package/MIGRATION.md +453 -0
- package/README.md +403 -0
- package/lib/actions/callback_action.d.ts +14 -0
- package/lib/actions/callback_action.d.ts.map +1 -0
- package/lib/actions/callback_action.js +25 -0
- package/lib/actions/callback_action.js.map +1 -0
- package/lib/actions/change_dialog.d.ts +10 -0
- package/lib/actions/change_dialog.d.ts.map +1 -0
- package/lib/actions/change_dialog.js +17 -0
- package/lib/actions/change_dialog.js.map +1 -0
- package/lib/actions/control_flow.d.ts +26 -0
- package/lib/actions/control_flow.d.ts.map +1 -0
- package/lib/actions/control_flow.js +61 -0
- package/lib/actions/control_flow.js.map +1 -0
- package/lib/actions/index.d.ts +6 -0
- package/lib/actions/index.d.ts.map +1 -0
- package/lib/actions/index.js +11 -0
- package/lib/actions/index.js.map +1 -0
- package/lib/actions/send_message.d.ts +16 -0
- package/lib/actions/send_message.d.ts.map +1 -0
- package/lib/actions/send_message.js +32 -0
- package/lib/actions/send_message.js.map +1 -0
- package/lib/actions/wait_for_input.d.ts +14 -0
- package/lib/actions/wait_for_input.d.ts.map +1 -0
- package/lib/actions/wait_for_input.js +54 -0
- package/lib/actions/wait_for_input.js.map +1 -0
- package/lib/core/bot_builder.d.ts +145 -0
- package/lib/core/bot_builder.d.ts.map +1 -0
- package/lib/core/bot_builder.js +678 -0
- package/lib/core/bot_builder.js.map +1 -0
- package/lib/core/button_registry.d.ts +39 -0
- package/lib/core/button_registry.d.ts.map +1 -0
- package/lib/core/button_registry.js +84 -0
- package/lib/core/button_registry.js.map +1 -0
- package/lib/core/dialog_manager.d.ts +71 -0
- package/lib/core/dialog_manager.d.ts.map +1 -0
- package/lib/core/dialog_manager.js +146 -0
- package/lib/core/dialog_manager.js.map +1 -0
- package/lib/core/index.d.ts +8 -0
- package/lib/core/index.d.ts.map +1 -0
- package/lib/core/index.js +8 -0
- package/lib/core/index.js.map +1 -0
- package/lib/core/input_manager.d.ts +49 -0
- package/lib/core/input_manager.d.ts.map +1 -0
- package/lib/core/input_manager.js +129 -0
- package/lib/core/input_manager.js.map +1 -0
- package/lib/core/keyboard_builder.d.ts +35 -0
- package/lib/core/keyboard_builder.d.ts.map +1 -0
- package/lib/core/keyboard_builder.js +87 -0
- package/lib/core/keyboard_builder.js.map +1 -0
- package/lib/core/middleware_chain.d.ts +25 -0
- package/lib/core/middleware_chain.d.ts.map +1 -0
- package/lib/core/middleware_chain.js +54 -0
- package/lib/core/middleware_chain.js.map +1 -0
- package/lib/core/schema_compiler.d.ts +25 -0
- package/lib/core/schema_compiler.d.ts.map +1 -0
- package/lib/core/schema_compiler.js +65 -0
- package/lib/core/schema_compiler.js.map +1 -0
- package/lib/errors/bot_error.d.ts +7 -0
- package/lib/errors/bot_error.d.ts.map +1 -0
- package/lib/errors/bot_error.js +17 -0
- package/lib/errors/bot_error.js.map +1 -0
- package/lib/errors/dialog_error.d.ts +12 -0
- package/lib/errors/dialog_error.d.ts.map +1 -0
- package/lib/errors/dialog_error.js +22 -0
- package/lib/errors/dialog_error.js.map +1 -0
- package/lib/errors/index.d.ts +5 -0
- package/lib/errors/index.d.ts.map +1 -0
- package/lib/errors/index.js +5 -0
- package/lib/errors/index.js.map +1 -0
- package/lib/errors/telegram_error.d.ts +12 -0
- package/lib/errors/telegram_error.d.ts.map +1 -0
- package/lib/errors/telegram_error.js +39 -0
- package/lib/errors/telegram_error.js.map +1 -0
- package/lib/errors/validation_error.d.ts +19 -0
- package/lib/errors/validation_error.d.ts.map +1 -0
- package/lib/errors/validation_error.js +35 -0
- package/lib/errors/validation_error.js.map +1 -0
- package/lib/index.d.ts +6 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +9 -18
- package/lib/index.js.map +1 -1
- package/lib/types/action.d.ts +58 -0
- package/lib/types/action.d.ts.map +1 -0
- package/lib/types/action.js +2 -0
- package/lib/types/action.js.map +1 -0
- package/lib/types/config.d.ts +36 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +12 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/dialog.d.ts +39 -0
- package/lib/types/dialog.d.ts.map +1 -0
- package/lib/types/dialog.js +2 -0
- package/lib/types/dialog.js.map +1 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +3 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/internal.d.ts +58 -0
- package/lib/types/internal.d.ts.map +1 -0
- package/lib/types/internal.js +11 -0
- package/lib/types/internal.js.map +1 -0
- package/lib/types/keyboard.d.ts +41 -0
- package/lib/types/keyboard.d.ts.map +1 -0
- package/lib/types/keyboard.js +2 -0
- package/lib/types/keyboard.js.map +1 -0
- package/lib/types/middleware.d.ts +24 -0
- package/lib/types/middleware.d.ts.map +1 -0
- package/lib/types/middleware.js +2 -0
- package/lib/types/middleware.js.map +1 -0
- package/lib/types/schema.d.ts +17 -0
- package/lib/types/schema.d.ts.map +1 -0
- package/lib/types/schema.js +2 -0
- package/lib/types/schema.js.map +1 -0
- package/lib/utils/constants.d.ts +33 -0
- package/lib/utils/constants.d.ts.map +1 -0
- package/lib/utils/constants.js +33 -0
- package/lib/utils/constants.js.map +1 -0
- package/lib/utils/deep_copy.d.ts +6 -0
- package/lib/utils/deep_copy.d.ts.map +1 -0
- package/lib/utils/deep_copy.js +45 -0
- package/lib/utils/deep_copy.js.map +1 -0
- package/lib/utils/hash.d.ts +17 -0
- package/lib/utils/hash.d.ts.map +1 -0
- package/lib/utils/hash.js +50 -0
- package/lib/utils/hash.js.map +1 -0
- package/lib/utils/index.d.ts +7 -0
- package/lib/utils/index.d.ts.map +1 -0
- package/lib/utils/index.js +7 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/logger.d.ts +17 -0
- package/lib/utils/logger.d.ts.map +1 -0
- package/lib/utils/logger.js +91 -0
- package/lib/utils/logger.js.map +1 -0
- package/lib/utils/resolvers.d.ts +29 -0
- package/lib/utils/resolvers.d.ts.map +1 -0
- package/lib/utils/resolvers.js +60 -0
- package/lib/utils/resolvers.js.map +1 -0
- package/lib/utils/type_guards.d.ts +22 -0
- package/lib/utils/type_guards.d.ts.map +1 -0
- package/lib/utils/type_guards.js +38 -0
- package/lib/utils/type_guards.js.map +1 -0
- package/lib/validation/index.d.ts +2 -0
- package/lib/validation/index.d.ts.map +1 -0
- package/lib/validation/index.js +2 -0
- package/lib/validation/index.js.map +1 -0
- package/lib/validation/schema_validator.d.ts +11 -0
- package/lib/validation/schema_validator.d.ts.map +1 -0
- package/lib/validation/schema_validator.js +156 -0
- package/lib/validation/schema_validator.js.map +1 -0
- package/package.json +59 -15
- package/lib/bot-service.d.ts +0 -27
- package/lib/bot-service.d.ts.map +0 -1
- package/lib/bot-service.js +0 -326
- package/lib/bot-service.js.map +0 -1
- package/lib/bot-struct.d.ts +0 -58
- package/lib/bot-struct.d.ts.map +0 -1
- package/lib/bot-struct.js +0 -3
- package/lib/bot-struct.js.map +0 -1
- package/readme.md +0 -54
- package/src/bot-service.ts +0 -289
- package/src/bot-struct.ts +0 -59
- package/src/index.ts +0 -2
- package/tsconfig.json +0 -108
package/Changelog.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [2.0.0] - 2024-XX-XX
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **Reply Keyboard Support** - Full support for reply keyboards alongside inline keyboards
|
|
13
|
+
- **Dialog Lifecycle Hooks** - `on_enter` and `on_leave` callbacks for dialogs
|
|
14
|
+
- **New Action Factories**:
|
|
15
|
+
- `go_to(dialog_id)` - Navigate to dialog
|
|
16
|
+
- `go_to_start()` - Reset to start dialog
|
|
17
|
+
- `emit(event, ...args)` - Emit event
|
|
18
|
+
- `call(fn, ...args)` - Call function
|
|
19
|
+
- `call_with_context(fn)` - Call function with full context
|
|
20
|
+
- `wait_for_text(handler, options)` - Wait for text input with validation
|
|
21
|
+
- `wait_for_file(handler, options)` - Wait for file upload
|
|
22
|
+
- `wait_for_photo(handler, options)` - Wait for photo
|
|
23
|
+
- `notify(text, options)` - Send notification message
|
|
24
|
+
- `notify_dynamic(fn, options)` - Send dynamic notification
|
|
25
|
+
- `sequence(...actions)` - Execute actions in sequence
|
|
26
|
+
- `when(condition, then, else)` - Conditional execution
|
|
27
|
+
- `delay(ms, action)` - Delayed execution
|
|
28
|
+
- `when_data(key, predicate, then, else)` - Check user data
|
|
29
|
+
- `set_data(key, value)` - Set user data
|
|
30
|
+
- `delete_data(key)` - Delete user data
|
|
31
|
+
- **Middleware System** - Express-style middleware with `next()` pattern
|
|
32
|
+
- **Schema Validation** - Zod-based validation with detailed error messages
|
|
33
|
+
- **Configurable Logging** - Built-in logger with levels and custom logger support
|
|
34
|
+
- **Input Validation** - Validators for `wait_for_*` actions
|
|
35
|
+
- **Input Timeouts** - Configurable timeouts with messages
|
|
36
|
+
- **Cancel Keywords** - Cancel input waiting with keywords
|
|
37
|
+
- **Error Dialogs** - Global error handling dialog
|
|
38
|
+
- **Fallback Actions** - Handle unmatched messages
|
|
39
|
+
- **User Data Storage** - `set_user_data` / `get_user_data` methods
|
|
40
|
+
- **TypeScript Types** - Comprehensive type exports
|
|
41
|
+
- **Custom Errors** - `BotError`, `DialogError`, `TelegramError`, `ValidationError`
|
|
42
|
+
|
|
43
|
+
### Changed
|
|
44
|
+
|
|
45
|
+
- **BREAKING**: Constructor signature changed to single config object
|
|
46
|
+
- **BREAKING**: Schema structure changed:
|
|
47
|
+
- `start` → `start_dialog`
|
|
48
|
+
- `content` → `dialogs`
|
|
49
|
+
- `buttons` → `inline_buttons`
|
|
50
|
+
- `Command.text` → `Command.name`
|
|
51
|
+
- `Button.mini_app` → `InlineButton.web_app`
|
|
52
|
+
- **BREAKING**: Action factory names changed:
|
|
53
|
+
- `ChangeDialog` → `go_to`
|
|
54
|
+
- `CallbackAction` → `emit` / `call`
|
|
55
|
+
- `WaitForData` → `wait_for_text` / `wait_for_file`
|
|
56
|
+
- **BREAKING**: Middleware signature changed to `(context, next)` pattern
|
|
57
|
+
- **BREAKING**: Bot instance properties renamed:
|
|
58
|
+
- `_bot` → `telegram`
|
|
59
|
+
- `ActionSystem` → `events`
|
|
60
|
+
- **Module System**: Migrated from CommonJS to ESM
|
|
61
|
+
- **Node.js Version**: Now requires Node.js 22+
|
|
62
|
+
- **TypeScript**: Stricter type checking enabled
|
|
63
|
+
|
|
64
|
+
### Removed
|
|
65
|
+
|
|
66
|
+
- **BREAKING**: Removed `BotBuilderMiddleware` type (use `Middleware` instead)
|
|
67
|
+
- **BREAKING**: Removed `MWBreakDown` internal type
|
|
68
|
+
- **BREAKING**: Removed `rfdc` dependency (replaced with built-in deep copy)
|
|
69
|
+
- **BREAKING**: Removed `object-hash` dependency (replaced with crypto-based hashing)
|
|
70
|
+
|
|
71
|
+
### Fixed
|
|
72
|
+
|
|
73
|
+
- Proper error handling for Telegram API errors
|
|
74
|
+
- Memory leak in middleware result caching
|
|
75
|
+
- Button hash collisions with dynamic content
|
|
76
|
+
- Race conditions in input waiting
|
|
77
|
+
|
|
78
|
+
### Security
|
|
79
|
+
|
|
80
|
+
- Input validation to prevent injection attacks
|
|
81
|
+
- Schema validation prevents invalid configurations
|
package/MIGRATION.md
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# Migration Guide: v1.x to v2.0
|
|
2
|
+
|
|
3
|
+
This guide helps you upgrade from telegram-botbuilder v1.x to v2.0.
|
|
4
|
+
|
|
5
|
+
## Breaking Changes
|
|
6
|
+
|
|
7
|
+
### 1. Constructor Signature
|
|
8
|
+
|
|
9
|
+
**Before (v1.x):**
|
|
10
|
+
```typescript
|
|
11
|
+
const bot = new BotBuilder(schema, "BOT_TOKEN", { polling: true });
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
**After (v2.0):**
|
|
15
|
+
```typescript
|
|
16
|
+
const bot = new BotBuilder(schema, {
|
|
17
|
+
token: "BOT_TOKEN",
|
|
18
|
+
telegram_options: { polling: true },
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 2. Schema Structure
|
|
23
|
+
|
|
24
|
+
**Before (v1.x):**
|
|
25
|
+
```typescript
|
|
26
|
+
const schema: Schema = {
|
|
27
|
+
start: "main",
|
|
28
|
+
content: [
|
|
29
|
+
{
|
|
30
|
+
id: "main",
|
|
31
|
+
text: "Hello",
|
|
32
|
+
buttons: [[{ text: "Click", action: ChangeDialog("other") }]],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
commands: [{ text: "help", action: CallbackAction("help") }],
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**After (v2.0):**
|
|
40
|
+
```typescript
|
|
41
|
+
const schema: Schema = {
|
|
42
|
+
start_dialog: "main",
|
|
43
|
+
dialogs: [
|
|
44
|
+
{
|
|
45
|
+
id: "main",
|
|
46
|
+
text: "Hello",
|
|
47
|
+
inline_buttons: [[{ text: "Click", action: go_to("other") }]],
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
commands: [{ name: "help", action: emit("help") }],
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 3. Action Factories
|
|
55
|
+
|
|
56
|
+
| v1.x | v2.0 |
|
|
57
|
+
|------|------|
|
|
58
|
+
| `ChangeDialog("id")` | `go_to("id")` |
|
|
59
|
+
| `CallbackAction("event")` | `emit("event")` |
|
|
60
|
+
| `CallbackAction(fn, ...args)` | `call(fn, ...args)` |
|
|
61
|
+
| `WaitForData("event")` | `wait_for_text("event")` |
|
|
62
|
+
| `WaitForData(fn)` | `wait_for_text(fn)` |
|
|
63
|
+
|
|
64
|
+
### 4. Bot Instance Properties
|
|
65
|
+
|
|
66
|
+
| v1.x | v2.0 |
|
|
67
|
+
|------|------|
|
|
68
|
+
| `bot._bot` | `bot.telegram` |
|
|
69
|
+
| `bot.ActionSystem` | `bot.events` |
|
|
70
|
+
|
|
71
|
+
### 5. Middleware
|
|
72
|
+
|
|
73
|
+
**Before (v1.x):**
|
|
74
|
+
```typescript
|
|
75
|
+
bot.use(async (chat: number, msg: Message | CallbackQuery) => {
|
|
76
|
+
console.log(`Message from ${chat}`);
|
|
77
|
+
return false; // false = continue, true = stop
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**After (v2.0):**
|
|
82
|
+
```typescript
|
|
83
|
+
bot.use(async (ctx, next) => {
|
|
84
|
+
console.log(`Message from ${ctx.chat_id}`);
|
|
85
|
+
await next(); // Call next() to continue, don't call to stop
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 6. Button Types
|
|
90
|
+
|
|
91
|
+
**Before (v1.x):**
|
|
92
|
+
```typescript
|
|
93
|
+
interface Button {
|
|
94
|
+
text?: TextFunc;
|
|
95
|
+
action?: Action[] | Action;
|
|
96
|
+
url?: TextFunc;
|
|
97
|
+
mini_app?: TextFunc;
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**After (v2.0):**
|
|
102
|
+
```typescript
|
|
103
|
+
interface InlineButton {
|
|
104
|
+
text: TextSource;
|
|
105
|
+
action?: Action | Action[];
|
|
106
|
+
url?: TextSource;
|
|
107
|
+
web_app?: TextSource; // Renamed from mini_app
|
|
108
|
+
callback_data?: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface ReplyButton {
|
|
112
|
+
text: TextSource;
|
|
113
|
+
action?: Action | Action[];
|
|
114
|
+
request_contact?: boolean;
|
|
115
|
+
request_location?: boolean;
|
|
116
|
+
request_poll?: { type?: "quiz" | "regular" };
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 7. Dialog Lifecycle Hooks
|
|
121
|
+
|
|
122
|
+
**New in v2.0:**
|
|
123
|
+
```typescript
|
|
124
|
+
{
|
|
125
|
+
id: "my_dialog",
|
|
126
|
+
text: "Welcome!",
|
|
127
|
+
on_enter: async (ctx) => {
|
|
128
|
+
console.log(`User ${ctx.chat_id} entered from ${ctx.other_dialog_id}`);
|
|
129
|
+
},
|
|
130
|
+
on_leave: async (ctx) => {
|
|
131
|
+
console.log(`User ${ctx.chat_id} leaving to ${ctx.other_dialog_id}`);
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 8. Input Waiting
|
|
137
|
+
|
|
138
|
+
**Before (v1.x):**
|
|
139
|
+
```typescript
|
|
140
|
+
// In button action
|
|
141
|
+
WaitForData("input_received")
|
|
142
|
+
|
|
143
|
+
// Listener
|
|
144
|
+
bot.ActionSystem.once("input_received", (chat, text) => {
|
|
145
|
+
console.log(text);
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**After (v2.0):**
|
|
150
|
+
```typescript
|
|
151
|
+
// Option 1: Event-based (similar to v1.x)
|
|
152
|
+
wait_for_text("input_received")
|
|
153
|
+
|
|
154
|
+
bot.events.on("input_received", (chat_id, text) => {
|
|
155
|
+
console.log(text);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Option 2: Callback-based (recommended)
|
|
159
|
+
wait_for_text(async (chat_id, text) => {
|
|
160
|
+
console.log(text);
|
|
161
|
+
}, {
|
|
162
|
+
timeout: 60000,
|
|
163
|
+
validator: (input) => input.length > 0 || "Cannot be empty",
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
// Option 3: Promise-based (in custom action)
|
|
167
|
+
call_with_context(async (ctx) => {
|
|
168
|
+
const result = await ctx.bot.wait_for_text(ctx.chat_id, {
|
|
169
|
+
timeout: 60000,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
if (result.success) {
|
|
173
|
+
console.log(result.value);
|
|
174
|
+
} else if (result.cancelled) {
|
|
175
|
+
console.log("User cancelled");
|
|
176
|
+
} else if (result.timed_out) {
|
|
177
|
+
console.log("Timeout");
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 9. Commands
|
|
183
|
+
|
|
184
|
+
**Before (v1.x):**
|
|
185
|
+
```typescript
|
|
186
|
+
commands: [
|
|
187
|
+
{ text: "help", action: CallbackAction("show_help") },
|
|
188
|
+
]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**After (v2.0):**
|
|
192
|
+
```typescript
|
|
193
|
+
commands: [
|
|
194
|
+
{
|
|
195
|
+
name: "help", // Changed from 'text' to 'name'
|
|
196
|
+
description: "Show help message", // New: for BotFather
|
|
197
|
+
action: emit("show_help"),
|
|
198
|
+
},
|
|
199
|
+
]
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 10. Error Handling
|
|
203
|
+
|
|
204
|
+
**New in v2.0:**
|
|
205
|
+
```typescript
|
|
206
|
+
import {
|
|
207
|
+
BotError,
|
|
208
|
+
DialogNotFoundError,
|
|
209
|
+
TelegramError,
|
|
210
|
+
ValidationError
|
|
211
|
+
} from "telegram-botbuilder";
|
|
212
|
+
|
|
213
|
+
// Global error dialog
|
|
214
|
+
const schema: Schema = {
|
|
215
|
+
start_dialog: "main",
|
|
216
|
+
error_dialog: "error", // New!
|
|
217
|
+
dialogs: [
|
|
218
|
+
{ id: "main", text: "Hello" },
|
|
219
|
+
{ id: "error", text: "Something went wrong. Please /start again." },
|
|
220
|
+
],
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Typed errors
|
|
224
|
+
try {
|
|
225
|
+
await bot.change_dialog(chat_id, "nonexistent");
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error instanceof DialogNotFoundError) {
|
|
228
|
+
console.log(`Dialog ${error.dialog_id} not found`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Step-by-Step Migration
|
|
234
|
+
|
|
235
|
+
### Step 1: Update Dependencies
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
npm install telegram-botbuilder@2
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Step 2: Update Imports
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
// Before
|
|
245
|
+
import { BotBuilder, ChangeDialog, CallbackAction, WaitForData } from "telegram-botbuilder";
|
|
246
|
+
|
|
247
|
+
// After
|
|
248
|
+
import {
|
|
249
|
+
BotBuilder,
|
|
250
|
+
go_to,
|
|
251
|
+
emit,
|
|
252
|
+
call,
|
|
253
|
+
wait_for_text,
|
|
254
|
+
type Schema
|
|
255
|
+
} from "telegram-botbuilder";
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Step 3: Update Schema
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// Before
|
|
262
|
+
const schema = {
|
|
263
|
+
start: "main",
|
|
264
|
+
content: [...],
|
|
265
|
+
commands: [{ text: "cmd", action: ... }],
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// After
|
|
269
|
+
const schema: Schema = {
|
|
270
|
+
start_dialog: "main",
|
|
271
|
+
dialogs: [...],
|
|
272
|
+
commands: [{ name: "cmd", action: ... }],
|
|
273
|
+
};
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Step 4: Update Dialogs
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
// Before
|
|
280
|
+
{
|
|
281
|
+
id: "menu",
|
|
282
|
+
text: "Choose option",
|
|
283
|
+
buttons: [
|
|
284
|
+
[{ text: "Option A", action: ChangeDialog("opt_a") }],
|
|
285
|
+
],
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// After
|
|
289
|
+
{
|
|
290
|
+
id: "menu",
|
|
291
|
+
text: "Choose option",
|
|
292
|
+
inline_buttons: [
|
|
293
|
+
[{ text: "Option A", action: go_to("opt_a") }],
|
|
294
|
+
],
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Step 5: Update Constructor
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
// Before
|
|
302
|
+
const bot = new BotBuilder(schema, process.env.TOKEN!, { polling: true });
|
|
303
|
+
|
|
304
|
+
// After
|
|
305
|
+
const bot = new BotBuilder(schema, {
|
|
306
|
+
token: process.env.TOKEN!,
|
|
307
|
+
telegram_options: { polling: true },
|
|
308
|
+
});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Step 6: Update Middleware
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
// Before
|
|
315
|
+
bot.use(async (chat, msg) => {
|
|
316
|
+
if (isBlocked(chat)) return true; // Stop
|
|
317
|
+
return false; // Continue
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// After
|
|
321
|
+
bot.use(async (ctx, next) => {
|
|
322
|
+
if (isBlocked(ctx.chat_id)) return; // Stop (don't call next)
|
|
323
|
+
await next(); // Continue
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Step 7: Update Event Listeners
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
// Before
|
|
331
|
+
bot.ActionSystem.on("my_event", (chat, ...args) => { ... });
|
|
332
|
+
|
|
333
|
+
// After
|
|
334
|
+
bot.events.on("my_event", (chat_id, ...args) => { ... });
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### Step 8: Update Direct Bot Access
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
// Before
|
|
341
|
+
await bot._bot.sendMessage(chat, "Hello");
|
|
342
|
+
|
|
343
|
+
// After
|
|
344
|
+
await bot.telegram.sendMessage(chat, "Hello");
|
|
345
|
+
// Or use the built-in method:
|
|
346
|
+
await bot.send_message(chat, "Hello");
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## New Features to Explore
|
|
350
|
+
|
|
351
|
+
After migration, consider using these new v2.0 features:
|
|
352
|
+
|
|
353
|
+
### Reply Keyboards
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
{
|
|
357
|
+
id: "menu",
|
|
358
|
+
text: "Choose from keyboard below:",
|
|
359
|
+
reply_buttons: [
|
|
360
|
+
[
|
|
361
|
+
{ text: "📞 Contact", request_contact: true },
|
|
362
|
+
{ text: "📍 Location", request_location: true },
|
|
363
|
+
],
|
|
364
|
+
],
|
|
365
|
+
reply_keyboard_options: {
|
|
366
|
+
resize_keyboard: true,
|
|
367
|
+
},
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Dialog Lifecycle
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
{
|
|
375
|
+
id: "form",
|
|
376
|
+
text: "Fill out the form",
|
|
377
|
+
on_enter: async (ctx) => {
|
|
378
|
+
// Initialize form data
|
|
379
|
+
ctx.bot.set_user_data(ctx.chat_id, "form", {});
|
|
380
|
+
},
|
|
381
|
+
on_leave: async (ctx) => {
|
|
382
|
+
// Save form data
|
|
383
|
+
const form = ctx.bot.get_user_data(ctx.chat_id, "form");
|
|
384
|
+
await saveToDatabase(form);
|
|
385
|
+
},
|
|
386
|
+
}
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Conditional Actions
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
inline_buttons: [
|
|
393
|
+
[{
|
|
394
|
+
text: "Admin Panel",
|
|
395
|
+
action: when(
|
|
396
|
+
(ctx) => ctx.bot.get_user_data(ctx.chat_id, "is_admin"),
|
|
397
|
+
go_to("admin"),
|
|
398
|
+
notify("Access denied")
|
|
399
|
+
),
|
|
400
|
+
}],
|
|
401
|
+
]
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### Input Validation
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
wait_for_text(handleEmail, {
|
|
408
|
+
validator: (input) => {
|
|
409
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
410
|
+
if (!emailRegex.test(input)) {
|
|
411
|
+
return "Please enter a valid email address";
|
|
412
|
+
}
|
|
413
|
+
return true;
|
|
414
|
+
},
|
|
415
|
+
timeout: 120000,
|
|
416
|
+
timeout_message: "Email input timed out",
|
|
417
|
+
})
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
## Common Issues
|
|
421
|
+
|
|
422
|
+
### Issue: "Cannot find module"
|
|
423
|
+
|
|
424
|
+
ESM requires `.js` extensions in imports. Make sure your `tsconfig.json` has:
|
|
425
|
+
```json
|
|
426
|
+
{
|
|
427
|
+
"compilerOptions": {
|
|
428
|
+
"module": "NodeNext",
|
|
429
|
+
"moduleResolution": "NodeNext"
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### Issue: Schema validation errors
|
|
435
|
+
|
|
436
|
+
v2.0 validates schemas by default. Check the error message for details:
|
|
437
|
+
```typescript
|
|
438
|
+
// Disable validation if needed (not recommended)
|
|
439
|
+
const bot = new BotBuilder(schema, {
|
|
440
|
+
token: "...",
|
|
441
|
+
validate_schema: false,
|
|
442
|
+
});
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### Issue: Middleware not working
|
|
446
|
+
|
|
447
|
+
Remember to call `next()` to continue the chain:
|
|
448
|
+
```typescript
|
|
449
|
+
bot.use(async (ctx, next) => {
|
|
450
|
+
// Your logic here
|
|
451
|
+
await next(); // Don't forget this!
|
|
452
|
+
});
|
|
453
|
+
```
|