From 5f6af637e8a02df271137a5eeb4d4165e7e6dd34 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Wed, 2 Aug 2017 07:39:26 +0300 Subject: [PATCH] More comments in FSM example. --- examples/finite_state_machine_example.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/finite_state_machine_example.py b/examples/finite_state_machine_example.py index 968f37a0..c6c68873 100644 --- a/examples/finite_state_machine_example.py +++ b/examples/finite_state_machine_example.py @@ -9,7 +9,10 @@ from aiogram.utils.markdown import text, bold API_TOKEN = 'BOT TOKEN HERE' loop = asyncio.get_event_loop() + bot = Bot(token=API_TOKEN, loop=loop) + +# For example use simple MemoryStorage for Dispatcher. storage = MemoryStorage() dp = Dispatcher(bot, storage=storage) @@ -40,8 +43,12 @@ async def cancel_handler(message: types.Message): Allow to cancel any action """ with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state: + # Ignore command is user is not in any (defined) state if await state.get_state() is None: return + + # Otherwise cancel state and inform user about that. + # And just in case remove keyboard. await state.reset_state(with_data=True) await message.reply('Canceled.', reply_markup=types.ReplyKeyboardRemove()) @@ -117,6 +124,7 @@ async def main(): # Skip old updates count = await dp.skip_updates() print(f"Skipped {count} updates.") + await dp.start_pooling()