Skip to content

Commit

Permalink
delete session
Browse files Browse the repository at this point in the history
- save first message in the session state
- delete all messages till firstMessage - 3(command message, welcome and first exercise)
  • Loading branch information
opiruyan committed May 9, 2024
1 parent b88e8c0 commit 710a5fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
15 changes: 12 additions & 3 deletions src/Bot.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions src/GrammyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
51 changes: 34 additions & 17 deletions src/HandleUserReponse.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,8 +8,9 @@ export type BotState = {
sessions: Map<ChatId, {
currentExercise: number
currentChatId: number
session: SessionAnswers,
}>,
firstMessageId: number
session: SessionAnswers
}>
}

export type SessionAnswers = {
Expand All @@ -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: 'Да',
Expand All @@ -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++
Expand All @@ -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--
}
}

0 comments on commit 710a5fb

Please sign in to comment.