grammy-tools 1.36.2
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/LICENSE +21 -0
- package/README.md +339 -0
- package/out/bot.d.ts +314 -0
- package/out/bot.js +669 -0
- package/out/composer.d.ts +764 -0
- package/out/composer.js +704 -0
- package/out/context.d.ts +1618 -0
- package/out/context.js +1976 -0
- package/out/convenience/constants.d.ts +98 -0
- package/out/convenience/constants.js +37 -0
- package/out/convenience/frameworks.d.ts +216 -0
- package/out/convenience/frameworks.js +370 -0
- package/out/convenience/inline_query.d.ts +401 -0
- package/out/convenience/inline_query.js +461 -0
- package/out/convenience/input_media.d.ts +72 -0
- package/out/convenience/input_media.js +82 -0
- package/out/convenience/keyboard.d.ts +763 -0
- package/out/convenience/keyboard.js +935 -0
- package/out/convenience/session.d.ts +345 -0
- package/out/convenience/session.js +455 -0
- package/out/convenience/webhook.d.ts +59 -0
- package/out/convenience/webhook.js +80 -0
- package/out/core/api.d.ts +1628 -0
- package/out/core/api.js +1907 -0
- package/out/core/client.d.ts +180 -0
- package/out/core/client.js +226 -0
- package/out/core/error.d.ts +53 -0
- package/out/core/error.js +87 -0
- package/out/core/payload.d.ts +39 -0
- package/out/core/payload.js +199 -0
- package/out/filter.d.ts +959 -0
- package/out/filter.js +378 -0
- package/out/mod.d.ts +14 -0
- package/out/mod.js +43 -0
- package/out/platform.node.d.ts +16 -0
- package/out/platform.node.js +43 -0
- package/out/shim.node.d.ts +2 -0
- package/out/shim.node.js +7 -0
- package/out/types.d.ts +1 -0
- package/out/types.js +17 -0
- package/out/types.node.d.ts +78 -0
- package/out/types.node.js +107 -0
- package/out/web.d.ts +1 -0
- package/out/web.mjs +5172 -0
- package/package.json +71 -0
package/out/bot.d.ts
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { BotError, Composer, type Middleware, type ReactionMiddleware } from "./composer.js";
|
|
2
|
+
import { Context, type MaybeArray, type ReactionContext } from "./context.js";
|
|
3
|
+
import { Api } from "./core/api.js";
|
|
4
|
+
import { type ApiClientOptions, type WebhookReplyEnvelope } from "./core/client.js";
|
|
5
|
+
import { type Filter, type FilterQuery } from "./filter.js";
|
|
6
|
+
import { type ReactionType, type ReactionTypeEmoji, type Update, type UserFromGetMe } from "./types.js";
|
|
7
|
+
export declare const DEFAULT_UPDATE_TYPES: readonly ["message", "edited_message", "channel_post", "edited_channel_post", "business_connection", "business_message", "edited_business_message", "deleted_business_messages", "inline_query", "chosen_inline_result", "callback_query", "shipping_query", "pre_checkout_query", "poll", "poll_answer", "my_chat_member", "chat_join_request", "chat_boost", "removed_chat_boost"];
|
|
8
|
+
/**
|
|
9
|
+
* Options that can be specified when running the bot via simple long polling.
|
|
10
|
+
*/
|
|
11
|
+
export interface PollingOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Limits the number of updates to be retrieved per `getUpdates` call.
|
|
14
|
+
* Values between 1-100 are accepted. Defaults to 100.
|
|
15
|
+
*/
|
|
16
|
+
limit?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Timeout in seconds for long polling. grammY uses 30 seconds as a default
|
|
19
|
+
* value.
|
|
20
|
+
*/
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/**
|
|
23
|
+
* A list of the update types you want your bot to receive. For example,
|
|
24
|
+
* specify ["message", "edited_channel_post", "callback_query"] to only
|
|
25
|
+
* receive updates of these types. See Update for a complete list of
|
|
26
|
+
* available update types. Specify an empty list to receive all update types
|
|
27
|
+
* except chat_member, message_reaction, and message_reaction_count
|
|
28
|
+
* (default). If not specified, the previous setting will be used.
|
|
29
|
+
*
|
|
30
|
+
* Please note that this parameter doesn't affect updates created before the
|
|
31
|
+
* call to the getUpdates, so unwanted updates may be received for a short
|
|
32
|
+
* period of time.
|
|
33
|
+
*/
|
|
34
|
+
allowed_updates?: ReadonlyArray<Exclude<keyof Update, "update_id">>;
|
|
35
|
+
/**
|
|
36
|
+
* Pass True to drop all pending updates before starting the long polling.
|
|
37
|
+
*/
|
|
38
|
+
drop_pending_updates?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* A callback function that is useful for logging (or setting up middleware
|
|
41
|
+
* if you did not do this before). It will be executed after the setup of
|
|
42
|
+
* the bot has completed, and immediately before the first updates are being
|
|
43
|
+
* fetched. The bot information `bot.botInfo` will be available when the
|
|
44
|
+
* function is run. For convenience, the callback function receives the
|
|
45
|
+
* value of `bot.botInfo` as an argument.
|
|
46
|
+
*
|
|
47
|
+
* When this function is invoked, the bot already signals that it is
|
|
48
|
+
* running. In other words, `bot.isRunning()` already returns true.
|
|
49
|
+
*/
|
|
50
|
+
onStart?: (botInfo: UserFromGetMe) => void | Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
export { BotError };
|
|
53
|
+
/**
|
|
54
|
+
* Error handler that can be installed on a bot to catch error thrown by
|
|
55
|
+
* middleware.
|
|
56
|
+
*/
|
|
57
|
+
export type ErrorHandler<C extends Context = Context> = (error: BotError<C>) => unknown;
|
|
58
|
+
/**
|
|
59
|
+
* Options to pass to the bot when creating it.
|
|
60
|
+
*/
|
|
61
|
+
export interface BotConfig<C extends Context> {
|
|
62
|
+
/**
|
|
63
|
+
* You can specify a number of advanced options under the `client` property.
|
|
64
|
+
* The options will be passed to the grammY client—this is the part of
|
|
65
|
+
* grammY that actually connects to the Telegram Bot API server in the end
|
|
66
|
+
* when making HTTP requests.
|
|
67
|
+
*/
|
|
68
|
+
client?: ApiClientOptions;
|
|
69
|
+
/**
|
|
70
|
+
* grammY automatically calls `getMe` when starting up to make sure that
|
|
71
|
+
* your bot has access to the bot's own information. If you restart your bot
|
|
72
|
+
* often, for example because it is running in a serverless environment,
|
|
73
|
+
* then you may want to skip this initial API call.
|
|
74
|
+
*
|
|
75
|
+
* Set this property of the options to pre-initialize the bot with cached
|
|
76
|
+
* values. If you use this option, grammY will not attempt to make a `getMe`
|
|
77
|
+
* call but use the provided data instead.
|
|
78
|
+
*/
|
|
79
|
+
botInfo?: UserFromGetMe;
|
|
80
|
+
/**
|
|
81
|
+
* Pass the constructor of a custom context object that will be used when
|
|
82
|
+
* creating the context for each incoming update.
|
|
83
|
+
*/
|
|
84
|
+
ContextConstructor?: new (...args: ConstructorParameters<typeof Context>) => C;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* This is the single most important class of grammY. It represents your bot.
|
|
88
|
+
*
|
|
89
|
+
* First, you must create a bot by talking to @BotFather, check out
|
|
90
|
+
* https://t.me/BotFather. Once it is ready, you obtain a secret token for your
|
|
91
|
+
* bot. grammY will use that token to identify as your bot when talking to the
|
|
92
|
+
* Telegram servers. Got the token? You are now ready to write some code and run
|
|
93
|
+
* your bot!
|
|
94
|
+
*
|
|
95
|
+
* You should do three things to run your bot:
|
|
96
|
+
* ```ts
|
|
97
|
+
* // 1. Create a bot instance
|
|
98
|
+
* const bot = new Bot('<secret-token>')
|
|
99
|
+
* // 2. Listen for updates
|
|
100
|
+
* bot.on('message:text', ctx => ctx.reply('You wrote: ' + ctx.message.text))
|
|
101
|
+
* // 3. Launch it!
|
|
102
|
+
* bot.start()
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare class Bot<C extends Context = Context, A extends Api = Api> extends Composer<C> {
|
|
106
|
+
readonly token: string;
|
|
107
|
+
private pollingRunning;
|
|
108
|
+
private pollingAbortController;
|
|
109
|
+
private lastTriedUpdateId;
|
|
110
|
+
/**
|
|
111
|
+
* Gives you full access to the Telegram Bot API.
|
|
112
|
+
* ```ts
|
|
113
|
+
* // This is how to call the Bot API methods:
|
|
114
|
+
* bot.api.sendMessage(chat_id, 'Hello, grammY!')
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Use this only outside of your middleware. If you have access to `ctx`,
|
|
118
|
+
* then using `ctx.api` instead of `bot.api` is preferred.
|
|
119
|
+
*/
|
|
120
|
+
readonly api: A;
|
|
121
|
+
private me;
|
|
122
|
+
private mePromise;
|
|
123
|
+
private readonly clientConfig;
|
|
124
|
+
private readonly ContextConstructor;
|
|
125
|
+
/** Used to log a warning if some update types are not in allowed_updates */
|
|
126
|
+
private observedUpdateTypes;
|
|
127
|
+
/**
|
|
128
|
+
* Holds the bot's error handler that is invoked whenever middleware throws
|
|
129
|
+
* (rejects). If you set your own error handler via `bot.catch`, all that
|
|
130
|
+
* happens is that this variable is assigned.
|
|
131
|
+
*/
|
|
132
|
+
errorHandler: ErrorHandler<C>;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a new Bot with the given token.
|
|
135
|
+
*
|
|
136
|
+
* Remember that you can listen for messages by calling
|
|
137
|
+
* ```ts
|
|
138
|
+
* bot.on('message', ctx => { ... })
|
|
139
|
+
* ```
|
|
140
|
+
* or similar methods.
|
|
141
|
+
*
|
|
142
|
+
* The simplest way to start your bot is via simple long polling:
|
|
143
|
+
* ```ts
|
|
144
|
+
* bot.start()
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @param token The bot's token as acquired from https://t.me/BotFather
|
|
148
|
+
* @param config Optional configuration properties for the bot
|
|
149
|
+
*/
|
|
150
|
+
constructor(token: string, config?: BotConfig<C>);
|
|
151
|
+
/**
|
|
152
|
+
* Information about the bot itself as retrieved from `api.getMe()`. Only
|
|
153
|
+
* available after the bot has been initialized via `await bot.init()`, or
|
|
154
|
+
* after the value has been set manually.
|
|
155
|
+
*
|
|
156
|
+
* Starting the bot will always perform the initialization automatically,
|
|
157
|
+
* unless a manual value is already set.
|
|
158
|
+
*
|
|
159
|
+
* Note that the recommended way to set a custom bot information object is
|
|
160
|
+
* to pass it to the configuration object of the `new Bot()` instantiation,
|
|
161
|
+
* rather than assigning this property.
|
|
162
|
+
*/
|
|
163
|
+
set botInfo(botInfo: UserFromGetMe);
|
|
164
|
+
get botInfo(): UserFromGetMe;
|
|
165
|
+
/**
|
|
166
|
+
* @inheritdoc
|
|
167
|
+
*/
|
|
168
|
+
on<Q extends FilterQuery>(filter: Q | Q[], ...middleware: Array<Middleware<Filter<C, Q>>>): Composer<Filter<C, Q>>;
|
|
169
|
+
/**
|
|
170
|
+
* @inheritdoc
|
|
171
|
+
*/
|
|
172
|
+
reaction(reaction: MaybeArray<ReactionTypeEmoji["emoji"] | ReactionType>, ...middleware: Array<ReactionMiddleware<C>>): Composer<ReactionContext<C>>;
|
|
173
|
+
/**
|
|
174
|
+
* Checks if the bot has been initialized. A bot is initialized if the bot
|
|
175
|
+
* information is set. The bot information can either be set automatically
|
|
176
|
+
* by calling `bot.init`, or manually through the bot constructor. Note that
|
|
177
|
+
* usually, initialization is done automatically and you do not have to care
|
|
178
|
+
* about this method.
|
|
179
|
+
*
|
|
180
|
+
* @returns true if the bot is initialized, and false otherwise
|
|
181
|
+
*/
|
|
182
|
+
isInited(): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Initializes the bot, i.e. fetches information about the bot itself. This
|
|
185
|
+
* method is called automatically, you usually don't have to call it
|
|
186
|
+
* manually.
|
|
187
|
+
*
|
|
188
|
+
* @param signal Optional `AbortSignal` to cancel the initialization
|
|
189
|
+
*/
|
|
190
|
+
init(signal?: AbortSignal): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Internal. Do not call. Handles an update batch sequentially by supplying
|
|
193
|
+
* it one-by-one to the middleware. Handles middleware errors and stores the
|
|
194
|
+
* last update identifier that was being tried to handle.
|
|
195
|
+
*
|
|
196
|
+
* @param updates An array of updates to handle
|
|
197
|
+
*/
|
|
198
|
+
private handleUpdates;
|
|
199
|
+
/**
|
|
200
|
+
* This is an internal method that you probably will not ever need to call.
|
|
201
|
+
* It is used whenever a new update arrives from the Telegram servers that
|
|
202
|
+
* your bot will handle.
|
|
203
|
+
*
|
|
204
|
+
* If you're writing a library on top of grammY, check out the
|
|
205
|
+
* [documentation](https://grammy.dev/plugins/runner) of the runner
|
|
206
|
+
* plugin for an example that uses this method.
|
|
207
|
+
*
|
|
208
|
+
* @param update An update from the Telegram Bot API
|
|
209
|
+
* @param webhookReplyEnvelope An optional webhook reply envelope
|
|
210
|
+
*/
|
|
211
|
+
handleUpdate(update: Update, webhookReplyEnvelope?: WebhookReplyEnvelope): Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* Starts your bot using long polling.
|
|
214
|
+
*
|
|
215
|
+
* > This method returns a `Promise` that will never resolve except if your
|
|
216
|
+
* > bot is stopped. **You don't need to `await` the call to `bot.start`**,
|
|
217
|
+
* > but remember to catch potential errors by calling `bot.catch`.
|
|
218
|
+
* > Otherwise your bot will crash (and stop) if something goes wrong in
|
|
219
|
+
* > your code.
|
|
220
|
+
*
|
|
221
|
+
* This method effectively enters a loop that will repeatedly call
|
|
222
|
+
* `getUpdates` and run your middleware for every received update, allowing
|
|
223
|
+
* your bot to respond to messages.
|
|
224
|
+
*
|
|
225
|
+
* If your bot is already running, this method does nothing.
|
|
226
|
+
*
|
|
227
|
+
* **Note that this starts your bot using a very simple long polling
|
|
228
|
+
* implementation.** `bot.start` should only be used for small bots. While
|
|
229
|
+
* the rest of grammY was built to perform well even under extreme loads,
|
|
230
|
+
* simple long polling is not capable of scaling up in a similar fashion.
|
|
231
|
+
* You should switch over to using `@grammyjs/runner` if you are running a
|
|
232
|
+
* bot with high load.
|
|
233
|
+
*
|
|
234
|
+
* What exactly _high load_ means differs from bot to bot, but as a rule of
|
|
235
|
+
* thumb, simple long polling should not be processing more than ~5K
|
|
236
|
+
* messages every hour. Also, if your bot has long-running operations such
|
|
237
|
+
* as large file transfers that block the middleware from completing, this
|
|
238
|
+
* will impact the responsiveness negatively, so it makes sense to use the
|
|
239
|
+
* `@grammyjs/runner` package even if you receive much fewer messages. If
|
|
240
|
+
* you worry about how much load your bot can handle, check out the grammY
|
|
241
|
+
* [documentation](https://grammy.dev/advanced/scaling) about scaling
|
|
242
|
+
* up.
|
|
243
|
+
*
|
|
244
|
+
* @param options Options to use for simple long polling
|
|
245
|
+
*/
|
|
246
|
+
start(options?: PollingOptions): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Stops the bot from long polling.
|
|
249
|
+
*
|
|
250
|
+
* All middleware that is currently being executed may complete, but no
|
|
251
|
+
* further `getUpdates` calls will be performed. The current `getUpdates`
|
|
252
|
+
* request will be cancelled.
|
|
253
|
+
*
|
|
254
|
+
* In addition, this method will _confirm_ the last received update to the
|
|
255
|
+
* Telegram servers by calling `getUpdates` one last time with the latest
|
|
256
|
+
* offset value. If any updates are received in this call, they are
|
|
257
|
+
* discarded and will be fetched again when the bot starts up the next time.
|
|
258
|
+
* Confer the official documentation on confirming updates if you want to
|
|
259
|
+
* know more: https://core.telegram.org/bots/api#getupdates
|
|
260
|
+
*
|
|
261
|
+
* > Note that this method will not wait for the middleware stack to finish.
|
|
262
|
+
* > If you need to run code after all middleware is done, consider waiting
|
|
263
|
+
* > for the promise returned by `bot.start()` to resolve.
|
|
264
|
+
*/
|
|
265
|
+
stop(): Promise<void>;
|
|
266
|
+
/**
|
|
267
|
+
* Returns true if the bot is currently running via built-in long polling,
|
|
268
|
+
* and false otherwise.
|
|
269
|
+
*
|
|
270
|
+
* If this method returns true, it means that `bot.start()` has been called,
|
|
271
|
+
* and that the bot has neither crashed nor was it stopped via a call to
|
|
272
|
+
* `bot.stop()`. This also means that you cannot use this method to check if
|
|
273
|
+
* a webhook server is running, or if grammY runner was started.
|
|
274
|
+
*
|
|
275
|
+
* Note that this method will already begin to return true even before the
|
|
276
|
+
* call to `bot.start()` has completed its initialization phase (and hence
|
|
277
|
+
* before `bot.isInited()` returns true). By extension, this method
|
|
278
|
+
* returns true before `onStart` callback of `bot.start()` is invoked.
|
|
279
|
+
*/
|
|
280
|
+
isRunning(): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* Sets the bots error handler that is used during long polling.
|
|
283
|
+
*
|
|
284
|
+
* You should call this method to set an error handler if you are using long
|
|
285
|
+
* polling, no matter whether you use `bot.start` or the `@grammyjs/runner`
|
|
286
|
+
* package to run your bot.
|
|
287
|
+
*
|
|
288
|
+
* Calling `bot.catch` when using other means of running your bot (or
|
|
289
|
+
* webhooks) has no effect.
|
|
290
|
+
*
|
|
291
|
+
* @param errorHandler A function that handles potential middleware errors
|
|
292
|
+
*/
|
|
293
|
+
catch(errorHandler: ErrorHandler<C>): void;
|
|
294
|
+
/**
|
|
295
|
+
* Internal. Do not call. Enters a loop that will perform long polling until
|
|
296
|
+
* the bot is stopped.
|
|
297
|
+
*/
|
|
298
|
+
private loop;
|
|
299
|
+
/**
|
|
300
|
+
* Internal. Do not call. Reliably fetches an update batch via `getUpdates`.
|
|
301
|
+
* Handles all known errors. Returns `undefined` if the bot is stopped and
|
|
302
|
+
* the call gets cancelled.
|
|
303
|
+
*
|
|
304
|
+
* @param options Polling options
|
|
305
|
+
* @returns An array of updates, or `undefined` if the bot is stopped.
|
|
306
|
+
*/
|
|
307
|
+
private fetchUpdates;
|
|
308
|
+
/**
|
|
309
|
+
* Internal. Do not call. Handles an error that occurred during long
|
|
310
|
+
* polling.
|
|
311
|
+
*/
|
|
312
|
+
private handlePollingError;
|
|
313
|
+
}
|
|
314
|
+
import { AbortSignal } from "./shim.node.js";
|