Python telegram bot is failing with this error: "ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host"

154 views Asked by At

I made a telegram bot that continuously checks for new messages, and will send a message to a chat when a certain person sends a message. It is encountering this error, either some time after the program has been run, on start, or after a few loops through.

Traceback (most recent call last):
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 1096, in _validate_conn
    conn.connect()
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 642, in connect
    sock_and_verified = _ssl_wrap_socket_and_match_hostname(
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 782, in _ssl_wrap_socket_and_match_hostname
    ssl_sock = ssl_wrap_socket(
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\ssl_.py", line 470, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\ssl_.py", line 514, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 501, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1041, in _create
    self.do_handshake()
  File "C:\Users\eli\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1310, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

This is the code:

import requests
import time
TOKEN = "************"
message = "********"
offset = 0
chat_id = "-**********"
while True:
    time.sleep(3)
    url = f"https://api.telegram.org/bot{TOKEN}/getUpdates?offset={offset}"
    headers = {'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"}
    results = requests.get(url, headers=headers).json()
    print(results)
    offset = str(results["result"][0].get("update_id"))
    if results["result"][0].get("message").get("from").get("First_name") == "*******":
        headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
        url2 = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
        print(requests.get(url2, headers=headers).json())

I tried to make it run continuously, limiting its request rate, and adding headers to my request call. I was expecting for this to run until manually stopped.

0

There are 0 answers