From 1edf116f6c49476310e7310cb187c6bcb9aa6f3a Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Wed, 16 Oct 2024 13:28:12 +0200 Subject: [PATCH 1/3] style: add override keyword --- src/bot.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 52e36663..3df66a54 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -255,7 +255,7 @@ export class Bot< /** * @inheritdoc */ - on( + override on( filter: Q | Q[], ...middleware: Array>> ): Composer> { @@ -267,7 +267,7 @@ export class Bot< /** * @inheritdoc */ - reaction( + override reaction( reaction: MaybeArray, ...middleware: Array> ): Composer> { From d9ec3acf08f0ae256c0ca90bdfc4d0e103068877 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Wed, 16 Oct 2024 13:38:23 +0200 Subject: [PATCH 2/3] feat: bot.isRunning --- src/bot.ts | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 3df66a54..b252b795 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -85,6 +85,9 @@ export interface PollingOptions { * fetched. The bot information `bot.botInfo` will be available when the * function is run. For convenience, the callback function receives the * value of `bot.botInfo` as an argument. + * + * When this function is invoked, the bot already signals that it is + * running. In other words, `bot.isRunning()` already returns true. */ onStart?: (botInfo: UserFromGetMe) => void | Promise; } @@ -495,6 +498,25 @@ a known bot info object.", } } + /** + * Returns true if the bot is currently running via built-in long polling, + * and false otherwise. + * + * If this method returns true, it means that `bot.start()` has been called, + * and that the bot has neither crashed nor was it stopped via a call to + * `bot.stop()`. This also means that you cannot use this method to check if + * a webhook server is running, or if grammY runner was started. + * + * Note that this method will already begin to return true even before the + * call to `bot.start()` has completed its initialization phase. During that + * period, it is `bot.isRunning() && !bot.isInited()`. By extension, this + * method returns true before `onStart` callback of `bot.start()` is + * invoked. + */ + isRunning() { + return this.pollingRunning; + } + /** * Sets the bots error handler that is used during long polling. * @@ -521,18 +543,22 @@ a known bot info object.", let allowed_updates: PollingOptions["allowed_updates"] = options?.allowed_updates ?? []; // reset to default if unspecified - while (this.pollingRunning) { - // fetch updates - const updates = await this.fetchUpdates( - { limit, timeout, allowed_updates }, - ); - // check if polling stopped - if (updates === undefined) break; - // handle updates - await this.handleUpdates(updates); - // Telegram uses the last setting if `allowed_updates` is omitted so - // we can save some traffic by only sending it in the first request - allowed_updates = undefined; + try { + while (this.pollingRunning) { + // fetch updates + const updates = await this.fetchUpdates( + { limit, timeout, allowed_updates }, + ); + // check if polling stopped + if (updates === undefined) break; + // handle updates + await this.handleUpdates(updates); + // Telegram uses the last setting if `allowed_updates` is omitted so + // we can save some traffic by only sending it in the first request + allowed_updates = undefined; + } + } finally { + this.pollingRunning = false; } } From 214253ce4671f5f23d6e52ae62b47c66bc186f6b Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Wed, 16 Oct 2024 14:06:17 +0200 Subject: [PATCH 3/3] docs: clarify order of state transitions --- src/bot.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index b252b795..0e94c381 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -508,10 +508,9 @@ a known bot info object.", * a webhook server is running, or if grammY runner was started. * * Note that this method will already begin to return true even before the - * call to `bot.start()` has completed its initialization phase. During that - * period, it is `bot.isRunning() && !bot.isInited()`. By extension, this - * method returns true before `onStart` callback of `bot.start()` is - * invoked. + * call to `bot.start()` has completed its initialization phase (and hence + * before `bot.isInited()` returns true). By extension, this method + * returns true before `onStart` callback of `bot.start()` is invoked. */ isRunning() { return this.pollingRunning;