0

There was a problem when writing a telegram bot with a primitive economy. The first idea was to make a simple game in which you had to solve examples, but I ran into a problem.

Briefly about the game: To enter the start "menu" of this game, you had to press a button from another menu, or exit the game itself (return to the menu).

The game starts and runs fine. If you solve the example correctly - it will answer, if wrong - also answer. However, it is impossible to exit this game using the buttons that it offers. You have to either scroll through the chat with the bot to find a button in other menus (there verified_user state is automatically transferred to the user for further normal work), or stop the bot.

I expect the kudos_calculator and kudos_calculator_playing handlers to work fine. So that you can exit the game or restart it by using them.

*Sorry, but this text i dropped into translator, so if there any issues with it - please note them. * Code here (part from handlers.py and full code from kb.py)

@router.callback_query(lambda c: c.data == 'finance_menu')
async def finance_menu(cbq: types.CallbackQuery, state: FSMContext):
    await cbq.message.answer('Вот твое финансовое меню (ЭТОТ ТЕКСТ НЕ ИЗ TEXT .PY, ЕГО НУЖНО ТУДА КИНУТЬ)',
                             reply_markup=kb.fin_menu) 
    await state.set_state(Verified.verified_user)


@router.callback_query(lambda c: c.data == 'kudos_calc')
@router.callback_query(lambda c: c.data == 'kudos_calc_finish')
async def kudos_calculator(cbq: types.CallbackQuery, state: FSMContext):
    await cbq.message.answer('Добро пожаловать в Kudos Calculator! Это первая игра, где ты сможешь зарабатывать '
                             'KUDOS! Тебе нужно всего лишь решать математические примеры! Чтобы начать, '
                             'нажми на кнопку "Начать игру" (НЕ ИЗ ФАЙЛА ТЕКСТА)', reply_markup=kb.kudos_calc)


@router.callback_query(lambda c: c.data == 'kudos_calc_start')
@router.callback_query(lambda c: c.data == 'kudos_calc_restart')
async def kudos_calc_playing(cbq: types.CallbackQuery, state: FSMContext):
    global a_value, b_value, c_value
    a_value = randint(1, 999)
    b_value = randint(1, 999)
    c_value = a_value + b_value
    await cbq.message.answer(f"{text.kudos_calc_solve}<b>{a_value}+{b_value}</b>?", reply_markup=kb.kudos_calc_quit)
    await state.set_state(Verified.kudos_calc_solving)


@router.message(Verified.kudos_calc_solving)
async def kudos_calc_solving(msg: Message, state: FSMContext):
    kudos = randint(1,3)
    if str(msg.text) == str(c_value):
        await msg.answer(f'Ты угадал! На твой счет зачислено {kudos} KUDOS', reply_markup=kb.kudos_calc_finish)
    else:
        await msg.answer('К сожалению, ты был не прав! Попробуй решить другой пример!', reply_markup=kb.kudos_calc_finish)

kb.py here:

from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
game_start_btn = InlineKeyboardButton(text='Начать регистрацию', callback_data='start_reg')
game_start = InlineKeyboardMarkup(inline_keyboard=[
    [game_start_btn],
])

menu_transfer_btn = InlineKeyboardButton(text='Перейти в меню', callback_data='to_menu_transfer')
menu_transfer = InlineKeyboardMarkup(inline_keyboard=[
    [menu_transfer_btn],
])

menu_b1 = InlineKeyboardButton(text='Мой профиль', callback_data='my_profile')
menu_b2 = InlineKeyboardButton(text='Меню финансов', callback_data='finance_menu')
menu_b3 = InlineKeyboardButton(text='Топы', callback_data='top_menu')
menu_b4 = InlineKeyboardButton(text='Настройки', callback_data='settings')   # ВНЕ ИСПОЛЬЗОВАНИЯ НА МОМЕНТ 26.06.2023
menu_b5 = InlineKeyboardButton(text='Информация (Credits)', callback_data='creator_info')

menu = InlineKeyboardMarkup(inline_keyboard=[
    [menu_b1],[menu_b2],
    [menu_b3],[menu_b5],
])

fin_menu_b1 = InlineKeyboardButton(text='Заработать в Kudos калькуляторе', callback_data='kudos_calc')

fin_menu = InlineKeyboardMarkup(inline_keyboard=[
    [fin_menu_b1],[menu_transfer_btn],
])

kudos_calc_b1 = InlineKeyboardButton(text='Начать игру', callback_data='kudos_calc_start')
kudos_calc_b2 = KeyboardButton(text='Выйти из игры', callback_data='kudos_calc_finish')
kudos_calc_b3 = KeyboardButton(text='Ещё один пример', callback_data='kudos_calc_restart')

kudos_calc = InlineKeyboardMarkup(inline_keyboard=[
    [kudos_calc_b1],[menu_b2],
])

kudos_calc_quit = ReplyKeyboardMarkup(keyboard=[
    [kudos_calc_b2]
], resize_keyboard = True)

kudos_calc_finish = ReplyKeyboardMarkup(keyboard=[
    [kudos_calc_b3],[kudos_calc_b2]
], resize_keyboard = True, input_field_placeholder='Чего бы тебе хотелось?')

0 Answers0