-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (65 loc) · 2.1 KB
/
Copy pathmain.py
File metadata and controls
82 lines (65 loc) · 2.1 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
74
75
76
77
78
79
80
81
82
"""Telegram Bot Template — main entry point with dynamic handler loading.
Replace BOT_NAME references and customize handlers/ for your project.
"""
import importlib
import logging
import sys
import os
from telegram.ext import Application
import config
from shared.db import init_pool, close_pool
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# ── Handler modules to load ──
# Each module must implement: def register(app: Application)
# Organized by phase for incremental deployment.
HANDLER_MODULES = [
# Phase 1 — Core
"handlers.help",
"handlers.welcome",
"handlers.logging_handler",
"handlers.tickets",
"handlers.announce",
# Phase 2 — Engagement
"handlers.leveling",
"handlers.giveaway",
# Add more handlers as needed:
# "handlers.your_handler_name",
]
async def post_init(application: Application):
"""Called after bot is initialized but before polling starts."""
await init_pool(config.DATABASE_URL)
logger.info("Database pool initialized")
async def post_shutdown(application: Application):
"""Called after polling stops."""
await close_pool()
logger.info("Database pool closed")
def main():
if not config.BOT_TOKEN:
logger.error("BOT_TOKEN not set in .env")
sys.exit(1)
app = (
Application.builder()
.token(config.BOT_TOKEN)
.post_init(post_init)
.post_shutdown(post_shutdown)
.build()
)
# Dynamically load handler modules
for module_name in HANDLER_MODULES:
try:
mod = importlib.import_module(module_name)
if hasattr(mod, "register"):
mod.register(app)
logger.info(f"Loaded: {module_name}")
else:
logger.warning(f"No register() in {module_name}, skipped")
except Exception as e:
logger.error(f"Failed to load {module_name}: {e}")
logger.info("Starting bot...")
app.run_polling(drop_pending_updates=True)
if __name__ == "__main__":
main()