Используя официальный пример,. import logging import…
- User
Assistant: User: Используя официальный пример,. import logging
import aiogram.utils.markdown as md 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
logging.basicConfig(level=logging.INFO)
API_TOKEN = "BOT TOKEN HERE"
bot = Bot(token=API_TOKEN)
For example use simple MemoryStorage for Dispatcher.
storage = MemoryStorage() dp = Dispatcher(bot, storage=storage)
States
class Form(StatesGroup): name = State() # Will be represented in storage as "Form:name" age = State() # Will be represented in storage as "Form:age" gender = State() # Will be represented in storage as "Form:gender"
@dp.message_handler(commands="start") async def cmd_start(message: types.Message): """ Conversation"s entry point """ # Set state await Form.name.set()
codeawait message.reply("Hi there! What"s your name?")You can use state "*" if you need to handle all states
@dp.message_handler(state="", commands="cancel") @dp.message_handler(Text(equals="cancel", ignore_case=True), state="") async def cancel_handler(message: types.Message, state: FSMContext): """ Allow user to cancel any action """ current_state = await state.get_state() if current_state is None: return
codelogging.info("Cancelling state %r", current_state) # Cancel state and inform user about it await state.finish() # And remove keyboard (just in case) await message.reply("Cancelled.", reply_markup=types.ReplyKeyboardRemove())@dp.message_handler(state=Form.name) async def process_name(message: types.Message, state: FSMContext): """ Process user name """ async with state.proxy() as data: data["name"] = message.text
codeawait Form.next() await message.reply("How old are you?")Check age. Age gotta be digit
@dp.message_handler(lambda message: not message.text.isdigit(), state=Form.age) async def process_age_invalid(message: types.Message): """ If age is invalid """ return await message.reply("Age gotta be a number. How old are you? (digits only)")
@dp.message_handler(lambda message: message.text.isdigit(), state=Form.age) async def process_age(message: types.Message, state: FSMContext): # Update state and data await Form.next() await state.update_data(age=int(message.text))
- Assistant