How to handle errors in Python Telegram Bot from other modules?

  Kiến thức lập trình

I am using PTB with job queue – aka apscheduler. I have set up a custom error handler for the bot itself to send the error info to a specific channel, however, I am also logging errors from apscheduler and would like those errors to be sent to the same channel. How do I go about doing that? I have tried using the logging module to create a custom handler, but was unable to figure out how to have the error info sent using telegram.

async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
    ...
    await context.bot.send_message(
        chat_id=DEVELOPER_CHAT_ID, text=message)

...
logging.getLogger('apscheduler.scheduler').setLevel(log_level)
...
application = Application.builder().token(TOKEN).build()
application.add_error_handler(error_handler)
application.run_polling()

LEAVE A COMMENT