python-telegram-bot/examples/errorhandlerbot.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.8 KiB
Python
Raw Normal View History

Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
#!/usr/bin/env python
2022-05-15 14:08:40 +02:00
# pylint: disable=unused-argument, wrong-import-position
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# This program is dedicated to the public domain under the CC0 license.
"""This is a very simple example on how one could implement a custom error handler."""
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
import html
import json
import logging
import traceback
2022-05-15 14:08:40 +02:00
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
2022-06-09 17:22:32 +02:00
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
2022-05-15 14:08:40 +02:00
)
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# Enable logging
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
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
async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
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.
2023-03-25 19:18:04 +01:00
logger.error("Exception while handling an update:", exc_info=context.error)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# 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__)
tb_string = "".join(tb_list)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
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.
update_str = update.to_dict() if isinstance(update, Update) else str(update)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
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"
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>"
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
)
# Finally, send the message
await context.bot.send_message(
chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML
)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
async def bad_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
"""Raise an error to trigger the error handler."""
await context.bot.wrong_method_name() # type: ignore[attr-defined]
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Displays info on how to trigger an error."""
await update.effective_message.reply_html(
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +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>."
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
)
def main() -> None:
"""Run the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token("TOKEN").build()
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# Register the commands...
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("bad_command", bad_command))
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# ...and the error handler
application.add_error_handler(error_handler)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
Add standalone example on error handlers (#1983) * Remove error handlers from examples Most examples use the same error handler, that error handler logs update.to_dict but doesn't log error traceback. Hiding error traceback is quite bad, removing the error handler entirely causes PTB to use default error logging which does include error traceback. * adding error handling example * Change error handler example Including: - Change the telegram message to include usual python error message. - HTML-escape the strings used to build the telegram message. - Capitalize comments and add more empty lines to hopefully unify the style with other examples, at least a bit. - Reorder imports. * Add an error-rising command to the error handler example * Slightly change example error handler docstring and comments * Make telegram message sent by the error handler example more readable * Rename error_handler.py to errorhandlerbot.py and add a start command * Change error handler example to work without developer chat id * Revert "Change error handler example to work without developer chat id" This reverts commit c4efea6f * Make bot token a module level constant in the error handler example Otherwise the example will require two edits 40 lines apart to run. * Show chat id in start command of the error handler example The example requires you to set developer chat id, this change will make things easier for users that don't know how to see their chat id. * Add errorhandlerbot.py to the examples folder readme Co-authored-by: poolitzer <25934244+poolitzer@users.noreply.github.com> Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
2020-06-12 18:50:12 +02:00
if __name__ == "__main__":
main()