More comments and annotations in state_machine example.

This commit is contained in:
Alex Root Junior 2017-07-22 20:15:48 +03:00
parent 050b3ba113
commit 50025728c4

View file

@ -1,14 +1,11 @@
import asyncio import asyncio
import logging
from aiogram import Bot, types from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.state import StateMachine from aiogram.dispatcher.state import StateMachine, Controller
API_TOKEN = 'BOT TOKEN HERE' API_TOKEN = 'BOT TOKEN HERE'
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
bot = Bot(token=API_TOKEN, loop=loop) bot = Bot(token=API_TOKEN, loop=loop)
dp = Dispatcher(bot) dp = Dispatcher(bot)
@ -26,7 +23,7 @@ async def send_welcome(message: types.Message):
state.set_state(message.chat.id, message.from_user.id, "name") state.set_state(message.chat.id, message.from_user.id, "name")
async def process_name(message, controller): async def process_name(message: types.Message, controller: Controller):
""" """
Process user name Process user name
""" """
@ -39,7 +36,7 @@ async def process_name(message, controller):
controller.set_state('age') controller.set_state('age')
async def process_age(message, controller): async def process_age(message: types.Message, controller: Controller):
# Check age. Age must be is digit # Check age. Age must be is digit
if not message.text.isdigit(): if not message.text.isdigit():
return await message.reply("Age should be a number.\nHow old are you?") return await message.reply("Age should be a number.\nHow old are you?")
@ -58,7 +55,8 @@ async def process_age(message, controller):
controller.set_state("sex") controller.set_state("sex")
async def process_sex(message, controller): async def process_sex(message: types.Message, controller: Controller):
# Check reply
if message.text not in ["Male", "Female", "Other"]: if message.text not in ["Male", "Female", "Other"]:
return await message.reply("Bad gender name. Choose you gender from keyboard.") return await message.reply("Bad gender name. Choose you gender from keyboard.")
@ -67,6 +65,7 @@ async def process_sex(message, controller):
# Remove keyboard # Remove keyboard
markup = types.ReplyKeyboardRemove() markup = types.ReplyKeyboardRemove()
# And send message
await bot.send_message(message.chat.id, await bot.send_message(message.chat.id,
f"Hi!\n" f"Hi!\n"
f"Nice to meet you, {controller['name']}.\n" f"Nice to meet you, {controller['name']}.\n"