M
M
matue892016-10-29 08:42:39
Python
matue89, 2016-10-29 08:42:39

How to run a Python - Telegram bot so that it does not crash due to errors?

Launched python process for telegram bot.
and it crashes once a day (the process ends) due to various errors, for example:

File "main_bot.py", line 67, in <module>
    bot.polling(none_stop=True, interval=0)
  File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 192, in polling
    self.__threaded_polling(none_stop, interval, timeout)
  File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 215, in __threaded_polling
    polling_thread.raise_exceptions()
  File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 73, in raise_exceptions
    six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
  File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 54, in run
    task(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 122, in __retrieve_updates
    updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 92, in get_updates
    json_updates = apihelper.get_updates(self.token, offset, limit, timeout)
  File "/usr/local/lib/python2.7/dist-packages/telebot/apihelper.py", line 139, in get_updates
    return _make_request(token, method_url, params=payload)
  File "/usr/local/lib/python2.7/dist-packages/telebot/apihelper.py", line 34, in _make_request
    result = requests.request(method, request_url, params=params, files=files, timeout=(connect_timeout, read_timeout))
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(101, 'Network is unreachable'))

Is it possible to make the py daemon not exit, or start it automatically if it crashes?
I run the bot on a VPS with Debian like this: I use pyTelegramBotAPI
bot.polling(none_stop=True, interval=0)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
rll, 2016-10-29
@rll

I run mine like this:

while True:
    try:
      bot.polling(none_stop=True)
    except: 
      print('bolt')
      logging.error('error: {}'.format(sys.exc_info()[0]))
      time.sleep(5)

R
Rou1997, 2016-10-29
@Rou1997

Well, you give, "is it possible"! If you have such problems, then what about us poor, unhappy, money-hungry TCP/IP programmers, who were told by their superiors to establish a stable, not a simple connection? :)
Handle the exception, or "stupidly" create a background process that would restart the bot, and you can test this algorithm by programmatically disconnecting from the network, plus NetLimiter, although I think simply disconnecting the network manually will be enough for the bot to fall down with the exception :)

A
asd111, 2016-10-29
@asd111

Try like this:

try: 
    bot.polling(none_stop=True, interval=0)
except Exception:
    pass

And see how to handle exceptions in python pythonicway.com/python-exceptions-handling
https://docs.python.org/3.6/tutorial/errors.html#e...

F
Fractal Zombie, 2016-10-29
@FractalZombie

The answer is in the question itself. See what exceptions you get and do try except...

N
Nicholas, 2016-10-29
@healqq

Your Internet session may be interrupted once a day.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question