More docs

This commit is contained in:
Alex Root Junior 2021-10-12 01:11:53 +03:00
parent 3931253a88
commit f97367b3ee
16 changed files with 415 additions and 100 deletions

View file

@ -2,40 +2,34 @@ import asyncio
import logging
import sys
from os import getenv
from typing import Any, Dict
from aiogram import Bot, Dispatcher, F
from aiogram.dispatcher.filters import Command
from aiogram import Bot, Dispatcher, F, Router, html
from aiogram.dispatcher.fsm.context import FSMContext
from aiogram.dispatcher.fsm.state import State, StatesGroup
from aiogram.types import KeyboardButton, Message, ReplyKeyboardMarkup, ReplyKeyboardRemove
from aiogram.utils.keyboard import KeyboardBuilder
from aiogram.utils.markdown import hbold
GENDERS = ["Male", "Female", "Helicopter", "Other"]
dp = Dispatcher()
form_router = Router()
# 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'
name = State()
like_bots = State()
language = State()
@dp.message(Command(commands=["start"]))
async def cmd_start(message: Message, state: FSMContext):
"""
Conversation's entry point
"""
# Set state
@form_router.message(commands={"start"})
async def command_start(message: Message, state: FSMContext) -> None:
await state.set_state(Form.name)
await message.answer("Hi there! What's your name?")
await message.answer(
"Hi there! What's your name?",
reply_markup=ReplyKeyboardRemove(),
)
@dp.message(Command(commands=["cancel"]))
@dp.message(F.text.lower() == "cancel")
async def cancel_handler(message: Message, state: FSMContext):
@form_router.message(commands={"cancel"})
@form_router.message(F.text.casefold() == "cancel")
async def cancel_handler(message: Message, state: FSMContext) -> None:
"""
Allow user to cancel any action
"""
@ -44,64 +38,86 @@ async def cancel_handler(message: Message, state: FSMContext):
return
logging.info("Cancelling state %r", current_state)
# Cancel state and inform user about it
await state.clear()
# And remove keyboard (just in case)
await message.answer("Cancelled.", reply_markup=ReplyKeyboardRemove())
@dp.message(Form.name)
async def process_name(message: Message, state: FSMContext):
"""
Process user name
"""
await state.update_data(name=message.text)
await state.set_state(Form.age)
await message.answer("How old are you?")
# Check age. Age gotta be digit
@dp.message(Form.age, ~F.text.isdigit())
async def process_age_invalid(message: Message):
"""
If age is invalid
"""
return await message.answer("Age gotta be a number.\nHow old are you? (digits only)")
@dp.message(Form.age)
async def process_age(message: Message, state: FSMContext):
# Update state and data
await state.set_state(Form.gender)
await state.update_data(age=int(message.text))
# Configure ReplyKeyboardMarkup
constructor = KeyboardBuilder(KeyboardButton)
constructor.add(*(KeyboardButton(text=text) for text in GENDERS)).adjust(2)
markup = ReplyKeyboardMarkup(
resize_keyboard=True, selective=True, keyboard=constructor.export()
)
await message.reply("What is your gender?", reply_markup=markup)
@dp.message(Form.gender)
async def process_gender(message: Message, state: FSMContext):
data = await state.update_data(gender=message.text)
await state.clear()
# And send message
await message.answer(
(
f'Hi, nice to meet you, {hbold(data["name"])}\n'
f'Age: {hbold(data["age"])}\n'
f'Gender: {hbold(data["gender"])}\n'
),
"Cancelled.",
reply_markup=ReplyKeyboardRemove(),
)
@form_router.message(Form.name)
async def process_name(message: Message, state: FSMContext) -> None:
await state.update_data(name=message.text)
await state.set_state(Form.like_bots)
await message.answer(
f"Nice to meet you, {html.quote(message.text)}!\nDid you like to write bots?",
reply_markup=ReplyKeyboardMarkup(
keyboard=[
[
KeyboardButton(text="Yes"),
KeyboardButton(text="No"),
]
],
resize_keyboard=True,
),
)
@form_router.message(Form.like_bots, F.text.casefold() == "no")
async def process_dont_like_write_bots(message: Message, state: FSMContext) -> None:
data = await state.get_data()
await state.clear()
await message.answer(
"Not bad not terrible.\nSee you soon.",
reply_markup=ReplyKeyboardRemove(),
)
await show_summary(message=message, data=data, positive=False)
@form_router.message(Form.like_bots, F.text.casefold() == "yes")
async def process_like_write_bots(message: Message, state: FSMContext) -> None:
await state.set_state(Form.language)
await message.reply(
"Cool! I'm too!\nWhat programming language did you use for it?",
reply_markup=ReplyKeyboardRemove(),
)
@form_router.message(Form.like_bots)
async def process_unknown_write_bots(message: Message, state: FSMContext) -> None:
await message.reply("I don't understand you :(")
@form_router.message(Form.language)
async def process_language(message: Message, state: FSMContext) -> None:
data = await state.update_data(language=message.text)
await state.clear()
text = (
"Thank for all! Python is in my hearth!\nSee you soon."
if message.text.casefold() == "python"
else "Thank for information!\nSee you soon."
)
await message.answer(text)
await show_summary(message=message, data=data)
async def show_summary(message: Message, data: Dict[str, Any], positive: bool = True) -> None:
name = data["name"]
language = data.get("language", "<something unexpected>")
text = f"I'll keep in mind that, {html.quote(name)}, "
text += (
f"you like to write bots with {html.quote(language)}."
if positive
else "you don't like to write bots, so sad..."
)
await message.answer(text=text, reply_markup=ReplyKeyboardRemove())
async def main():
bot = Bot(token=getenv("TELEGRAM_TOKEN"), parse_mode="HTML")
dp = Dispatcher()
dp.include_router(form_router)
await dp.start_polling(bot)