2

I have a problem with my telegram bot. Just using

bot = telebot.TeleBot(TOKEN)

bot.polling(none_stop=False ,interval=2)

When the internet connection is down, the script crashes, because polling fails.

I want to add some code to check if the internet connection is available before bot polling,

import urllib
try :
    url = "https://www.google.com"
    urllib.urlopen(url)
    status = "Connected"
except :
    status = "Not connect"

something like:

"if" status is "connected" ---> bot polling.

else ----> wait and try again in "seconds"

mkj
  • 2,761
  • 5
  • 24
  • 28
Nivek
  • 21
  • 4
  • 2
    I think instead of checking if your internet connection is working before polling (which would be useless in most cases anyways since you are checking only at the start, and you won't be preventing a crash if the internet disconnects while the bot is polling), you should try to handle the crash gracefully per EAFP. Wrap the polling funciton in a try-except block and have it retry after sleeping for a couple seconds if it throws an exception. – lonewaft Aug 17 '16 at 18:25

0 Answers0