Skip to content
Back to top
View this page

# `errorhandlerbot.py`

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