Skip to content

Commit

Permalink
fix: improve error handling when the bot is busy
Browse files Browse the repository at this point in the history
Signed-off-by: Eiko Wagenknecht <[email protected]>
  • Loading branch information
eikowagenknecht committed Feb 10, 2025
1 parent de4f3c0 commit f7df05a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/services/telegrambot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ export class TelegramBotService {
private startBroadcastCheck(): void {
const checkNewMessages = async () => {
if (this.sendingBroadcast) {
logger.verbose(
"Telegram service is already broadcasting messages, skipping check.",
);
logger.verbose("Telegram service is busy broadcasting messages.");
return;
}
this.sendingBroadcast = true;
Expand Down
21 changes: 16 additions & 5 deletions src/services/telegrambot/handlers/callbacks/router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { telegramBotService } from "@/services/telegrambot";
import type { BotContext } from "@/services/telegrambot/types/middleware";
import { unpackFirstField } from "@/services/telegrambot/utils/callbackPack";
import { logger } from "@/utils/logger";
Expand Down Expand Up @@ -53,12 +54,22 @@ export async function handleCallback(
});
}
} catch (error) {
logger.error(
logger.warn(
`Error handling callback: ${error instanceof Error ? error.message : String(error)}`,
);
await ctx.answerCallbackQuery({
text: "An error occurred",
show_alert: true,
});
try {
if (!ctx.chatId) {
return;
}

await telegramBotService.sendWithTimeout(
ctx.chatId,
"Couldn't handle the button press in time. This happens when too many users are using the bot at the same time and thus it gets rate-limited by Telegram. Please try again later.",
);
} catch (error) {
logger.error(
`Failed to notify user of callback error: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
}
2 changes: 1 addition & 1 deletion src/services/telegrambot/handlers/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function logCall(ctx: Context): void {
? `command "${ctx.message.text}"`
: "unknown command";

logger.debug(`Received ${commandName} from ${getCallerName(ctx)}.`);
logger.verbose(`Received ${commandName} from ${getCallerName(ctx)}.`);
}

export function getCallerName(ctx: Context): string {
Expand Down
2 changes: 1 addition & 1 deletion src/services/telegrambot/handlers/commands/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function handleManageCommand(
export async function buildManageKeyboard(chatId: number) {
const inlineKeyboard = new InlineKeyboard();

logger.verbose(`Building manage keyboard for chat ${chatId.toFixed()}`);
logger.debug(`Building manage keyboard for chat ${chatId.toFixed()}`);

// Add subscription toggle buttons for each source/type/duration/platform combination
const combinations = getEnabledFeedCombinations();
Expand Down

0 comments on commit f7df05a

Please sign in to comment.