From 5f1ad4028ce1b9ea09807b97b1e1023240a907c1 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sun, 7 Sep 2025 16:09:39 +0300 Subject: [PATCH] feat(bot): add cancel command to conversation middleware - Implemented a '/cancel' command in the bot's conversation middleware to allow users to exit all conversations gracefully. - Removed the manual cancellation check from the addContact conversation, streamlining the code and improving user experience. --- apps/bot/src/bot/conversations/add-contact.ts | 5 ----- apps/bot/src/bot/index.ts | 5 ++++- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/apps/bot/src/bot/conversations/add-contact.ts b/apps/bot/src/bot/conversations/add-contact.ts index 479f388..aa37d6f 100644 --- a/apps/bot/src/bot/conversations/add-contact.ts +++ b/apps/bot/src/bot/conversations/add-contact.ts @@ -34,11 +34,6 @@ export async function addContact(conversation: Conversation, c // Ждем любое сообщение от пользователя const waitCtx = await conversation.wait(); - // Проверяем команду отмены - if (waitCtx.message?.text === '/cancel') { - return ctx.reply(await conversation.external(({ t }) => t('msg-cancel'))); - } - // Проверяем, что отправлен контакт if (!waitCtx.message?.contact) { return ctx.reply(await conversation.external(({ t }) => t('msg-send-contact'))); diff --git a/apps/bot/src/bot/index.ts b/apps/bot/src/bot/index.ts index 0717f44..f3448e6 100644 --- a/apps/bot/src/bot/index.ts +++ b/apps/bot/src/bot/index.ts @@ -38,7 +38,10 @@ export function createBot({ token }: Parameters_) { }), ); - bot.use(grammyConversations()); + bot.use(grammyConversations()).command('cancel', async (ctx) => { + await ctx.conversation.exitAll(); + await ctx.reply(ctx.t('msg-cancel')); + }); for (const conversation of Object.values(conversations)) { bot.use(createConversation(conversation));