Telegram bot loses connection

I deployed a Telegram bot on Koyeb. Since it doesn’t listen to any port, regular health-checks fail, resulting in stopping my application. As adviced here, I modified code so that simple web-server runs when the application starts working.

The problem is that every 40-240 minutes an exeption emerges:

2022-12-13 00:17:54,367 ( MainThread) ERROR - TeleBot: "Infinity polling exception: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))"
2022-12-13 00:17:54,368 ( MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/", line 710, in urlopen
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/", line 386, in _make_request
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/", line 1042, in _validate_conn
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/", line 424, in connect
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/util/", line 450, in ssl_wrap_socket
sock, context, tls_in_tls, server_hostname=server_hostname
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/util/", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "/app/.heroku/python/lib/python3.7/", line 423, in wrap_socket
File "/app/.heroku/python/lib/python3.7/", line 870, in _create
File "/app/.heroku/python/lib/python3.7/", line 1139, in do_handshake
ConnectionResetError: [Errno 104] Connection reset by peer

When I run this app on my local computer, the problem does not arise. (Python 3.7.9 is used on both local computer and in my application on Koyeb). On the other serverless platform for deployment I also didn’t have this problem. That platform doesn’t perform health-checks, hence I didn’t have to run a dummy server within my application. So may be my problem is somehow related to running a server? I can’t check this assumption, since if I run my app without running a server, it won’t work longer than 15 minutes due to health-check fails.

This is the code I added to run a server (my app is in Python):

from os import environ

from http.server import BaseHTTPRequestHandler, HTTPServer

APP_PORT = int(environ.get('PORT'))

class GetHandler(BaseHTTPRequestHandler):
    def _set_headers(self):

    def do_GET(self):

def run_server(handler_class=GetHandler):
    server_address = (APP_HOST, APP_PORT)
    httpd = HTTPServer(server_address, handler_class)

I run my bot and server together like that:

if __name__ == '__main__':

Do you have any ideas on what’s the reason of loss of connection and how can I avoid it? Thank you in advance!