Refactor and improve bot messages

Refactored bot code to use aiogram enumerations and enhanced chat messages with markdown beautifications for a more user-friendly display.

CommandStart() is now used instead of Command('start') for readability.

Furthermore, the bot's 'stop' command was improved, ensuring it executes appropriately during KeyboardInterrupt or SystemExit.

Additionally, the bot's logging was adjusted to output to sys.stdout for better logs' readability.
This commit is contained in:
latan 2023-08-10 11:46:22 +03:00
parent 68c0516f69
commit 518739724c
7 changed files with 63 additions and 45 deletions

View file

@ -5,7 +5,8 @@ from os import getenv
from typing import Any, Dict
from aiogram import Bot, Dispatcher, F, Router, html
from aiogram.filters import Command
from aiogram.enums import ParseMode
from aiogram.filters import Command, CommandStart
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import (
@ -24,7 +25,7 @@ class Form(StatesGroup):
language = State()
@form_router.message(Command("start"))
@form_router.message(CommandStart())
async def command_start(message: Message, state: FSMContext) -> None:
await state.set_state(Form.name)
await message.answer(
@ -91,7 +92,7 @@ async def process_like_write_bots(message: Message, state: FSMContext) -> None:
@form_router.message(Form.like_bots)
async def process_unknown_write_bots(message: Message, state: FSMContext) -> None:
async def process_unknown_write_bots(message: Message) -> None:
await message.reply("I don't understand you :(")
@ -99,12 +100,12 @@ async def process_unknown_write_bots(message: Message, state: FSMContext) -> Non
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)
if message.text.casefold() == "python":
await message.reply(
"Python, you say? That's the language that makes my circuits light up! 😉"
)
await show_summary(message=message, data=data)
@ -121,7 +122,7 @@ async def show_summary(message: Message, data: Dict[str, Any], positive: bool =
async def main():
bot = Bot(token=getenv("TELEGRAM_TOKEN"), parse_mode="HTML")
bot = Bot(token=getenv("TELEGRAM_TOKEN"), parse_mode=ParseMode.HTML)
dp = Dispatcher()
dp.include_router(form_router)
@ -130,4 +131,7 @@ async def main():
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logging.info("Bot stopped!")