Структура проекта:
├── handlers
│ ├── commands.py # Обработчики команд
│ ├── text.py # Обработчики текстовых сообщений
│ └── ...
└── main.py # Главный файл
main.py:
from aiogram import Bot, Dispatcher, executor, types
from handlers import commands, text
bot = Bot(token="YOUR_BOT_TOKEN")
dp = Dispatcher(bot)
# Регистрируем обработчики из модулей
dp.register_message_handler(commands.start_command, commands=["start"])
dp.register_message_handler(commands.help_command, commands=["help"])
dp.register_message_handler(text.text_handler)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
commands.py:
from aiogram import types
async def start_command(message: types.Message):
await message.answer("Привет! Это команда /start")
async def help_command(message: types.Message):
await message.answer("Это команда /help")
text.py:
from aiogram import types
async def text_handler(message: types.Message):
if message.text.startswith("Привет"):
await message.answer("И тебе привет!")
else:
await message.answer("Я не понимаю твоё сообщение")