refactor: streamline feature integration in bot by using a loop for feature registration; add unhandled feature handling

This commit is contained in:
vchikalkin 2025-08-23 17:00:46 +03:00
parent 5396a83150
commit 8d0c1f6d2b
2 changed files with 24 additions and 2 deletions

View File

@ -0,0 +1,17 @@
import { type Context } from '@/bot/context';
import { logHandle } from '@/bot/helpers/logging';
import { Composer } from 'grammy';
const composer = new Composer<Context>();
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 };

View File

@ -1,6 +1,7 @@
/* eslint-disable n/callback-return */ /* eslint-disable n/callback-return */
import { type Context } from './context'; import { type Context } from './context';
import * as features from './features'; import * as features from './features';
import { unhandledFeature } from './features/unhandled';
import { errorHandler } from './handlers/errors'; import { errorHandler } from './handlers/errors';
import { i18n } from './i18n'; import { i18n } from './i18n';
import * as middlewares from './middlewares'; import * as middlewares from './middlewares';
@ -66,8 +67,12 @@ export function createBot({ apiRoot, token }: Parameters_) {
protectedBot.use(middlewares.updateLogger()); protectedBot.use(middlewares.updateLogger());
protectedBot.use(autoChatAction(bot.api)); protectedBot.use(autoChatAction(bot.api));
protectedBot.use(hydrate()); 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; return bot;
} }