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.
Files changed (165) hide show
  1. package/Changelog.md +81 -0
  2. package/MIGRATION.md +453 -0
  3. package/README.md +403 -0
  4. package/lib/actions/callback_action.d.ts +14 -0
  5. package/lib/actions/callback_action.d.ts.map +1 -0
  6. package/lib/actions/callback_action.js +25 -0
  7. package/lib/actions/callback_action.js.map +1 -0
  8. package/lib/actions/change_dialog.d.ts +10 -0
  9. package/lib/actions/change_dialog.d.ts.map +1 -0
  10. package/lib/actions/change_dialog.js +17 -0
  11. package/lib/actions/change_dialog.js.map +1 -0
  12. package/lib/actions/control_flow.d.ts +26 -0
  13. package/lib/actions/control_flow.d.ts.map +1 -0
  14. package/lib/actions/control_flow.js +61 -0
  15. package/lib/actions/control_flow.js.map +1 -0
  16. package/lib/actions/index.d.ts +6 -0
  17. package/lib/actions/index.d.ts.map +1 -0
  18. package/lib/actions/index.js +11 -0
  19. package/lib/actions/index.js.map +1 -0
  20. package/lib/actions/send_message.d.ts +16 -0
  21. package/lib/actions/send_message.d.ts.map +1 -0
  22. package/lib/actions/send_message.js +32 -0
  23. package/lib/actions/send_message.js.map +1 -0
  24. package/lib/actions/wait_for_input.d.ts +14 -0
  25. package/lib/actions/wait_for_input.d.ts.map +1 -0
  26. package/lib/actions/wait_for_input.js +54 -0
  27. package/lib/actions/wait_for_input.js.map +1 -0
  28. package/lib/core/bot_builder.d.ts +145 -0
  29. package/lib/core/bot_builder.d.ts.map +1 -0
  30. package/lib/core/bot_builder.js +678 -0
  31. package/lib/core/bot_builder.js.map +1 -0
  32. package/lib/core/button_registry.d.ts +39 -0
  33. package/lib/core/button_registry.d.ts.map +1 -0
  34. package/lib/core/button_registry.js +84 -0
  35. package/lib/core/button_registry.js.map +1 -0
  36. package/lib/core/dialog_manager.d.ts +71 -0
  37. package/lib/core/dialog_manager.d.ts.map +1 -0
  38. package/lib/core/dialog_manager.js +146 -0
  39. package/lib/core/dialog_manager.js.map +1 -0
  40. package/lib/core/index.d.ts +8 -0
  41. package/lib/core/index.d.ts.map +1 -0
  42. package/lib/core/index.js +8 -0
  43. package/lib/core/index.js.map +1 -0
  44. package/lib/core/input_manager.d.ts +49 -0
  45. package/lib/core/input_manager.d.ts.map +1 -0
  46. package/lib/core/input_manager.js +129 -0
  47. package/lib/core/input_manager.js.map +1 -0
  48. package/lib/core/keyboard_builder.d.ts +35 -0
  49. package/lib/core/keyboard_builder.d.ts.map +1 -0
  50. package/lib/core/keyboard_builder.js +87 -0
  51. package/lib/core/keyboard_builder.js.map +1 -0
  52. package/lib/core/middleware_chain.d.ts +25 -0
  53. package/lib/core/middleware_chain.d.ts.map +1 -0
  54. package/lib/core/middleware_chain.js +54 -0
  55. package/lib/core/middleware_chain.js.map +1 -0
  56. package/lib/core/schema_compiler.d.ts +25 -0
  57. package/lib/core/schema_compiler.d.ts.map +1 -0
  58. package/lib/core/schema_compiler.js +65 -0
  59. package/lib/core/schema_compiler.js.map +1 -0
  60. package/lib/errors/bot_error.d.ts +7 -0
  61. package/lib/errors/bot_error.d.ts.map +1 -0
  62. package/lib/errors/bot_error.js +17 -0
  63. package/lib/errors/bot_error.js.map +1 -0
  64. package/lib/errors/dialog_error.d.ts +12 -0
  65. package/lib/errors/dialog_error.d.ts.map +1 -0
  66. package/lib/errors/dialog_error.js +22 -0
  67. package/lib/errors/dialog_error.js.map +1 -0
  68. package/lib/errors/index.d.ts +5 -0
  69. package/lib/errors/index.d.ts.map +1 -0
  70. package/lib/errors/index.js +5 -0
  71. package/lib/errors/index.js.map +1 -0
  72. package/lib/errors/telegram_error.d.ts +12 -0
  73. package/lib/errors/telegram_error.d.ts.map +1 -0
  74. package/lib/errors/telegram_error.js +39 -0
  75. package/lib/errors/telegram_error.js.map +1 -0
  76. package/lib/errors/validation_error.d.ts +19 -0
  77. package/lib/errors/validation_error.d.ts.map +1 -0
  78. package/lib/errors/validation_error.js +35 -0
  79. package/lib/errors/validation_error.js.map +1 -0
  80. package/lib/index.d.ts +6 -2
  81. package/lib/index.d.ts.map +1 -1
  82. package/lib/index.js +9 -18
  83. package/lib/index.js.map +1 -1
  84. package/lib/types/action.d.ts +58 -0
  85. package/lib/types/action.d.ts.map +1 -0
  86. package/lib/types/action.js +2 -0
  87. package/lib/types/action.js.map +1 -0
  88. package/lib/types/config.d.ts +36 -0
  89. package/lib/types/config.d.ts.map +1 -0
  90. package/lib/types/config.js +12 -0
  91. package/lib/types/config.js.map +1 -0
  92. package/lib/types/dialog.d.ts +39 -0
  93. package/lib/types/dialog.d.ts.map +1 -0
  94. package/lib/types/dialog.js +2 -0
  95. package/lib/types/dialog.js.map +1 -0
  96. package/lib/types/index.d.ts +10 -0
  97. package/lib/types/index.d.ts.map +1 -0
  98. package/lib/types/index.js +3 -0
  99. package/lib/types/index.js.map +1 -0
  100. package/lib/types/internal.d.ts +58 -0
  101. package/lib/types/internal.d.ts.map +1 -0
  102. package/lib/types/internal.js +11 -0
  103. package/lib/types/internal.js.map +1 -0
  104. package/lib/types/keyboard.d.ts +41 -0
  105. package/lib/types/keyboard.d.ts.map +1 -0
  106. package/lib/types/keyboard.js +2 -0
  107. package/lib/types/keyboard.js.map +1 -0
  108. package/lib/types/middleware.d.ts +24 -0
  109. package/lib/types/middleware.d.ts.map +1 -0
  110. package/lib/types/middleware.js +2 -0
  111. package/lib/types/middleware.js.map +1 -0
  112. package/lib/types/schema.d.ts +17 -0
  113. package/lib/types/schema.d.ts.map +1 -0
  114. package/lib/types/schema.js +2 -0
  115. package/lib/types/schema.js.map +1 -0
  116. package/lib/utils/constants.d.ts +33 -0
  117. package/lib/utils/constants.d.ts.map +1 -0
  118. package/lib/utils/constants.js +33 -0
  119. package/lib/utils/constants.js.map +1 -0
  120. package/lib/utils/deep_copy.d.ts +6 -0
  121. package/lib/utils/deep_copy.d.ts.map +1 -0
  122. package/lib/utils/deep_copy.js +45 -0
  123. package/lib/utils/deep_copy.js.map +1 -0
  124. package/lib/utils/hash.d.ts +17 -0
  125. package/lib/utils/hash.d.ts.map +1 -0
  126. package/lib/utils/hash.js +50 -0
  127. package/lib/utils/hash.js.map +1 -0
  128. package/lib/utils/index.d.ts +7 -0
  129. package/lib/utils/index.d.ts.map +1 -0
  130. package/lib/utils/index.js +7 -0
  131. package/lib/utils/index.js.map +1 -0
  132. package/lib/utils/logger.d.ts +17 -0
  133. package/lib/utils/logger.d.ts.map +1 -0
  134. package/lib/utils/logger.js +91 -0
  135. package/lib/utils/logger.js.map +1 -0
  136. package/lib/utils/resolvers.d.ts +29 -0
  137. package/lib/utils/resolvers.d.ts.map +1 -0
  138. package/lib/utils/resolvers.js +60 -0
  139. package/lib/utils/resolvers.js.map +1 -0
  140. package/lib/utils/type_guards.d.ts +22 -0
  141. package/lib/utils/type_guards.d.ts.map +1 -0
  142. package/lib/utils/type_guards.js +38 -0
  143. package/lib/utils/type_guards.js.map +1 -0
  144. package/lib/validation/index.d.ts +2 -0
  145. package/lib/validation/index.d.ts.map +1 -0
  146. package/lib/validation/index.js +2 -0
  147. package/lib/validation/index.js.map +1 -0
  148. package/lib/validation/schema_validator.d.ts +11 -0
  149. package/lib/validation/schema_validator.d.ts.map +1 -0
  150. package/lib/validation/schema_validator.js +156 -0
  151. package/lib/validation/schema_validator.js.map +1 -0
  152. package/package.json +59 -15
  153. package/lib/bot-service.d.ts +0 -27
  154. package/lib/bot-service.d.ts.map +0 -1
  155. package/lib/bot-service.js +0 -326
  156. package/lib/bot-service.js.map +0 -1
  157. package/lib/bot-struct.d.ts +0 -58
  158. package/lib/bot-struct.d.ts.map +0 -1
  159. package/lib/bot-struct.js +0 -3
  160. package/lib/bot-struct.js.map +0 -1
  161. package/readme.md +0 -54
  162. package/src/bot-service.ts +0 -289
  163. package/src/bot-struct.ts +0 -59
  164. package/src/index.ts +0 -2
  165. package/tsconfig.json +0 -108
