Как передать данные State в Callback?
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import ParseMode
from aiogram.utils import executor
API_TOKEN = "TOKEN"
bot = Bot(token=API_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
class Form(StatesGroup):
name = State()
age = State()
@dp.message_handler(commands='start')
async def cmd_start(message: types.Message):
await Form.name.set()
await message.reply("Your name?")
@dp.message_handler(state=Form.name)
async def process_name(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data["name"] = message.text
await Form.next()
await message.reply("How old are you?")
@dp.message_handler(state=Form.age)
async def process_age(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data["age"] = message.text
await state.finish()
keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton(text = "Click the button", callback_data = "send_storage"))
await bot.send_message(message.from_user.id, "Click me", reply_markup = keyboard)
@dp.callback_query_handler(state = Form)
async def process_callback(call, state: FSMContext):
if call.data == "send_storage":
print("Here")
#И сюда нужно вывести name и age
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)
Ответы (1 шт):
Автор решения: Алексей
→ Ссылка
Добавить еще одно состояние:
fin = State()
потом, вместо
state.finish() -> Form.next()
и последний обработчик так
@dp.callback_query_handler(text = 'send_storage', state=Form.fin)
async def process_callback(call: types.CallbackQuery, state: FSMContext):
await call.answer()
if call.data == "send_storage":
print("Here")
print(await state.get_data())
#И сюда нужно вывести name и age
await state.finish()