В своих обработчиках я храню данные в FSMContext, как показано ниже:
Код: Выделить всё
@admin_router.message(SelectEx.topic, F.text)
async def select_subtopic(message: types.Message, session: AsyncSession, state: FSMContext):
topic_title = message.text
await state.update_data(topic=topic_title)
subtopics = await orm_get_subtopics(session, topic_title=topic_title)
topics = await orm_get_topics(session)
my_topics = [topic.title for topic in topics]
buttons = [subtopic.title for subtopic in subtopics]
if topic_title in my_topics:
await message.answer("Well done! Choose your next challenge!",
reply_markup=get_keyboard(*buttons,
placeholder="Use buttons below",
sizes=(2,)))
await state.set_state(SelectEx.subtopic)
else:
await message.answer(
"There's no practice like that. Try using the buttons below.",
reply_markup=get_keyboard(*buttons, placeholder="Use buttons below", sizes=(2,)))
Код: Выделить всё
bot = Bot(
token=os.getenv('TOKEN'),
session=AiohttpSession(),
default=DefaultBotProperties(
parse_mode=ParseMode.HTML,
)
)
storage = MemoryStorage()
dp = Dispatcher(storage=storage)
dp.include_router(user_start_router)
dp.include_router(user_group_router)
dp.include_router(user_book_router)
dp.include_router(admin_router)
async def on_shutdown(bot: Bot):
print('Shutting down...') # Debugging line
my_data = await storage.get_data(0)
print(my_data)
print('Shutdown complete.') # Debugging line
async def main():
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
dp.update.middleware(DataBaseSession(session_pool=session_maker))
await bot.delete_webhook(drop_pending_updates=True)
await bot.set_my_commands(commands=private, scope=types.BotCommandScopeAllPrivateChats())
await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types())
asyncio.run(main())
Подробнее здесь: https://stackoverflow.com/questions/788 ... iogram-bot