package/README.md ADDED
@@ -0,0 +1,403 @@
1
+ # telegram-botbuilder
2
+
3
+ > 🤖 Declarative dialog-based Telegram bot framework for Node.js
4
+
5
+ [![npm version](https://img.shields.io/npm/v/telegram-botbuilder.svg)](https://www.npmjs.com/package/telegram-botbuilder)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![Node.js Version](https://img.shields.io/node/v/telegram-botbuilder.svg)](https://nodejs.org)
8
+
9
+ Build Telegram bots with a simple, declarative dialog-based approach. Define your bot's conversation flow as a schema of dialogs and let the framework handle the rest.
10
+
11
+ ## ✨ Features
12
+
13
+ - 📝 **Declarative Schema** - Define dialogs, buttons, and commands in a clear structure
14
+ - 🔀 **Dialog Navigation** - Seamless transitions between conversation states
15
+ - ⌨️ **Dual Keyboard Support** - Both inline and reply keyboards
16
+ - 🔗 **Action Chaining** - Compose complex behaviors from simple actions
17
+ - 🛡️ **Type-Safe** - Full TypeScript support with strict types
18
+ - 🔌 **Middleware System** - Extend functionality with custom middleware
19
+ - ✅ **Schema Validation** - Catch configuration errors early with Zod validation
20
+ - 📊 **Configurable Logging** - Built-in logger with custom logger support
21
+ - 🎯 **Input Handling** - Wait for text, files, photos with validation and timeouts
22
+
23
+ ## 📦 Installation
24
+
25
+ ```bash
26
+ npm install telegram-botbuilder
27
+ ```
28
+
29
+ ## 🚀 Quick Start
30
+
31
+ ```typescript
32
+ import { BotBuilder, go_to, notify, type Schema } from "telegram-botbuilder";
33
+
34
+ const schema: Schema = {
35
+ start_dialog: "welcome",
36
+
37
+ dialogs: [
38
+ {
39
+ id: "welcome",
40
+ text: "👋 Welcome! Choose an option:",
41
+ inline_buttons: [
42
+ [
43
+ { text: "📖 About", action: go_to("about") },
44
+ { text: "⚙️ Settings", action: go_to("settings") },
45
+ ],
46
+ [
47
+ { text: "❓ Help", action: notify("This is a help message!") },
48
+ ],
49
+ ],
50
+ },
51
+ {
52
+ id: "about",
53
+ text: "ℹ️ This is a demo bot built with telegram-botbuilder.",
54
+ inline_buttons: [
55
+ [{ text: "⬅️ Back", action: go_to("welcome") }],
56
+ ],
57
+ },
58
+ {
59
+ id: "settings",
60
+ text: "⚙️ Settings menu",
61
+ inline_buttons: [
62
+ [{ text: "⬅️ Back", action: go_to("welcome") }],
63
+ ],
64
+ },
65
+ ],
66
+
67
+ commands: [
68
+ { name: "help", action: notify("Use /start to begin!") },
69
+ ],
70
+ };
71
+
72
+ const bot = new BotBuilder(schema, {
73
+ token: process.env.BOT_TOKEN!,
74
+ telegram_options: { polling: true },
75
+ });
76
+
77
+ console.log("Bot is running...");
78
+ ```
79
+
80
+ ## 📚 Core Concepts
81
+
82
+ ### Schema
83
+
84
+ The schema defines your entire bot structure:
85
+
86
+ ```typescript
87
+ interface Schema {
88
+ start_dialog: string; // Initial dialog ID
89
+ dialogs: Dialog[]; // All dialogs
90
+ commands?: Command[]; // Bot commands
91
+ error_dialog?: string; // Shown on errors
92
+ fallback_action?: Action; // Unmatched messages
93
+ }
94
+ ```
95
+
96
+ ### Dialogs
97
+
98
+ Dialogs are conversation states with text and buttons:
99
+
100
+ ```typescript
101
+ interface Dialog {
102
+ id: string; // Unique identifier
103
+ text?: TextSource; // Message text (string or function)
104
+ inline_buttons?: ButtonSource; // Inline keyboard
105
+ reply_buttons?: ButtonSource; // Reply keyboard
106
+ images?: ImageSource; // Photos to display
107
+ on_enter?: DialogAction; // Called when entering
108
+ on_leave?: DialogAction; // Called when leaving
109
+ }
110
+ ```
111
+
112
+ ### Dynamic Content
113
+
114
+ Text, buttons, and images can be static or dynamic:
115
+
116
+ ```typescript
117
+ // Static
118
+ text: "Hello, world!"
119
+
120
+ // Dynamic (sync)
121
+ text: (chat_id) => `Hello, user ${chat_id}!`
122
+
123
+ // Dynamic (async)
124
+ text: async (chat_id) => {
125
+ const user = await db.getUser(chat_id);
126
+ return `Hello, ${user.name}!`;
127
+ }
128
+ ```
129
+
130
+ ### Buttons
131
+
132
+ #### Inline Buttons (under messages)
133
+
134
+ ```typescript
135
+ inline_buttons: [
136
+ [
137
+ { text: "Click me", action: go_to("other_dialog") },
138
+ { text: "Open link", url: "https://example.com" },
139
+ ],
140
+ [
141
+ { text: "Web App", web_app: "https://webapp.example.com" },
142
+ ],
143
+ ]
144
+ ```
145
+
146
+ #### Reply Buttons (main keyboard)
147
+
148
+ ```typescript
149
+ reply_buttons: [
150
+ [
151
+ { text: "📞 Share Contact", request_contact: true },
152
+ { text: "📍 Share Location", request_location: true },
153
+ ],
154
+ [
155
+ { text: "Option A", action: go_to("option_a") },
156
+ { text: "Option B", action: go_to("option_b") },
157
+ ],
158
+ ],
159
+ reply_keyboard_options: {
160
+ resize_keyboard: true,
161
+ one_time_keyboard: false,
162
+ }
163
+ ```
164
+
165
+ ## 🎬 Actions
166
+
167
+ Actions are functions that execute when buttons are clicked or commands are received.
168
+
169
+ ### Navigation
170
+
171
+ ```typescript
172
+ import { go_to, go_to_start } from "telegram-botbuilder";
173
+
174
+ // Navigate to specific dialog
175
+ go_to("dialog_id")
176
+
177
+ // Reset to start dialog
178
+ go_to_start()
179
+ ```
180
+
181
+ ### Messaging
182
+
183
+ ```typescript
184
+ import { notify, notify_dynamic } from "telegram-botbuilder";
185
+
186
+ // Send static message
187
+ notify("Hello!")
188
+
189
+ // Auto-delete after 5 seconds
190
+ notify("Temporary message", { auto_delete_ms: 5000 })
191
+
192
+ // Dynamic message
193
+ notify_dynamic((chat_id) => `Your ID: ${chat_id}`)
194
+ ```
195
+
196
+ ### Event Emission
197
+
198
+ ```typescript
199
+ import { emit, call } from "telegram-botbuilder";
200
+
201
+ // Emit event
202
+ emit("button_clicked", "extra_data")
203
+
204
+ // Listen for event
205
+ bot.events.on("button_clicked", (chat_id, data) => {
206
+ console.log(`User ${chat_id} clicked, data: ${data}`);
207
+ });
208
+
209
+ // Call function directly
210
+ call(async (chat_id, ...args) => {
211
+ await someAsyncOperation(chat_id);
212
+ }, arg1, arg2)
213
+ ```
214
+
215
+ ### Input Waiting
216
+
217
+ ```typescript
218
+ import { wait_for_text, wait_for_file, wait_for_photo } from "telegram-botbuilder";
219
+
220
+ // Wait for text input
221
+ wait_for_text(async (chat_id, text) => {
222
+ console.log(`User ${chat_id} entered: ${text}`);
223
+ }, {
224
+ timeout: 60000, // 1 minute timeout
225
+ validator: (input) => { // Validation
226
+ if (input.length < 3) return "Too short!";
227
+ return true;
228
+ },
229
+ cancel_keywords: ["/cancel", "cancel"],
230
+ })
231
+
232
+ // Wait for file
233
+ wait_for_file("file_received", {
234
+ input_types: ["document", "photo"],
235
+ })
236
+
237
+ // Wait for photo specifically
238
+ wait_for_photo((chat_id, result) => {
239
+ console.log(`Photo file_id: ${result.file_id}`);
240
+ })
241
+ ```
242
+
243
+ ### Control Flow
244
+
245
+ ```typescript
246
+ import { sequence, when, delay, when_data, set_data } from "telegram-botbuilder";
247
+
248
+ // Execute multiple actions
249
+ sequence(
250
+ notify("Step 1"),
251
+ delay(1000, notify("Step 2")),
252
+ go_to("next_dialog")
253
+ )
254
+
255
+ // Conditional execution
256
+ when(
257
+ (ctx) => ctx.bot.get_user_data(ctx.chat_id, "is_admin"),
258
+ go_to("admin_panel"),
259
+ notify("Access denied")
260
+ )
261
+
262
+ // Check user data
263
+ when_data<boolean>("premium",
264
+ (value) => value === true,
265
+ go_to("premium_features"),
266
+ notify("Upgrade to premium!")
267
+ )
268
+
269
+ // Set/modify user data
270
+ set_data("counter", (chat_id) => {
271
+ const current = bot.get_user_data<number>(chat_id, "counter") ?? 0;
272
+ return current + 1;
273
+ })
274
+ ```
275
+
276
+ ## 🔌 Middleware
277
+
278
+ Add custom logic to process all updates:
279
+
280
+ ```typescript
281
+ // Logging middleware
282
+ bot.use(async (ctx, next) => {
283
+ console.log(`[${ctx.update_type}] User ${ctx.chat_id}`);
284
+ await next(); // Continue to next middleware/handler
285
+ });
286
+
287
+ // Auth middleware
288
+ bot.use(async (ctx, next) => {
289
+ const allowed = await checkUserAllowed(ctx.user_id);
290
+ if (!allowed) {
291
+ await ctx.bot.send_message(ctx.chat_id, "Access denied");
292
+ return; // Don't call next() - stops processing
293
+ }
294
+ await next();
295
+ });
296
+
297
+ // Rate limiting
298
+ const requests = new Map<number, number>();
299
+ bot.use(async (ctx, next) => {
300
+ const count = requests.get(ctx.chat_id) ?? 0;
301
+ if (count > 10) {
302
+ await ctx.bot.send_message(ctx.chat_id, "Too many requests!");
303
+ return;
304
+ }
305
+ requests.set(ctx.chat_id, count + 1);
306
+ setTimeout(() => requests.delete(ctx.chat_id), 60000);
307
+ await next();
308
+ });
309
+ ```
310
+
311
+ ## 🎛️ Configuration
312
+
313
+ ```typescript
314
+ const bot = new BotBuilder(schema, {
315
+ token: "YOUR_BOT_TOKEN",
316
+
317
+ // Telegram API options
318
+ telegram_options: {
319
+ polling: true,
320
+ // or webhook configuration
321
+ },
322
+
323
+ // Enable /start command (default: true)
324
+ enable_start_command: true,
325
+
326
+ // Parse mode for messages (default: "HTML")
327
+ default_parse_mode: "HTML",
328
+
329
+ // Schema validation (default: true)
330
+ validate_schema: true,
331
+
332
+ // Auto-delete user messages (default: true)
333
+ auto_delete_user_messages: true,
334
+
335
+ // Default input timeout (default: 5 minutes)
336
+ default_input_timeout: 300000,
337
+
338
+ // Logging
339
+ logger: {
340
+ level: "info", // "debug" | "info" | "warn" | "error" | "silent"
341
+
342
+ // Custom logger (optional)
343
+ custom_logger: {
344
+ debug: (msg, ...args) => myLogger.debug(msg, ...args),
345
+ info: (msg, ...args) => myLogger.info(msg, ...args),
346
+ warn: (msg, ...args) => myLogger.warn(msg, ...args),
347
+ error: (msg, ...args) => myLogger.error(msg, ...args),
348
+ },
349
+ },
350
+ });
351
+ ```
352
+
353
+ ## 📖 API Reference
354
+
355
+ ### BotBuilder
356
+
357
+ ```typescript
358
+ class BotBuilder {
359
+ // Properties
360
+ telegram: TelegramBot; // Raw telegram bot instance
361
+ events: EventEmitter; // Event emitter
362
+
363
+ // Methods
364
+ use(middleware: Middleware): this;
365
+ change_dialog(chat_id: number, dialog_id: string): Promise<void>;
366
+ send_message(chat_id: number, text: string, options?): Promise<Message>;
367
+ get_user_state(chat_id: number): UserState;
368
+ set_user_data(chat_id: number, key: string, value: unknown): void;
369
+ get_user_data<T>(chat_id: number, key: string): T | undefined;
370
+ reset_user(chat_id: number): Promise<void>;
371
+ delete_message(chat_id: number, message_id: number): Promise<boolean>;
372
+ get_dialog(dialog_id: string): Dialog | undefined;
373
+ wait_for_text(chat_id: number, options?): Promise<WaitResult<string>>;
374
+ wait_for_file(chat_id: number, options?): Promise<FileWaitResult>;
375
+ stop(): Promise<void>;
376
+ }
377
+ ```
378
+
379
+ ### Types
380
+
381
+ See [types documentation](./docs/types.md) for complete type definitions.
382
+
383
+ ## 🔄 Migration from v1.x
384
+
385
+ See [MIGRATION.md](./MIGRATION.md) for detailed migration guide.
386
+
387
+ ### Quick Changes
388
+
389
+ | v1.x | v2.0 |
390
+ |------|------|
391
+ | `new BotBuilder(schema, token, options)` | `new BotBuilder(schema, { token, ...options })` |
392
+ | `schema.start` | `schema.start_dialog` |
393
+ | `schema.content` | `schema.dialogs` |
394
+ | `dialog.buttons` | `dialog.inline_buttons` |
395
+ | `ChangeDialog(id)` | `go_to(id)` |
396
+ | `CallbackAction(desc)` | `emit(desc)` |
397
+ | `WaitForData(desc)` | `wait_for_text(handler)` |
398
+ | `_bot` | `telegram` |
399
+ | `ActionSystem` | `events` |
400
+
401
+ ## 📄 License
402
+
403
+ MIT © [zxcpadik](https://github.com/zxcpadik)
@@ -0,0 +1,14 @@
1
+ import type { Action, CallbackFunction } from "../types/action.js";
2
+ /**
3
+ * Create an action that emits an event
4
+ */
5
+ export declare function emit(event_name: string, ...args: unknown[]): Action;
6
+ /**
7
+ * Create an action that calls a function directly
8
+ */
9
+ export declare function call(fn: CallbackFunction, ...args: unknown[]): Action;
10
+ /**
11
+ * Create an action that calls a function with the full context
12
+ */
13
+ export declare function call_with_context(fn: (context: import("../types/action.js").ActionContext) => void | Promise<void>): Action;
14
+ //# sourceMappingURL=callback_action.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callback_action.d.ts","sourceRoot":"","sources":["../../src/actions/callback_action.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEnE;;GAEG;AACH,wBAAgB,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAInE;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,EAAE,EAAE,gBAAgB,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAIrE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,OAAO,oBAAoB,EAAE,aAAa,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAI3H"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Create an action that emits an event
3
+ */
4
+ export function emit(event_name, ...args) {
5
+ return (context) => {
6
+ context.bot.events.emit(event_name, context.chat_id, ...args);
7
+ };
8
+ }
9
+ /**
10
+ * Create an action that calls a function directly
11
+ */
12
+ export function call(fn, ...args) {
13
+ return async (context) => {
14
+ await fn(context.chat_id, ...args);
15
+ };
16
+ }
17
+ /**
18
+ * Create an action that calls a function with the full context
19
+ */
20
+ export function call_with_context(fn) {
21
+ return async (context) => {
22
+ await fn(context);
23
+ };
24
+ }
25
+ //# sourceMappingURL=callback_action.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callback_action.js","sourceRoot":"","sources":["../../src/actions/callback_action.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,IAAI,CAAC,UAAkB,EAAE,GAAG,IAAe;IACzD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IAChE,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,IAAI,CAAC,EAAoB,EAAE,GAAG,IAAe;IAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IACrC,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAAiF;IACjH,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { Action } from "../types/action.js";
2
+ /**
3
+ * Create an action that navigates to a specific dialog
4
+ */
5
+ export declare function go_to(dialog_id: string): Action;
6
+ /**
7
+ * Create an action that navigates back to start dialog
8
+ */
9
+ export declare function go_to_start(): Action;
10
+ //# sourceMappingURL=change_dialog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"change_dialog.d.ts","sourceRoot":"","sources":["../../src/actions/change_dialog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;GAEG;AACH,wBAAgB,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAI/C;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,MAAM,CAIpC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Create an action that navigates to a specific dialog
3
+ */
4
+ export function go_to(dialog_id) {
5
+ return async (context) => {
6
+ await context.bot.change_dialog(context.chat_id, dialog_id);
7
+ };
8
+ }
9
+ /**
10
+ * Create an action that navigates back to start dialog
11
+ */
12
+ export function go_to_start() {
13
+ return async (context) => {
14
+ await context.bot.reset_user(context.chat_id);
15
+ };
16
+ }
17
+ //# sourceMappingURL=change_dialog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"change_dialog.js","sourceRoot":"","sources":["../../src/actions/change_dialog.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,KAAK,CAAC,SAAiB;IACrC,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC9D,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { Action, ActionContext } from "../types/action.js";
2
+ /**
3
+ * Execute multiple actions in sequence
4
+ */
5
+ export declare function sequence(...actions: Action[]): Action;
6
+ /**
7
+ * Execute action conditionally
8
+ */
9
+ export declare function when(condition: (context: ActionContext) => boolean | Promise<boolean>, then_action: Action, else_action?: Action): Action;
10
+ /**
11
+ * Execute action with delay
12
+ */
13
+ export declare function delay(ms: number, action: Action): Action;
14
+ /**
15
+ * Execute action only if user data matches
16
+ */
17
+ export declare function when_data<T>(key: string, predicate: (value: T | undefined) => boolean, then_action: Action, else_action?: Action): Action;
18
+ /**
19
+ * Set user data
20
+ */
21
+ export declare function set_data(key: string, value: unknown | ((chat_id: number) => unknown)): Action;
22
+ /**
23
+ * Delete user data
24
+ */
25
+ export declare function delete_data(key: string): Action;
26
+ //# sourceMappingURL=control_flow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control_flow.d.ts","sourceRoot":"","sources":["../../src/actions/control_flow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEhE;;GAEG;AACH,wBAAgB,QAAQ,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAMrD;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,SAAS,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EACjE,WAAW,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,MAAM,GACnB,MAAM,CAUR;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAKxD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,KAAK,OAAO,EAC5C,WAAW,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,MAAM,GACnB,MAAM,CASR;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,MAAM,CAK7F;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAK/C"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Execute multiple actions in sequence
3
+ */
4
+ export function sequence(...actions) {
5
+ return async (context) => {
6
+ for (const action of actions) {
7
+ await action(context);
8
+ }
9
+ };
10
+ }
11
+ /**
12
+ * Execute action conditionally
13
+ */
14
+ export function when(condition, then_action, else_action) {
15
+ return async (context) => {
16
+ const result = await condition(context);
17
+ if (result) {
18
+ await then_action(context);
19
+ }
20
+ else if (else_action) {
21
+ await else_action(context);
22
+ }
23
+ };
24
+ }
25
+ /**
26
+ * Execute action with delay
27
+ */
28
+ export function delay(ms, action) {
29
+ return async (context) => {
30
+ await new Promise((resolve) => setTimeout(resolve, ms));
31
+ await action(context);
32
+ };
33
+ }
34
+ /**
35
+ * Execute action only if user data matches
36
+ */
37
+ export function when_data(key, predicate, then_action, else_action) {
38
+ return when((context) => {
39
+ const value = context.bot.get_user_data(context.chat_id, key);
40
+ return predicate(value);
41
+ }, then_action, else_action);
42
+ }
43
+ /**
44
+ * Set user data
45
+ */
46
+ export function set_data(key, value) {
47
+ return (context) => {
48
+ const resolved = typeof value === "function" ? value(context.chat_id) : value;
49
+ context.bot.set_user_data(context.chat_id, key, resolved);
50
+ };
51
+ }
52
+ /**
53
+ * Delete user data
54
+ */
55
+ export function delete_data(key) {
56
+ return (context) => {
57
+ const state = context.bot.get_user_state(context.chat_id);
58
+ delete state.custom_data[key];
59
+ };
60
+ }
61
+ //# sourceMappingURL=control_flow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control_flow.js","sourceRoot":"","sources":["../../src/actions/control_flow.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAG,OAAiB;IAC3C,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;QACxB,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,IAAI,CAClB,SAAiE,EACjE,WAAmB,EACnB,WAAoB;IAEpB,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,CAAC;QAExC,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;aAAM,IAAI,WAAW,EAAE,CAAC;YACvB,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,KAAK,CAAC,EAAU,EAAE,MAAc;IAC9C,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;QACxD,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CACvB,GAAW,EACX,SAA4C,EAC5C,WAAmB,EACnB,WAAoB;IAEpB,OAAO,IAAI,CACT,CAAC,OAAO,EAAE,EAAE;QACV,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAI,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QACjE,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC,EACD,WAAW,EACX,WAAW,CACZ,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAW,EAAE,KAA+C;IACnF,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QAC9E,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC5D,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC1D,OAAO,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { go_to, go_to_start } from "./change_dialog.js";
2
+ export { emit, call, call_with_context } from "./callback_action.js";
3
+ export { wait_for_text, wait_for_file, wait_for_photo } from "./wait_for_input.js";
4
+ export { notify, notify_dynamic } from "./send_message.js";
5
+ export { sequence, when, delay, when_data, set_data, delete_data, } from "./control_flow.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/actions/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGxD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAGrE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAGnF,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAG3D,OAAO,EACL,QAAQ,EACR,IAAI,EACJ,KAAK,EACL,SAAS,EACT,QAAQ,EACR,WAAW,GACZ,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,11 @@
1
+ // Navigation
2
+ export { go_to, go_to_start } from "./change_dialog.js";
3
+ // Callbacks
4
+ export { emit, call, call_with_context } from "./callback_action.js";
5
+ // Input waiting
6
+ export { wait_for_text, wait_for_file, wait_for_photo } from "./wait_for_input.js";
7
+ // Messaging
8
+ export { notify, notify_dynamic } from "./send_message.js";
9
+ // Control flow
10
+ export { sequence, when, delay, when_data, set_data, delete_data, } from "./control_flow.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/actions/index.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAExD,YAAY;AACZ,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAErE,gBAAgB;AAChB,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEnF,YAAY;AACZ,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAE3D,eAAe;AACf,OAAO,EACL,QAAQ,EACR,IAAI,EACJ,KAAK,EACL,SAAS,EACT,QAAQ,EACR,WAAW,GACZ,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Action } from "../types/action.js";
2
+ /**
3
+ * Create an action that sends a standalone message
4
+ */
5
+ export declare function notify(text: string, options?: {
6
+ parse_mode?: "HTML" | "Markdown" | "MarkdownV2";
7
+ auto_delete_ms?: number;
8
+ }): Action;
9
+ /**
10
+ * Create an action that sends a message with text resolved at runtime
11
+ */
12
+ export declare function notify_dynamic(text_fn: (chat_id: number) => string | Promise<string>, options?: {
13
+ parse_mode?: "HTML" | "Markdown" | "MarkdownV2";
14
+ auto_delete_ms?: number;
15
+ }): Action;
16
+ //# sourceMappingURL=send_message.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send_message.d.ts","sourceRoot":"","sources":["../../src/actions/send_message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;GAEG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,YAAY,CAAC;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,GACA,MAAM,CAYR;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,EACtD,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,YAAY,CAAC;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,GACA,MAAM,CAaR"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Create an action that sends a standalone message
3
+ */
4
+ export function notify(text, options) {
5
+ return async (context) => {
6
+ const msg = await context.bot.send_message(context.chat_id, text, {
7
+ parse_mode: options?.parse_mode,
8
+ });
9
+ if (options?.auto_delete_ms) {
10
+ setTimeout(async () => {
11
+ await context.bot.delete_message(context.chat_id, msg.message_id);
12
+ }, options.auto_delete_ms);
13
+ }
14
+ };
15
+ }
16
+ /**
17
+ * Create an action that sends a message with text resolved at runtime
18
+ */
19
+ export function notify_dynamic(text_fn, options) {
20
+ return async (context) => {
21
+ const text = await text_fn(context.chat_id);
22
+ const msg = await context.bot.send_message(context.chat_id, text, {
23
+ parse_mode: options?.parse_mode,
24
+ });
25
+ if (options?.auto_delete_ms) {
26
+ setTimeout(async () => {
27
+ await context.bot.delete_message(context.chat_id, msg.message_id);
28
+ }, options.auto_delete_ms);
29
+ }
30
+ };
31
+ }
32
+ //# sourceMappingURL=send_message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send_message.js","sourceRoot":"","sources":["../../src/actions/send_message.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,MAAM,CACpB,IAAY,EACZ,OAGC;IAED,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE;YAChE,UAAU,EAAE,OAAO,EAAE,UAAU;SAChC,CAAC,CAAC;QAEH,IAAI,OAAO,EAAE,cAAc,EAAE,CAAC;YAC5B,UAAU,CAAC,KAAK,IAAI,EAAE;gBACpB,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;YACpE,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAsD,EACtD,OAGC;IAED,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE;YAChE,UAAU,EAAE,OAAO,EAAE,UAAU;SAChC,CAAC,CAAC;QAEH,IAAI,OAAO,EAAE,cAAc,EAAE,CAAC;YAC5B,UAAU,CAAC,KAAK,IAAI,EAAE;gBACpB,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;YACpE,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}