Support
Quality
Security
License
Reuse
kandi has reviewed MessageHandler and discovered the below as its top functions. This is intended to give you an instant insight into MessageHandler implemented functionality, and help decide if they suit your requirements.
Get all kandi verified functions for this library.
Get all kandi verified functions for this library.
Simple Handler forwarding, for supporting pause、resume、cancelAllMessage、killSelf skills.
QUESTION
How to hide telegram.vendor.ptb error and warning messages?
Asked 2022-Apr-05 at 11:08I am running a telegram bot with the code below.
def main():
"""Start the bot."""
global token, allowedUsers
# Create the Updater and pass it your bot's token.
updater = Updater(token)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Registering handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_info))
dispatcher.add_handler(CommandHandler("learn", learn))
dispatcher.add_handler(CommandHandler("revise", revise))
dispatcher.add_handler(CommandHandler("quote", quote))
dispatcher.add_handler(CommandHandler("more", more))
dispatcher.add_handler(CommandHandler("quiz", quiz))
# on non command i.e message
dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))
# Start the Bot
updater.start_polling(drop_pending_updates=True)
updater.idle()
The code is working fine and it is doing what needs to be done. The only issue I am facing is in my terminal, when the code has run for hours and hours, I wake up and see this mess (over and over again!):
2022-04-02 08:53:21,539 - apscheduler.scheduler - INFO - Scheduler
started 2022-04-02 15:59:10,367 -
telegram.vendor.ptb_urllib3.urllib3.connectionpool - WARNING -
Retrying (Retry(total=2, connect=None, read=None, redirect=None))
after connection broken by
'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
object at 0x0000028CB212F160>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed')':
/bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
15:59:10,378 - telegram.vendor.ptb_urllib3.urllib3.connectionpool -
WARNING - Retrying (Retry(total=1, connect=None, read=None,
redirect=None)) after connection broken by
'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
object at 0x0000028CB21330A0>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed')':
/bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
15:59:10,379 - telegram.vendor.ptb_urllib3.urllib3.connectionpool -
WARNING - Retrying (Retry(total=0, connect=None, read=None,
redirect=None)) after connection broken by
'NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
object at 0x0000028CB212E400>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed')':
/bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates 2022-04-02
15:59:10,380 - telegram.ext.updater - ERROR - Error while getting
Updates: urllib3 HTTPError
HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries
exceeded with url:
/bot98956256546:THISisJustRandomStuffdon'tmindme/getUpdates (Caused by
NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection
object at 0x0000028CB212E580>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed'))
But, even after all these warning/error messages, the bot is running fine. There is no issue there. The only problem is the network error messages which after a lot of googling, I am convinced, I can't get rid of. All I want is to hide these messages so that I can have a cleaner terminal for relevant information which I need.
Here is the things I have tried but none has worked.
Try 1:
import logging
logging.getLogger("telegram").setLevel(logging.CRITICAL)
logger = logging.getLogger(__name__)
Try 2:
import logging
logging.getLogger("telegram.vendor,ptb_urllib3.urllib3").setLevel(
logging.CRITICAL)
logger = logging.getLogger(__name__)
Nothing seems to be working and I am a little disappointed. Please show me what I am missing! This should not be this frustrating. It is a simple bot.
ANSWER
Answered 2022-Apr-05 at 11:08There is a simple solution to this:
import logging
import (other modules)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logging.getLogger("telegram.vendor.ptb_urllib3.urllib3").setLevel(logging.ERROR)
logger = logging.getLogger(__name__)
The main function will remain the same:
def main():
"""Start the bot."""
global token, allowedUsers
# Create the Updater and pass it your bot's token.
updater = Updater(token)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Registering handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_info))
dispatcher.add_handler(CommandHandler("learn", learn))
dispatcher.add_handler(CommandHandler("revise", revise))
dispatcher.add_handler(CommandHandler("quote", quote))
dispatcher.add_handler(CommandHandler("more", more))
dispatcher.add_handler(CommandHandler("quiz", quiz))
# on non command i.e message
dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))
# Start the Bot
updater.start_polling(drop_pending_updates=True)
updater.idle()
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source