Reputation: 523
From time to time, errors appear in the telegram bot, and I would like to log them. In all "try:except" I set logging, but for some reason these errors pop up in the console and I cannot find a place to grab them
I need to find not the cause of the problem, but how to log it from the console
(__init__.py:445 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: message can\'t be edited"}']"
At the end of the file I have such a construction, but for some reason it does not capture the error that I indicated above
while True:
try:
bot.polling(none_stop=True, interval=1, timeout=20)
except Exception as E:
log(E)
Upvotes: 1
Views: 6511
Reputation: 43962
python-telegram-bot has his own Exception Handling;
Example;
from telegram.error import (TelegramError, Unauthorized, BadRequest,
TimedOut, ChatMigrated, NetworkError)
def error_callback(update, context):
try:
raise context.error
except Unauthorized:
# remove update.message.chat_id from conversation list
except BadRequest:
# handle malformed requests - read more below!
except TimedOut:
# handle slow connection problems
except NetworkError:
# handle other connection problems
except ChatMigrated as e:
# the chat_id of a group has changed, use e.new_chat_id instead
except TelegramError:
# handle all other telegram related errors
dispatcher.add_error_handler(error_callback)
Any other errors will be caught and logged by the Dispatcher. (As described in on there git page).
Small example of configuring the dispatcher;
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
Upvotes: 3