2020-06-12 18:50:12 +02:00
|
|
|
#!/usr/bin/env python
|
2022-05-05 09:27:54 +02:00
|
|
|
# pylint: disable=unused-argument
|
2020-06-12 18:50:12 +02:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
|
|
|
|
2021-05-27 20:34:58 +02:00
|
|
|
"""This is a very simple example on how one could implement a custom error handler."""
|
2020-06-12 18:50:12 +02:00
|
|
|
import html
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
|
2021-10-19 18:28:19 +02:00
|
|
|
from telegram import Update
|
|
|
|
from telegram.constants import ParseMode
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.ext import Application, CallbackContext, CommandHandler
|
2020-06-12 18:50:12 +02:00
|
|
|
|
2021-10-09 13:56:50 +02:00
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
|
|
)
|
2020-06-12 18:50:12 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# This can be your own ID, or one for a developer group/channel.
|
|
|
|
# You can use the /start command of this bot to see your chat id.
|
|
|
|
DEVELOPER_CHAT_ID = 123456789
|
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def error_handler(update: object, context: CallbackContext.DEFAULT_TYPE) -> None:
|
2020-06-12 18:50:12 +02:00
|
|
|
"""Log the error and send a telegram message to notify the developer."""
|
|
|
|
# Log the error before we do anything else, so we can see it even if something breaks.
|
|
|
|
logger.error(msg="Exception while handling an update:", exc_info=context.error)
|
|
|
|
|
|
|
|
# traceback.format_exception returns the usual python message about an exception, but as a
|
|
|
|
# list of strings rather than a single string, so we have to join them together.
|
|
|
|
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
2020-10-31 16:33:34 +01:00
|
|
|
tb_string = ''.join(tb_list)
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
# 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.
|
2021-03-13 16:21:03 +01:00
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2020-06-12 18:50:12 +02:00
|
|
|
message = (
|
2020-11-23 22:09:29 +01:00
|
|
|
f'An exception was raised while handling an update\n'
|
2021-03-13 16:21:03 +01:00
|
|
|
f'<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}'
|
2020-11-23 22:09:29 +01:00
|
|
|
'</pre>\n\n'
|
|
|
|
f'<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n'
|
|
|
|
f'<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n'
|
|
|
|
f'<pre>{html.escape(tb_string)}</pre>'
|
2020-06-12 18:50:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Finally, send the message
|
2022-04-24 12:38:09 +02:00
|
|
|
await context.bot.send_message(
|
|
|
|
chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML
|
|
|
|
)
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def bad_command(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None:
|
2020-06-12 18:50:12 +02:00
|
|
|
"""Raise an error to trigger the error handler."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await context.bot.wrong_method_name() # type: ignore[attr-defined]
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def start(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Displays info on how to trigger an error."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.effective_message.reply_html(
|
2020-10-09 17:22:07 +02:00
|
|
|
'Use /bad_command to cause an error.\n'
|
2020-11-23 22:09:29 +01:00
|
|
|
f'Your chat id is <code>{update.effective_chat.id}</code>.'
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Run the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
# Register the commands...
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler('start', start))
|
|
|
|
application.add_handler(CommandHandler('bad_command', bad_command))
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
# ...and the error handler
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_error_handler(error_handler)
|
2020-06-12 18:50:12 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
|
|
|
application.run_polling()
|
2020-06-12 18:50:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|