1

I 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.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
Aaron Jones
  • 72
  • 2
  • 9
  • try `"telegram.vendor.ptb_urllib3.urllib3"` instead of `"telegram.vendor,ptb_urllib3.urllib3"` (the comma) – CallMeStag Apr 02 '22 at 16:38
  • @CallMeStag Okay, will try this and let you know. The errors come once the script runs for a long time. So, won't know till tomorrow morning I guess. Thanks for the help. – Aaron Jones Apr 02 '22 at 18:16

1 Answers1

0

There 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()
G.M.
  • 56
  • 5