import time, ssl, socket, json
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
print("Start 0")
for i in range(120):
    try:
        sock = socket.create_connection(("api.telegram.org", 443), timeout=10)
        ss = ctx.wrap_socket(sock, server_hostname="api.telegram.org")
        ss.settimeout(10)
        body = b"timeout=0"
        req = b"POST /bot8781986513:AAFnD-EYQPUiv4noJgZjk_9CsEHPvcTsEN4/getUpdates HTTP/1.1\r\nHost: api.telegram.org\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " + str(len(body)).encode() + b"\r\nConnection: close\r\n\r\n" + body
        ss.sendall(req)
        resp = b""
        try:
            while True:
                d = ss.read(8192)
                if not d: break
                resp += d
        except: pass
        ss.close()
        parts = resp.split(b"\r\n\r\n", 1)
        ok = False
        if len(parts) > 1:
            r = json.loads(parts[1])
            ok = r.get("ok", False)
        print(f"Alive {i}s API={ok}")
    except Exception as e:
        print(f"Error {i}s: {type(e).__name__}")
    time.sleep(2)
print("DONE")
