-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·52 lines (44 loc) · 1.4 KB
/
main.py
File metadata and controls
executable file
·52 lines (44 loc) · 1.4 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
#!/usr/bin/env python3
"""
Telegram Bot Message Receiver
Standalone Python app that connects to Telegram bot and logs all direct messages.
"""
import asyncio
import logging
import sys
from pathlib import Path
from src.bot_handler import run_bot
from src.env_loader import load_bot_name, load_bot_token
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def main() -> None:
"""Main entry point for the bot."""
env_file = Path(".env")
# Load bot configuration from .env
try:
logger.info(f"Loading configuration from {env_file}")
token = load_bot_token(str(env_file))
bot_name = load_bot_name(str(env_file))
logger.info(f"Configuration loaded successfully (bot: @{bot_name})")
except FileNotFoundError:
print(f"Error: {env_file} file not found", file=sys.stderr)
sys.exit(1)
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
# Run the bot
logger.info(f"Starting Telegram bot receiver (@{bot_name})...")
try:
asyncio.run(run_bot(token, "MESSAGES.txt"))
except KeyboardInterrupt:
logger.info("Bot stopped by user")
sys.exit(0)
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()