File size: 1,515 Bytes
16bfc87 |
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 |
import logging
import traceback
import html
import telegram
from telegram import Update
from telegram.ext import CallbackContext
from dtb.settings import TELEGRAM_LOGS_CHAT_ID
from users.models import User
def send_stacktrace_to_tg_chat(update: Update, context: CallbackContext) -> None:
u = User.get_user(update, context)
logging.error("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)
# Build the message with some markup and additional information about what happened.
# You might need to add some logic to deal with messages longer than the 4096 character limit.
message = (
f'An exception was raised while handling an update\n'
f'<pre>{html.escape(tb_string)}</pre>'
)
user_message = """
😔 Something broke inside the bot.
It is because we are constantly improving our service but sometimes we might forget to test some basic stuff.
We already received all the details to fix the issue.
Return to /start
"""
context.bot.send_message(
chat_id=u.user_id,
text=user_message,
)
admin_message = f"⚠️⚠️⚠️ for {u.tg_str}:\n{message}"[:4090]
if TELEGRAM_LOGS_CHAT_ID:
context.bot.send_message(
chat_id=TELEGRAM_LOGS_CHAT_ID,
text=admin_message,
parse_mode=telegram.ParseMode.HTML,
)
else:
logging.error(admin_message)
|