grambot 1.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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +126 -0
  3. package/dist/action/action.d.ts +17 -0
  4. package/dist/action/action.d.ts.map +1 -0
  5. package/dist/action/action.js +59 -0
  6. package/dist/action/action.js.map +1 -0
  7. package/dist/conversation/conversation.d.ts +15 -0
  8. package/dist/conversation/conversation.d.ts.map +1 -0
  9. package/dist/conversation/conversation.js +346 -0
  10. package/dist/conversation/conversation.js.map +1 -0
  11. package/dist/engine/engine.d.ts +24 -0
  12. package/dist/engine/engine.d.ts.map +1 -0
  13. package/dist/engine/engine.js +847 -0
  14. package/dist/engine/engine.js.map +1 -0
  15. package/dist/grambot.d.ts +142 -0
  16. package/dist/grambot.d.ts.map +1 -0
  17. package/dist/grambot.js +204 -0
  18. package/dist/grambot.js.map +1 -0
  19. package/dist/index.d.ts +31 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +28 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/menu/button.d.ts +99 -0
  24. package/dist/menu/button.d.ts.map +1 -0
  25. package/dist/menu/button.js +157 -0
  26. package/dist/menu/button.js.map +1 -0
  27. package/dist/menu/layout.d.ts +86 -0
  28. package/dist/menu/layout.d.ts.map +1 -0
  29. package/dist/menu/layout.js +90 -0
  30. package/dist/menu/layout.js.map +1 -0
  31. package/dist/menu/list.d.ts +43 -0
  32. package/dist/menu/list.d.ts.map +1 -0
  33. package/dist/menu/list.js +60 -0
  34. package/dist/menu/list.js.map +1 -0
  35. package/dist/menu/menu.d.ts +29 -0
  36. package/dist/menu/menu.d.ts.map +1 -0
  37. package/dist/menu/menu.js +73 -0
  38. package/dist/menu/menu.js.map +1 -0
  39. package/dist/telebot.d.ts +142 -0
  40. package/dist/telebot.d.ts.map +1 -0
  41. package/dist/telebot.js +204 -0
  42. package/dist/telebot.js.map +1 -0
  43. package/dist/types.d.ts +322 -0
  44. package/dist/types.d.ts.map +1 -0
  45. package/dist/types.js +2 -0
  46. package/dist/types.js.map +1 -0
  47. package/dist/ui/ui.d.ts +12 -0
  48. package/dist/ui/ui.d.ts.map +1 -0
  49. package/dist/ui/ui.js +39 -0
  50. package/dist/ui/ui.js.map +1 -0
  51. package/package.json +55 -0
