-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (58 loc) · 2.23 KB
/
main.py
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
import yaml
import logging
from telegram.ext import ApplicationBuilder, CommandHandler, filters, MessageHandler
from bot import *
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
def load_config() -> dict:
"""Load the configuration from the config.yml file."""
try:
with open('config.yml', 'r') as file:
return yaml.safe_load(file)
except FileNotFoundError:
logger.error("Configuration file 'config.yml' not found.")
raise
except yaml.YAMLError as e:
logger.error("Error parsing 'config.yml': %s", e)
raise
def create_bot_application(config: dict):
"""Create and return the Telegram bot application."""
try:
BOT_TOKEN = config.get('bot_token')
if not BOT_TOKEN:
raise ValueError("Bot token is missing in the configuration.")
app_builder = ApplicationBuilder().token(BOT_TOKEN)
BASE_URL = config.get('endpoint')
if BASE_URL:
app_builder.base_url(BASE_URL)
app = app_builder.build()
return app
except (KeyError, TypeError, ValueError) as e:
logger.error(f"Error creating the Telegram bot application: {e}")
raise
def add_command_handlers(app):
"""Add command handlers to the bot application."""
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("list", list))
app.add_handler(CommandHandler("sub", sub))
app.add_handler(CommandHandler("unsub", unsub))
app.add_handler(CommandHandler("set", set))
app.add_handler(MessageHandler(filters.COMMAND, unknown))
app.add_error_handler(error)
def main():
try:
config = load_config()
app = create_bot_application(config)
add_command_handlers(app)
# fixed the restart loading task issue
# https://docs.python-telegram-bot.org/en/v21.2/telegram.ext.jobqueue.html#telegram.ext.JobQueue.run_once
app.job_queue.run_once(reload_rss_tasks, 1)
app.run_polling(poll_interval=3)
logger.info("Bot is starting...")
except Exception as e:
logger.error("Failed to start the bot: %s", e)
if __name__ == '__main__':
main()