是的,aiogram中存在register_next_step_handler函数,该函数用于注册下一步信息处理程序。以下是一个代码示例:
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import Message
from aiogram import Bot, Dispatcher, types
from aiogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove, KeyboardButton
bot = Bot(token='TOKEN_HERE')
dp = Dispatcher(bot)
class Form(StatesGroup):
name = State()
age = State()
gender = State()
@dp.message_handler(commands='start')
async def process_start_command(message: Message):
await message.reply("Hi there! What's your name?")
await Form.name.set()
async def process_name(message: Message):
async with state.proxy() as data:
data['name'] = message.text
await message.reply("How old are you?")
await Form.next()
async def process_age(message: Message):
async with state.proxy() as data:
data['age'] = message.text
# Remove keyboard
markup = ReplyKeyboardRemove()
# And send message
await bot.send_message(
message.chat.id,
"What is your gender?",
reply_markup=markup
)
# Next step
await Form.next()
async def process_gender(message: Message):
async with state.proxy() as data:
data['gender'] = message.text
# Do something with the collected data
await message.reply("Thank you for helping us to improve!")
# Finish conversation
await state.finish()
# Register state handlers
dp.register_message_handler(process_name, state=Form.name)
dp.register_message_handler(process_age, state=Form.age)
dp.register_message_handler(process_gender, state=Form.gender)
# Register next_step_handler
dp.register_message_handler(process_name, commands='help', state='*')
dp.register_message_handler(process_age, state=Form.name)
dp.register_message_handler(process_gender, state=Form.age)