@@ -0,0 +1,142 @@
1
+ import { Bot } from "grammy";
2
+ import type { ActionHandler, ActionRef, LayoutBuilderInterface, MenuRef, TelebotConfig, TelebotContext } from "./types.js";
3
+ /**
4
+ * # Telebot
5
+ *
6
+ * The main entry-point for building Telegram bots declaratively.
7
+ *
8
+ * ### Quick Start
9
+ * ```ts
10
+ * import { Telebot } from "@superpackages/telebot";
11
+ * import mainMenu from "./menus/main.js";
12
+ *
13
+ * const bot = Telebot.create({
14
+ * token: process.env.BOT_TOKEN!,
15
+ * menu: mainMenu,
16
+ * });
17
+ *
18
+ * bot.start();
19
+ * ```
20
+ */
21
+ /**
22
+ * Helper to build actions or menus attached to specific triggers.
23
+ * Used internally by the {@link Telebot} class.
24
+ */
25
+ declare class TriggerBuilder {
26
+ private triggers;
27
+ /**
28
+ * @internal
29
+ */
30
+ constructor(type: "command" | "word" | "regexp", value: string | RegExp);
31
+ /**
32
+ * Add a command trigger (e.g., /settings).
33
+ * @param name - The command name without the leading slash.
34
+ */
35
+ command(name: string): TriggerBuilder;
36
+ /**
37
+ * Add an exact word or phrase trigger.
38
+ * @param text - The text to trigger on.
39
+ */
40
+ word(text: string): TriggerBuilder;
41
+ /**
42
+ * Add a regular expression trigger.
43
+ * @param pattern - The regex pattern to match against the message text.
44
+ */
45
+ regexp(pattern: RegExp): TriggerBuilder;
46
+ /**
47
+ * Attach an action to the collected triggers.
48
+ * @param handler - The function to handle the action.
49
+ * @returns An {@link ActionRef} that can be used in menus.
50
+ */
51
+ action<P = undefined>(handler: ActionHandler<P>): ActionRef<P>;
52
+ /**
53
+ * Attach a menu to the collected triggers.
54
+ * @param builder - A function to build the menu layout.
55
+ * @param options - Optional menu settings.
56
+ * @returns A {@link MenuRef} that can be used or sent.
57
+ */
58
+ menu(builder: (layout: LayoutBuilderInterface, ctx: TelebotContext) => void | Promise<void>, options?: {
59
+ id?: string;
60
+ }): MenuRef;
61
+ }
62
+ /**
63
+ * The main entry-point for building Telegram bots declaratively.
64
+ *
65
+ * Provides static methods to create triggers, actions, and menus.
66
+ */
67
+ export declare class Telebot {
68
+ /**
69
+ * Create a trigger based on a command (e.g., `/start`).
70
+ * @param name - The command name.
71
+ */
72
+ static command(name: string): TriggerBuilder;
73
+ /**
74
+ * Create a trigger based on an exact word or phrase.
75
+ * @param text - The text to match.
76
+ */
77
+ static word(text: string): TriggerBuilder;
78
+ /**
79
+ * Create a trigger based on a regular expression.
80
+ * @param pattern - The pattern to match.
81
+ */
82
+ static regexp(pattern: RegExp): TriggerBuilder;
83
+ /**
84
+ * Create a typed action with an optional payload generic.
85
+ * @param handler - The logic to execute when the action is triggered.
86
+ */
87
+ static action<P = undefined>(handler: ActionHandler<P>): ActionRef<P>;
88
+ /**
89
+ * Create a declarative menu.
90
+ * @param builder - A layout builder function.
91
+ * @param options - Optional configuration (e.g., fixed ID).
92
+ */
93
+ static menu(builder: (layout: LayoutBuilderInterface, ctx: TelebotContext) => void | Promise<void>, options?: {
94
+ id?: string;
95
+ }): MenuRef;
96
+ /**
97
+ * Create a fully configured bot instance.
98
+ * @param options - Configuration including token and root menu.
99
+ */
100
+ static create(options: TelebotConfig & {
101
+ menu: MenuRef;
102
+ }): TelebotApp;
103
+ }
104
+ /**
105
+ * A running bot instance with the menu tree installed.
106
+ *
107
+ * Created via {@link Telebot.create}.
108
+ */
109
+ export declare class TelebotApp {
110
+ /** The underlying grammy bot instance. */
111
+ readonly bot: Bot<TelebotContext>;
112
+ private readonly config;
113
+ private readonly rootMenu;
114
+ private readonly installPromise;
115
+ /**
116
+ * @internal Use {@link Telebot.create} instead.
117
+ */
118
+ constructor(options: TelebotConfig & {
119
+ menu: MenuRef;
120
+ });
121
+ /**
122
+ * Start the bot using long-polling.
123
+ */
124
+ start(): Promise<void>;
125
+ /**
126
+ * Manually send the root menu to a specific chat.
127
+ * @param chatId - The recipient chat ID.
128
+ * @param ctx - The current context.
129
+ */
130
+ sendMenu(chatId: number, ctx: TelebotContext): Promise<void>;
131
+ /**
132
+ * Returns a callback function for webhook usage.
133
+ * @see grammy documentation for parameters.
134
+ */
135
+ webhookCallback(path?: string, onTimeout?: "throw" | "return" | "continue", timeoutMilliseconds?: number): (req: any, res: any, next?: any) => Promise<any>;
136
+ /**
137
+ * Manually handle an update (e.g., from a custom webhook implementation).
138
+ */
139
+ handleUpdate(update: any, webhookResponse?: any): Promise<void>;
140
+ }
141
+ export {};
142
+ //# sourceMappingURL=telebot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telebot.d.ts","sourceRoot":"","sources":["../src/telebot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAiB,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EACV,aAAa,EACb,SAAS,EACT,sBAAsB,EACtB,OAAO,EACP,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAKpB;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;GAGG;AACH,cAAM,cAAc;IAClB,OAAO,CAAC,QAAQ,CAAqE;IAErF;;OAEG;gBACS,IAAI,EAAE,SAAS,GAAG,MAAM,GAAG,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAMvE;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc;IAKrC;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc;IAKlC;;;OAGG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc;IAKvC;;;;OAIG;IACH,MAAM,CAAC,CAAC,GAAG,SAAS,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC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;gBACS,OAAO,EAAE,aAAa,GAAG;QAAE,IAAI,EAAE,OAAO,CAAA;KAAE;IAkBtD;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAS5B;;;;OAIG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlE;;;OAGG;IACH,eAAe,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,UAAU,EAAE,mBAAmB,CAAC,EAAE,MAAM,IACxF,KAAK,GAAG,EAAE,KAAK,GAAG,EAAE,OAAO,GAAG;IAO9C;;OAEG;IACG,YAAY,CAAC,MAAM,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;CAItE"}
@@ -0,0 +1,204 @@
1
+ import { Bot } from "grammy";
2
+ import { createAction } from "./action/action.js";
3
+ import { createMenu } from "./menu/menu.js";
4
+ import { installMenu, sendMenu } from "./engine/engine.js";
5
+ /**
6
+ * # Telebot
7
+ *
8
+ * The main entry-point for building Telegram bots declaratively.
9
+ *
10
+ * ### Quick Start
11
+ * ```ts
12
+ * import { Telebot } from "@superpackages/telebot";
13
+ * import mainMenu from "./menus/main.js";
14
+ *
15
+ * const bot = Telebot.create({
16
+ * token: process.env.BOT_TOKEN!,
17
+ * menu: mainMenu,
18
+ * });
19
+ *
20
+ * bot.start();
21
+ * ```
22
+ */
23
+ /**
24
+ * Helper to build actions or menus attached to specific triggers.
25
+ * Used internally by the {@link Telebot} class.
26
+ */
27
+ class TriggerBuilder {
28
+ triggers = {};
29
+ /**
30
+ * @internal
31
+ */
32
+ constructor(type, value) {
33
+ if (type === "command")
34
+ this.triggers.commands = [value];
35
+ if (type === "word")
36
+ this.triggers.words = [value];
37
+ if (type === "regexp")
38
+ this.triggers.regexps = [value];
39
+ }
40
+ /**
41
+ * Add a command trigger (e.g., /settings).
42
+ * @param name - The command name without the leading slash.
43
+ */
44
+ command(name) {
45
+ this.triggers.commands = [...(this.triggers.commands || []), name];
46
+ return this;
47
+ }
48
+ /**
49
+ * Add an exact word or phrase trigger.
50
+ * @param text - The text to trigger on.
51
+ */
52
+ word(text) {
53
+ this.triggers.words = [...(this.triggers.words || []), text];
54
+ return this;
55
+ }
56
+ /**
57
+ * Add a regular expression trigger.
58
+ * @param pattern - The regex pattern to match against the message text.
59
+ */
60
+ regexp(pattern) {
61
+ this.triggers.regexps = [...(this.triggers.regexps || []), pattern];
62
+ return this;
63
+ }
64
+ /**
65
+ * Attach an action to the collected triggers.
66
+ * @param handler - The function to handle the action.
67
+ * @returns An {@link ActionRef} that can be used in menus.
68
+ */
69
+ action(handler) {
70
+ const ref = createAction(handler);
71
+ ref.triggers = { ...this.triggers };
72
+ return ref;
73
+ }
74
+ /**
75
+ * Attach a menu to the collected triggers.
76
+ * @param builder - A function to build the menu layout.
77
+ * @param options - Optional menu settings.
78
+ * @returns A {@link MenuRef} that can be used or sent.
79
+ */
80
+ menu(builder, options) {
81
+ const ref = createMenu(builder, options);
82
+ ref.triggers = { ...this.triggers };
83
+ return ref;
84
+ }
85
+ }
86
+ /**
87
+ * The main entry-point for building Telegram bots declaratively.
88
+ *
89
+ * Provides static methods to create triggers, actions, and menus.
90
+ */
91
+ export class Telebot {
92
+ /**
93
+ * Create a trigger based on a command (e.g., `/start`).
94
+ * @param name - The command name.
95
+ */
96
+ static command(name) {
97
+ return new TriggerBuilder("command", name);
98
+ }
99
+ /**
100
+ * Create a trigger based on an exact word or phrase.
101
+ * @param text - The text to match.
102
+ */
103
+ static word(text) {
104
+ return new TriggerBuilder("word", text);
105
+ }
106
+ /**
107
+ * Create a trigger based on a regular expression.
108
+ * @param pattern - The pattern to match.
109
+ */
110
+ static regexp(pattern) {
111
+ return new TriggerBuilder("regexp", pattern);
112
+ }
113
+ /**
114
+ * Create a typed action with an optional payload generic.
115
+ * @param handler - The logic to execute when the action is triggered.
116
+ */
117
+ static action(handler) {
118
+ return createAction(handler);
119
+ }
120
+ /**
121
+ * Create a declarative menu.
122
+ * @param builder - A layout builder function.
123
+ * @param options - Optional configuration (e.g., fixed ID).
124
+ */
125
+ static menu(builder, options) {
126
+ return createMenu(builder, options);
127
+ }
128
+ /**
129
+ * Create a fully configured bot instance.
130
+ * @param options - Configuration including token and root menu.
131
+ */
132
+ static create(options) {
133
+ return new TelebotApp(options);
134
+ }
135
+ }
136
+ /**
137
+ * A running bot instance with the menu tree installed.
138
+ *
139
+ * Created via {@link Telebot.create}.
140
+ */
141
+ export class TelebotApp {
142
+ /** The underlying grammy bot instance. */
143
+ bot;
144
+ config;
145
+ rootMenu;
146
+ installPromise;
147
+ /**
148
+ * @internal Use {@link Telebot.create} instead.
149
+ */
150
+ constructor(options) {
151
+ this.config = options;
152
+ this.rootMenu = options.menu;
153
+ this.bot = new Bot(options.token);
154
+ // Global error boundary — prevents crashes from Grammy/Telegram API errors
155
+ this.bot.catch((err) => {
156
+ const e = err.error;
157
+ if (options.onError) {
158
+ options.onError(e, err.ctx);
159
+ }
160
+ else {
161
+ console.error("[Telebot] Unhandled error:", e);
162
+ }
163
+ });
164
+ this.installPromise = installMenu(this.bot, this.rootMenu, this.config);
165
+ }
166
+ /**
167
+ * Start the bot using long-polling.
168
+ */
169
+ async start() {
170
+ await this.installPromise;
171
+ console.log("🤖 Telebot started");
172
+ await this.bot.start({
173
+ onStart: () => { },
174
+ drop_pending_updates: true,
175
+ });
176
+ }
177
+ /**
178
+ * Manually send the root menu to a specific chat.
179
+ * @param chatId - The recipient chat ID.
180
+ * @param ctx - The current context.
181
+ */
182
+ async sendMenu(chatId, ctx) {
183
+ await sendMenu(this.bot, chatId, this.rootMenu, ctx, this.config.translator);
184
+ }
185
+ /**
186
+ * Returns a callback function for webhook usage.
187
+ * @see grammy documentation for parameters.
188
+ */
189
+ webhookCallback(path, onTimeout, timeoutMilliseconds) {
190
+ return async (req, res, next) => {
191
+ await this.installPromise;
192
+ // @ts-ignore
193
+ return this.bot.webhookCallback(path, onTimeout, timeoutMilliseconds)(req, res, next);
194
+ };
195
+ }
196
+ /**
197
+ * Manually handle an update (e.g., from a custom webhook implementation).
198
+ */
199
+ async handleUpdate(update, webhookResponse) {
200
+ await this.installPromise;
201
+ await this.bot.handleUpdate(update, webhookResponse);
202
+ }
203
+ }
204
+ //# sourceMappingURL=telebot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telebot.js","sourceRoot":"","sources":["../src/telebot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAiB,MAAM,QAAQ,CAAC;AAS5C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE3D;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;GAGG;AACH,MAAM,cAAc;IACV,QAAQ,GAAkE,EAAE,CAAC;IAErF;;OAEG;IACH,YAAY,IAAmC,EAAE,KAAsB;QACrE,IAAI,IAAI,KAAK,SAAS;YAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,GAAG,CAAC,KAAe,CAAC,CAAC;QACnE,IAAI,IAAI,KAAK,MAAM;YAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,KAAe,CAAC,CAAC;QAC7D,IAAI,IAAI,KAAK,QAAQ;YAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,KAAe,CAAC,CAAC;IACnE,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,IAAY;QAClB,IAAI,CAAC,QAAQ,CAAC,QAAQ,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QACnE,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH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KAAK;QACT,MAAM,IAAI,CAAC,cAAc,CAAC;QAC1B,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;QAClC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;YACnB,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC;YACjB,oBAAoB,EAAE,IAAI;SAC3B,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,QAAQ,CAAC,MAAc,EAAE,GAAmB;QAChD,MAAM,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC/E,CAAC;IAED;;;OAGG;IACH,eAAe,CAAC,IAAa,EAAE,SAA2C,EAAE,mBAA4B;QACtG,OAAO,KAAK,EAAE,GAAQ,EAAE,GAAQ,EAAE,IAAU,EAAE,EAAE;YAC9C,MAAM,IAAI,CAAC,cAAc,CAAC;YAC1B,aAAa;YACb,OAAO,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,mBAAmB,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QACxF,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAAC,MAAW,EAAE,eAAqB;QACnD,MAAM,IAAI,CAAC,cAAc,CAAC;QAC1B,MAAM,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACvD,CAAC;CACF"}
@@ -0,0 +1,322 @@
1
+ import type { Context, CallbackQueryContext, StorageAdapter } from "grammy";
2
+ export type { StorageAdapter };
3
+ import type { Conversation, ConversationFlavor } from "@grammyjs/conversations";
4
+ /**
5
+ * Extend this interface via declaration merging to type `ctx.user`:
6
+ *
7
+ * ```ts
8
+ * declare module "Grambot" {
9
+ * interface GrambotUser { isAdmin: boolean; balance: number; }
10
+ * }
11
+ * ```
12
+ */
13
+ export interface GrambotUser {
14
+ [key: string]: any;
15
+ }
16
+ import type { SessionFlavor } from "grammy";
17
+ /**
18
+ * Internal session structure used by Grambot.
19
+ */
20
+ export interface GrambotSession {
21
+ /** Data for @grammyjs/conversations */
22
+ conversation: object;
23
+ /** Tracks the origin menu to handle "back" navigation and updates */
24
+ originMenuId?: string;
25
+ }
26
+ /**
27
+ * The context object used throughout the framework.
28
+ * Extends Grammy's Context with a typed `user` property.
29
+ * Includes SessionFlavor for conversation support.
30
+ */
31
+ export type GrambotContext = ConversationFlavor<Context & SessionFlavor<GrambotSession> & {
32
+ /** The current user, resolved via `resolveUser` config or defaults */
33
+ user: GrambotUser;
34
+ }>;
35
+ /**
36
+ * Context for callback query updates (button clicks).
37
+ */
38
+ export type GrambotCallbackContext = CallbackQueryContext<GrambotContext>;
39
+ /**
40
+ * Type for conversation handlers.
41
+ */
42
+ export type GrambotConversation = Conversation<GrambotContext, GrambotContext>;
43
+ /** Message parse modes supported by Telegram */
44
+ export type ParseMode = "HTML" | "Markdown" | "MarkdownV2";
45
+ /** Button color styles supported by Telegram Bot API 9.4+ */
46
+ export type ButtonStyle = "danger" | "success" | "primary";
47
+ /** Types of input that can be requested from the user */
48
+ export type AskFieldType = "text" | "number" | "photo";
49
+ export interface AskOptions<T = string> {
50
+ /** Expected input type */
51
+ type?: AskFieldType;
52
+ /** Custom validation function (can be async) */
53
+ validate?: (value: T) => boolean | Promise<boolean>;
54
+ /** Message to send if validation fails */
55
+ errorMessage?: string;
56
+ /** Translation interpolation data */
57
+ replace?: Record<string, any>;
58
+ /** Message parse mode */
59
+ parseMode?: ParseMode;
60
+ }
61
+ /** Options for the `say` helper in conversations */
62
+ export interface SayOptions {
63
+ /** Translation interpolation data */
64
+ replace?: Record<string, any>;
65
+ /** Message parse mode */
66
+ parseMode?: ParseMode;
67
+ }
68
+ /** Configuration for a button in an `ask` keyboard */
69
+ export interface AskKeyboardButton {
70
+ /** Button label */
71
+ text: string;
72
+ /** Optional button ID */
73
+ _id?: string;
74
+ /** Set the button ID */
75
+ id(value: string): AskKeyboardButton;
76
+ /** Set a URL (makes it a URL button) */
77
+ url(value: string): AskKeyboardButton;
78
+ /** Set an action handler */
79
+ action(handler: ButtonActionHandler): AskKeyboardButton;
80
+ /** Set a submenu to open */
81
+ menu(ref: MenuRef): AskKeyboardButton;
82
+ /** Navigate to a submenu (shorthand for .menu()) */
83
+ navigate(ref: MenuRef): AskKeyboardButton;
84
+ /** Set custom payload for the action */
85
+ payload(data: Record<string, unknown>): AskKeyboardButton;
86
+ /** Force this button to be on a new row */
87
+ row(): AskKeyboardButton;
88
+ }
89
+ /** Function to build an inline keyboard for the `ask` helper */
90
+ export type AskKeyboardBuilder = (keyboard: AskKeyboard) => void;
91
+ /** Keyboard builder interface for conversations */
92
+ export interface AskKeyboard {
93
+ /** Add a button to the keyboard */
94
+ button(text: string): AskKeyboardButton;
95
+ /** Set maximum number of buttons per row */
96
+ maxPerRow(count: number): void;
97
+ }
98
+ /** Definition for a form field in a multi-step conversation */
99
+ export interface FormFieldDefinition<K extends string = string> {
100
+ /** Field name in the resulting object */
101
+ name: K;
102
+ /** Question to ask the user */
103
+ question: string;
104
+ /** Expected data type */
105
+ type: AskFieldType;
106
+ }
107
+ /** Helper provided to actions to manage conversations */
108
+ export interface ConversationHelper {
109
+ /**
110
+ * @returns The user input or `undefined` if cancelled/navigated away.
111
+ */
112
+ ask<T = string>(question: string, options?: AskOptions<T>): Promise<T | undefined>;
113
+ /** Ask for a choice from a keyboard */
114
+ ask(question: string, builder: AskKeyboardBuilder): Promise<string | undefined>;
115
+ /** Ask for a choice from a keyboard with options */
116
+ ask(question: string, options: AskOptions<any>, builder: AskKeyboardBuilder): Promise<string | undefined>;
117
+ /**
118
+ * Update the current prompt with new text/buttons WITHOUT waiting for input.
119
+ * Useful for showing results or intermediate states.
120
+ */
121
+ say(text: string, options?: SayOptions | AskKeyboardBuilder): Promise<void>;
122
+ /** Say with both options and a keyboard builder */
123
+ say(text: string, options: SayOptions, builder: AskKeyboardBuilder): Promise<void>;
124
+ /**
125
+ * Manually delete the current conversation prompt.
126
+ */
127
+ delete(): Promise<void>;
128
+ /** Run a multi-step form collection */
129
+ form<T extends Record<string, unknown>>(fields: FormFieldDefinition<Extract<keyof T, string>>[]): Promise<T | undefined>;
130
+ /** Navigate to a specific menu or the root menu */
131
+ navigate(menu?: MenuRef): Promise<void>;
132
+ }
133
+ /** Helper for simple UI interactions */
134
+ export interface UIHelper {
135
+ /** Show a temporary toast message (answerCallbackQuery) */
136
+ toast(text: string): Promise<void>;
137
+ /** Show an alert dialog */
138
+ alert(text: string): Promise<void>;
139
+ }
140
+ /** Context passed to action handlers */
141
+ export interface ActionContext<P = undefined> {
142
+ /** Grammy context */
143
+ ctx: GrambotContext;
144
+ /** Custom data passed to this action */
145
+ payload: P;
146
+ /** Conversation manager */
147
+ conversation: ConversationHelper;
148
+ /** UI interaction helpers */
149
+ ui: UIHelper;
150
+ /** Layout builder for response */
151
+ layout: LayoutBuilderInterface;
152
+ /** The value of the button ID or 'id' field from payload */
153
+ id: string;
154
+ /** Navigate to a specific menu or the root menu */
155
+ navigate(menu?: MenuRef): Promise<void>;
156
+ }
157
+ /** A function that handles a specific bot action */
158
+ export type ActionHandler<P = undefined> = (context: ActionContext<P>) => Promise<void> | void;
159
+ /** A function used to conditionally show or hide elements */
160
+ export type GuardFn = (ctx: GrambotContext) => boolean | Promise<boolean>;
161
+ /** A string or a function that returns a string based on context */
162
+ export type DynamicLabel = string | ((ctx: GrambotContext) => string);
163
+ /** Action handler for a button click */
164
+ export type ButtonActionHandler = ActionHandler<any> | ActionRef<any>;
165
+ /** A reference to a registered action */
166
+ export interface ActionRef<P = undefined> {
167
+ __Grambot_action: true;
168
+ /** Unique action ID */
169
+ id: string;
170
+ /** The actual handler function */
171
+ handler: ActionHandler<P>;
172
+ /** Triggers that can invoke this action globally */
173
+ triggers?: {
174
+ commands?: string[];
175
+ words?: string[];
176
+ regexps?: RegExp[];
177
+ };
178
+ /** Add command trigger */
179
+ command(name: string): ActionRef<P>;
180
+ /** Add text trigger */
181
+ word(text: string): ActionRef<P>;
182
+ /** Add regex trigger */
183
+ regexp(pattern: RegExp): ActionRef<P>;
184
+ }
185
+ /** Configuration for a menu button */
186
+ export interface ButtonConfig {
187
+ /** Display text or label builder */
188
+ label: DynamicLabel;
189
+ /** Custom button ID */
190
+ buttonId?: string;
191
+ /** URL for the button */
192
+ url?: string;
193
+ /** If true, this button will always start a new row */
194
+ forceRow: boolean;
195
+ /** Condition for showing this button */
196
+ guard?: GuardFn;
197
+ /** Action to execute on click */
198
+ action?: ButtonActionHandler;
199
+ /** @internal */
200
+ inlineHandler?: ActionHandler<any>;
201
+ /** Submenu to open when clicked */
202
+ submenu?: MenuRef;
203
+ /** Custom data to pass to the action */
204
+ payload?: Record<string, unknown>;
205
+ /** If true, this action is triggered if no other buttons match (for text/regex) */
206
+ isDefault?: boolean;
207
+ /** Button color style (danger=red, success=green, primary=blue). Requires Bot API 9.4+ */
208
+ style?: ButtonStyle;
209
+ /** Custom emoji icon ID displayed before the button text. Requires Bot API 9.4+ */
210
+ iconCustomEmojiId?: string;
211
+ }
212
+ /** A reference to a registered menu */
213
+ export interface MenuRef {
214
+ __Grambot_menu: true;
215
+ /** Unique menu ID */
216
+ id: string;
217
+ /** Function that defines the menu layout */
218
+ builder: (layout: LayoutBuilderInterface, ctx: GrambotContext) => void | Promise<void>;
219
+ /** Triggers that can open this menu globally */
220
+ triggers?: {
221
+ commands?: string[];
222
+ words?: string[];
223
+ regexps?: RegExp[];
224
+ };
225
+ /** Add command trigger */
226
+ command(name: string): MenuRef;
227
+ /** Add text trigger */
228
+ word(text: string): MenuRef;
229
+ /** Add regex trigger */
230
+ regexp(pattern: RegExp): MenuRef;
231
+ }
232
+ /** Configuration for a paginated list of items */
233
+ export interface ListConfig<T> {
234
+ /** Data items to display */
235
+ items: T[];
236
+ /** Number of items per page */
237
+ itemsPerPage: number;
238
+ /** Number of grid columns */
239
+ columns: number;
240
+ /** Function to render each item as a button */
241
+ renderFn?: (item: T) => ButtonBuilderInterface;
242
+ /** Default action for all items in the list */
243
+ action?: ButtonActionHandler;
244
+ }
245
+ /** Interface for building menu layouts */
246
+ export interface LayoutBuilderInterface {
247
+ /** Set the menu body text */
248
+ text(content: string): TextBuilderInterface;
249
+ /** Set the menu header image */
250
+ image(url: string): void;
251
+ /** Add a button to the menu */
252
+ button(label: DynamicLabel): ButtonBuilderInterface;
253
+ /** Add a paginated grid of items */
254
+ list<T>(items: T[], action?: ButtonActionHandler): ListBuilderInterface<T>;
255
+ /** Set maximum number of buttons per row */
256
+ maxPerRow(count: number): void;
257
+ /** Add a button that refreshes the current menu */
258
+ refreshButton(label: string): void;
259
+ }
260
+ /** Interface for building menu text */
261
+ export interface TextBuilderInterface {
262
+ /** Set the parse mode for the text */
263
+ parseAs(mode: ParseMode): TextBuilderInterface;
264
+ /** Set interpolation variables for the text */
265
+ replace(data: Record<string, any>): TextBuilderInterface;
266
+ }
267
+ /** Interface for configuring a menu button */
268
+ export interface ButtonBuilderInterface {
269
+ /** Set a custom button ID */
270
+ id(value: string): ButtonBuilderInterface;
271
+ /** Set a URL (makes it a URL button) */
272
+ url(value: string): ButtonBuilderInterface;
273
+ /** Force this button to be on a new row */
274
+ row(): ButtonBuilderInterface;
275
+ /** Set a visibility guard */
276
+ guard(fn: GuardFn): ButtonBuilderInterface;
277
+ /** Set an action handler */
278
+ action(handler: ButtonActionHandler): ButtonBuilderInterface;
279
+ /** Set a submenu to open */
280
+ menu(ref: MenuRef): ButtonBuilderInterface;
281
+ /** Set custom payload for the action */
282
+ payload<P extends Record<string, unknown>>(data: P): ButtonBuilderInterface;
283
+ /** Mark as the default action for this menu */
284
+ default(): ButtonBuilderInterface;
285
+ /** Set a button color style (danger=red, success=green, primary=blue) */
286
+ style(value: ButtonStyle): ButtonBuilderInterface;
287
+ /** Shorthand for .style("danger") — red button */
288
+ danger(): ButtonBuilderInterface;
289
+ /** Shorthand for .style("success") — green button */
290
+ success(): ButtonBuilderInterface;
291
+ /** Shorthand for .style("primary") — blue button */
292
+ primary(): ButtonBuilderInterface;
293
+ /** Set a custom emoji icon displayed before the button text */
294
+ icon(customEmojiId: string): ButtonBuilderInterface;
295
+ }
296
+ /** Interface for configuring a paginated list */
297
+ export interface ListBuilderInterface<T> {
298
+ /** Set items per page */
299
+ perPage(count: number): ListBuilderInterface<T>;
300
+ /** Set grid columns */
301
+ columns(count: number): ListBuilderInterface<T>;
302
+ /** Define how to render each item */
303
+ render(fn: (item: T) => ButtonBuilderInterface): void;
304
+ /** Set the default action for items in this list */
305
+ action(handler: ButtonActionHandler): ListBuilderInterface<T>;
306
+ }
307
+ /** Function for localizing strings */
308
+ export type Translator = (key: string, ctx: GrambotContext, options?: Record<string, any>) => string;
309
+ /** Main configuration for GrambotApp */
310
+ export interface GrambotConfig {
311
+ /** Bot token from @BotFather */
312
+ token: string;
313
+ /** Optional: resolve the GrambotUser from ctx (e.g., DB lookup) */
314
+ resolveUser?: (ctx: Context) => GrambotUser | Promise<GrambotUser>;
315
+ /** Optional: translator function for internal strings */
316
+ translator?: Translator;
317
+ /** Optional: custom storage for bot sessions */
318
+ sessionStorage?: StorageAdapter<GrambotSession>;
319
+ /** Optional: custom error handler. If not set, errors are logged to console without crashing. */
320
+ onError?: (error: unknown, ctx: GrambotContext) => void;
321
+ }
322
+ //# sourceMappingURL=types.d.ts.map