errorhandlerbot.pyΒΆ

  1#!/usr/bin/env python
  2# pylint: disable=unused-argument, wrong-import-position
  3# This program is dedicated to the public domain under the CC0 license.
  4
  5"""This is a very simple example on how one could implement a custom error handler."""
  6import html
  7import json
  8import logging
  9import traceback
 10
 11from telegram import __version__ as TG_VER
 12
 13try:
 14    from telegram import __version_info__
 15except ImportError:
 16    __version_info__ = (0, 0, 0, 0, 0)  # type: ignore[assignment]
 17
 18if __version_info__ < (20, 0, 0, "alpha", 1):
 19    raise RuntimeError(
 20        f"This example is not compatible with your current PTB version {TG_VER}. To view the "
 21        f"{TG_VER} version of this example, "
 22        f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
 23    )
 24from telegram import Update
 25from telegram.constants import ParseMode
 26from telegram.ext import Application, CommandHandler, ContextTypes
 27
 28# Enable logging
 29logging.basicConfig(
 30    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
 31)
 32# set higher logging level for httpx to avoid all GET and POST requests being logged
 33logging.getLogger("httpx").setLevel(logging.WARNING)
 34
 35logger = logging.getLogger(__name__)
 36
 37# This can be your own ID, or one for a developer group/channel.
 38# You can use the /start command of this bot to see your chat id.
 39DEVELOPER_CHAT_ID = 123456789
 40
 41
 42async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
 43    """Log the error and send a telegram message to notify the developer."""
 44    # Log the error before we do anything else, so we can see it even if something breaks.
 45    logger.error("Exception while handling an update:", exc_info=context.error)
 46
 47    # traceback.format_exception returns the usual python message about an exception, but as a
 48    # list of strings rather than a single string, so we have to join them together.
 49    tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
 50    tb_string = "".join(tb_list)
 51
 52    # Build the message with some markup and additional information about what happened.
 53    # You might need to add some logic to deal with messages longer than the 4096 character limit.
 54    update_str = update.to_dict() if isinstance(update, Update) else str(update)
 55    message = (
 56        f"An exception was raised while handling an update\n"
 57        f"<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}"
 58        "</pre>\n\n"
 59        f"<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n"
 60        f"<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n"
 61        f"<pre>{html.escape(tb_string)}</pre>"
 62    )
 63
 64    # Finally, send the message
 65    await context.bot.send_message(
 66        chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML
 67    )
 68
 69
 70async def bad_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
 71    """Raise an error to trigger the error handler."""
 72    await context.bot.wrong_method_name()  # type: ignore[attr-defined]
 73
 74
 75async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
 76    """Displays info on how to trigger an error."""
 77    await update.effective_message.reply_html(
 78        "Use /bad_command to cause an error.\n"
 79        f"Your chat id is <code>{update.effective_chat.id}</code>."
 80    )
 81
 82
 83def main() -> None:
 84    """Run the bot."""
 85    # Create the Application and pass it your bot's token.
 86    application = Application.builder().token("TOKEN").build()
 87
 88    # Register the commands...
 89    application.add_handler(CommandHandler("start", start))
 90    application.add_handler(CommandHandler("bad_command", bad_command))
 91
 92    # ...and the error handler
 93    application.add_error_handler(error_handler)
 94
 95    # Run the bot until the user presses Ctrl-C
 96    application.run_polling(allowed_updates=Update.ALL_TYPES)
 97
 98
 99if __name__ == "__main__":
100    main()