import traceback from config.tasks import mail_admins from telegram import Update from telegram.ext import ContextTypes from django.conf import settings from django.utils.translation import gettext as _ async def command_unknown(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if update.effective_chat: await context.bot.send_message(chat_id=update.effective_chat.id, text=_("Unknown command.")) async def handle_stacktrace(update: object, context: ContextTypes.DEFAULT_TYPE) -> None: ignored_errors = [ 'httpx HTTPError: [Errno -2] Name or service not known', "Forbidden: bot can't initiate conversation with a user", 'Forbidden: bot was blocked by the user', 'Message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message' ] message = f'ERROR on update: {update}' if context.error: tb_list = traceback.format_exception( None, context.error, context.error.__traceback__, ) tb_string = ''.join(tb_list) message = f'{message}\n{tb_string}'[:20000] if not settings.DEBUG and str(context.error) not in ignored_errors: mail_admins(subject="Telegram bot error", message=message)