Files
Telegram-bot-old/main.py

69 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.types import BotCommand
from handlers import routers
from Middleware.anti_spam_middleware import AntiSpamMiddleware
import logging
# Получение токена бота из переменных окружения
BOT_TOKEN = os.getenv("TOKEN")
if not BOT_TOKEN:
raise ValueError(
"Не задан токен бота. Убедитесь, что переменная окружения 'TOKEN' установлена.")
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
# Установка middleware для защиты от спама
dp.message.middleware(AntiSpamMiddleware(rate_limit=1))
async def set_commands():
"""Устанавливает команды для бота."""
commands = [
BotCommand(command="start", description="Запуск бота"),
BotCommand(command="subscriptions", description="Мои подписки"),
BotCommand(command="support", description="Техподдержка"),
BotCommand(command="referrals", description="Реферальная программа"),
]
await bot.set_my_commands(commands)
async def on_startup():
"""Действия при запуске бота."""
await set_commands()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Бот запущен!")
async def on_shutdown():
"""Действия при остановке бота."""
await bot.session.close()
print("Бот остановлен.")
async def main():
"""Основной цикл работы бота."""
for router in routers:
dp.include_router(router) # Регистрация хендлеров
await on_startup()
try:
# Запуск polling
await dp.start_polling(bot)
finally:
# Действия при завершении работы
await on_shutdown()
if __name__ == "__main__":
asyncio.run(main())