-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (58 loc) · 1.96 KB
/
app.py
File metadata and controls
73 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# app.py
"""
Main application file for AutoCare Telegram Bot.
This bot helps users find nearby autoservices and carwashes
in Uzbekistan using location-based search.
"""
import asyncio
import logging
import os
from aiogram import Bot, Dispatcher
from handlers import setup_routers
from database import init_db, close_db
from middlewares.rate_limit import RateLimitMiddleware
def setup_logging() -> None:
"""Configure logging with proper format and level."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
# Reduce noise from some libraries
logging.getLogger("aiogram.event").setLevel(logging.WARNING)
async def main() -> None:
"""
Main entry point for the bot application.
Initializes database, sets up bot and dispatcher,
registers handlers and middlewares, then starts polling.
"""
setup_logging()
# Initialize database
try:
await init_db()
logging.info("✅ Database initialized successfully")
except Exception as e:
logging.error(f"❌ Database initialization error: {e}")
return
# Get bot token from environment
api_token = os.getenv("BOT_TOKEN")
if not api_token:
logging.error("❌ BOT_TOKEN environment variable not found")
return
bot = Bot(token=api_token)
dp = Dispatcher()
# Register rate limiting middleware (1 request per second per user)
dp.message.middleware(RateLimitMiddleware(rate_limit=1.0))
dp.callback_query.middleware(RateLimitMiddleware(rate_limit=0.5))
# Setup routers
setup_routers(dp)
try:
logging.info("🤖 Bot starting...")
await dp.start_polling(bot)
except Exception as e:
logging.error(f"❌ Bot startup error: {e}")
finally:
await close_db()
await bot.session.close()
logging.info("👋 Bot stopped")
if __name__ == "__main__":
asyncio.run(main())