-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (82 loc) · 2.74 KB
/
main.py
File metadata and controls
107 lines (82 loc) · 2.74 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
# Guillermo Siesto
# github.com/GSiesto
import logging
import os
import sys
from logging.handlers import RotatingFileHandler
from pathlib import Path
from telegram.constants import ParseMode
from telegram.ext import (
Application,
CallbackQueryHandler,
CommandHandler,
ContextTypes,
Defaults,
)
import commands
def setup_logging() -> None:
"""Configure lightweight logging for console (and optional file) output."""
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
log_file = os.getenv("LOG_FILE")
handlers = [logging.StreamHandler(sys.stdout)]
if log_file:
log_path = Path(log_file).expanduser()
if log_path.parent and not log_path.parent.exists():
log_path.parent.mkdir(parents=True, exist_ok=True)
handlers.append(
RotatingFileHandler(
log_path,
maxBytes=1_000_000,
backupCount=3,
encoding="utf-8",
)
)
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
level=getattr(logging, log_level, logging.INFO),
handlers=handlers,
)
logging.captureWarnings(True)
logging.getLogger("httpx").setLevel(logging.WARNING)
def main() -> None:
"""Entry point for the Telegram bot."""
setup_logging()
token = os.getenv("TELEGRAM_BOT_TOKEN")
if not token:
logging.error("Environment variable TELEGRAM_BOT_TOKEN is not set.")
sys.exit(1)
application = (
Application.builder()
.token(token)
.defaults(Defaults(parse_mode=ParseMode.MARKDOWN))
.build()
)
application.add_handler(CommandHandler("start", commands.cmd_start))
application.add_handler(CommandHandler("status", commands.cmd_status))
application.add_handler(CommandHandler("players", commands.cmd_players))
application.add_handler(
CallbackQueryHandler(
commands.cb_status,
pattern=fr"^{commands.CallbackData.STATUS.value}$",
)
)
application.add_handler(
CallbackQueryHandler(
commands.cb_players,
pattern=fr"^{commands.CallbackData.PLAYERS.value}$",
)
)
application.add_handler(
CallbackQueryHandler(
commands.cb_about,
pattern=fr"^{commands.CallbackData.ABOUT.value}$",
)
)
application.add_error_handler(log_error)
application.run_polling()
async def log_error(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Log any uncaught exceptions raised while handling updates."""
logging.exception("Unhandled exception while processing update %s", update, exc_info=context.error)
if __name__ == "__main__":
main()