import os import sys import subprocess import threading from http.server import SimpleHTTPRequestHandler from socketserver import TCPServer class HealthCheckHandler(SimpleHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html; charset=utf-8") self.end_headers() html = """ elshater77 Chess Bot

ā™Ÿļø elshater77 Chess Bot šŸ¤–

The Stockfish-powered Lichess Bot is running successfully 24/7 on Hugging Face Spaces!

STATUS: ONLINE & ACTIVE 🟢
""" self.wfile.write(html.encode("utf-8")) def log_message(self, format, *args): # Silence HTTP logs to keep console clean pass def run_health_server(): port = int(os.environ.get("PORT", 7860)) print(f"šŸ“” Starting background health check server on port {port}...") TCPServer.allow_reuse_address = True try: with TCPServer(("", port), HealthCheckHandler) as httpd: httpd.serve_forever() except Exception as e: print(f"āš ļø Health check server encountered an error: {e}") def main(): print("šŸš€ Starting elshater77 Chess Bot wrapper...") # 1. Start HTTP Health Check Server in a background thread health_thread = threading.Thread(target=run_health_server, daemon=True) health_thread.start() # 2. Retrieve Lichess token from Hugging Face Space secrets token = os.environ.get("LICHESS_BOT_TOKEN") if not token: print("āŒ Error: LICHESS_BOT_TOKEN environment variable not set.") print("Please add 'LICHESS_BOT_TOKEN' as a Secret in your Hugging Face Space Settings.") print("Waiting indefinitely to prevent crash loop...") import time while True: time.sleep(3600) # 3. Read template and inject token template_path = "config.yml.template" config_path = "config.yml" if not os.path.exists(template_path): print(f"āŒ Error: Template file {template_path} not found.") sys.exit(1) print("šŸ“ Injecting credentials into config.yml...") with open(template_path, "r", encoding="utf-8") as f: config_data = f.read() config_data = config_data.replace("LICHESS_BOT_TOKEN_PLACEHOLDER", token) with open(config_path, "w", encoding="utf-8") as f: f.write(config_data) print("āœ… Configuration ready. Launching lichess-bot...") # 4. Execute the official lichess-bot script try: subprocess.run([sys.executable, "lichess-bot.py"], check=True) except KeyboardInterrupt: print("\nšŸ‘‹ Bot stopped by user.") except Exception as e: print(f"āŒ lichess-bot crashed with error: {e}") sys.exit(1) if __name__ == "__main__": main()