diff --git a/src/Bot.ts b/src/Bot.ts index ca383ad..c98bf61 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -1,9 +1,7 @@ import { Bot } from 'grammy' import 'dotenv/config' -import { messages } from './Messages' -import { handleResponse } from './HandleUserReponse' +import { STATE_STAGE, deleteMessagesInChat, handleResponse, processUserResponse } from './HandleUserReponse' import { state } from './State' -import { createInlineKeyboard } from './GrammyAdapter' import { handleStartCommand } from './HandleCommands' if (!process.env.BOT_TOKEN) { @@ -17,7 +15,18 @@ bot.command('start', (ctx) => { }) bot.on("message:text", (ctx) => { + processUserResponse(ctx.msg.chat.id, state, ctx.message) handleResponse(ctx.msg.chat.id, state, ctx.message.text) + + let session = state.sessions.get(ctx.msg.chat.id) + if (!session) { + throw new Error('Session not found') + } + + if (session.currentExercise === STATE_STAGE.SHOW_RESULTS) { + deleteMessagesInChat(ctx.msg.chat.id, session.firstMessageId, ctx.msg.message_id) + session.currentExercise = 0 + } }) bot.on("callback_query:data", (ctx) => { diff --git a/src/GrammyAdapter.ts b/src/GrammyAdapter.ts index 278c5c1..1cd5467 100644 --- a/src/GrammyAdapter.ts +++ b/src/GrammyAdapter.ts @@ -111,4 +111,8 @@ export function showIntro(chatId: number) { bot.api.sendMessage(chatId, messages.welcomeMessage, { reply_markup: createKeyboardButtons([messages.exercise1, messages.help.button]), }) +} + +export function deleteMessage(chatId: number, messageId: number) { + bot.api.deleteMessage(chatId, messageId) } \ No newline at end of file diff --git a/src/HandleUserReponse.ts b/src/HandleUserReponse.ts index 4afa7dc..97e7494 100644 --- a/src/HandleUserReponse.ts +++ b/src/HandleUserReponse.ts @@ -1,4 +1,5 @@ -import { hideKeyboard, showFirstExercise, showFourthExercise, showHelp, showIntro, showResults, showSecondExercise, showThirdExercise } from './GrammyAdapter' +import { Message } from 'grammy/types' +import { deleteMessage, hideKeyboard, showFirstExercise, showFourthExercise, showHelp, showIntro, showResults, showSecondExercise, showThirdExercise } from './GrammyAdapter' import { messages } from './Messages' type ChatId = number @@ -7,8 +8,9 @@ export type BotState = { sessions: Map, + firstMessageId: number + session: SessionAnswers + }> } export type SessionAnswers = { @@ -26,24 +28,13 @@ export const STATE_STAGE = { SHOW_RESULTS: 5, } as const -export async function handleResponse(chatId: number, state: BotState, message: string) { +export async function processUserResponse(chatId: number, state: BotState, message: Message) { let session = state.sessions.get(chatId) - - if (message === messages.exit) { - state.sessions.delete(chatId) - hideKeyboard(chatId) - return - } - - if (message === messages.help.button) { - showHelp(chatId) - return - } - if (!session) { session = { currentExercise: 0, currentChatId: chatId, + firstMessageId: message.message_id, session: { catchingThoughts: 'Думаю о себе', balanceCoin: 'Да', @@ -53,6 +44,24 @@ export async function handleResponse(chatId: number, state: BotState, message: s } state.sessions.set(chatId, session) } +} + +export async function handleResponse(chatId: number, state: BotState, message: string) { + let session = state.sessions.get(chatId) + if (!session) { + throw new Error('Session not found') + } + + if (message === messages.exit) { + state.sessions.delete(chatId) + hideKeyboard(chatId) + return + } + + if (message === messages.help.button) { + showHelp(chatId) + return + } if (message !== messages.goBack) { session.currentExercise++ @@ -79,10 +88,18 @@ export async function handleResponse(chatId: number, state: BotState, message: s case STATE_STAGE.SHOW_RESULTS: session.session.compass = message as SessionAnswers['compass'] await showResults(session.currentChatId) - session.currentExercise = 0 hideKeyboard(chatId) break default: showIntro(chatId) } } + + +export function deleteMessagesInChat(chatId: number, stopMessageId: number, latestMessageId: number) { + let currentMessageId = latestMessageId + while (currentMessageId > stopMessageId - 3) { + deleteMessage(chatId, currentMessageId) + currentMessageId-- + } +} \ No newline at end of file