From 8d0c1f6d2bbe1047e098eb486c4ead802e0e5080 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sat, 23 Aug 2025 17:00:46 +0300 Subject: [PATCH] refactor: streamline feature integration in bot by using a loop for feature registration; add unhandled feature handling --- apps/bot/src/bot/features/unhandled.ts | 17 +++++++++++++++++ apps/bot/src/bot/index.ts | 9 +++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 apps/bot/src/bot/features/unhandled.ts diff --git a/apps/bot/src/bot/features/unhandled.ts b/apps/bot/src/bot/features/unhandled.ts new file mode 100644 index 0000000..b376890 --- /dev/null +++ b/apps/bot/src/bot/features/unhandled.ts @@ -0,0 +1,17 @@ +import { type Context } from '@/bot/context'; +import { logHandle } from '@/bot/helpers/logging'; +import { Composer } from 'grammy'; + +const composer = new Composer(); + +const feature = composer.chatType('private'); + +feature.on('message', logHandle('unhandled-message'), (ctx) => { + return ctx.reply(ctx.t('msg-unhandled')); +}); + +feature.on('callback_query', logHandle('unhandled-callback-query'), (ctx) => { + return ctx.answerCallbackQuery(); +}); + +export { composer as unhandledFeature }; diff --git a/apps/bot/src/bot/index.ts b/apps/bot/src/bot/index.ts index a786c1e..48b976b 100644 --- a/apps/bot/src/bot/index.ts +++ b/apps/bot/src/bot/index.ts @@ -1,6 +1,7 @@ /* eslint-disable n/callback-return */ import { type Context } from './context'; import * as features from './features'; +import { unhandledFeature } from './features/unhandled'; import { errorHandler } from './handlers/errors'; import { i18n } from './i18n'; import * as middlewares from './middlewares'; @@ -66,8 +67,12 @@ export function createBot({ apiRoot, token }: Parameters_) { protectedBot.use(middlewares.updateLogger()); protectedBot.use(autoChatAction(bot.api)); protectedBot.use(hydrate()); - protectedBot.use(features.welcome); - protectedBot.use(features.download); + + for (const feature of Object.values(features)) { + protectedBot.use(feature); + } + + protectedBot.use(unhandledFeature); return bot; }