-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (60 loc) · 2.03 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
75
76
77
78
"""Entry point for the application."""
import html
import json
import logging
import os
import traceback
from dotenv import load_dotenv
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
import commands
from db.connection import DBHandler
load_dotenv()
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# Do this on every module that you want to use
logger = logging.getLogger(__name__)
# Logs telegram api errors
async def error(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Log Errors caused by Updates."""
logger.error(msg="Exception while handling an update:", exc_info=context.error)
tb_list = traceback.format_exception(
None, context.error, context.error.__traceback__
)
tb_string = "".join(tb_list)
update_str = update.to_dict() if isinstance(update, Update) else str(update)
message = (
f"An exception was raised while handling an update\n"
f"<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}"
"</pre>\n\n"
f"<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n"
f"<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n"
f"<pre>{html.escape(tb_string)}</pre>"
)
await context.bot.send_message(
chat_id=os.getenv("DEVELOPER_CHAT_ID"), text=message, parse_mode=ParseMode.HTML
)
def main():
"""Start the bot."""
app = ApplicationBuilder().token(os.getenv("TOKEN")).build()
DBHandler().connect_pool()
app.add_handlers(
[
CommandHandler(command, getattr(commands, command))
for command in commands.get_all_commands()
]
)
app.add_handler(MessageHandler(filters.COMMAND, commands.unknown))
app.add_error_handler(error)
app.run_polling()
if __name__ == "__main__":
main()