CoolFace
Apppublic

Darshit3001/OpenClaw-Automatic

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
app.py63 linesDownload Raw Back to root
1import gradio as gr2import os3import telebot4from openai import OpenAI5import threading6import time7 8# 1. SETUP KEYS9BOT_TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN')10OPENROUTER_KEY = os.environ.get('OPENROUTER_API_KEY')11 12# 2. DEFINITELY WAIT FOR INTERNET13def start_bot():14    print("⏳ SYSTEM STARTUP: Waiting 20 seconds for Internet to connect...")15    time.sleep(20) 16    print("✅ INTERNET SHOULD BE READY. Starting Bot...")17 18    if not BOT_TOKEN:19        print("❌ FATAL: Bot Token missing.")20        return21 22    bot = telebot.TeleBot(BOT_TOKEN)23    client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=OPENROUTER_KEY)24 25    @bot.message_handler(func=lambda message: True)26    def handle_message(message):27        try:28            print(f"📩 MSG RECEIVED: {message.text}")29            bot.send_chat_action(message.chat.id, 'typing')30            31            response = client.chat.completions.create(32                model="google/gemini-2.0-flash-lite-preview-02-05:free",33                messages=[34                    {"role": "system", "content": "You are OpenClaw. Professional and brief."},35                    {"role": "user", "content": message.text}36                ]37            )38            bot.reply_to(message, response.choices[0].message.content)39            print("✅ REPLY SENT")40        except Exception as e:41            print(f"❌ AI ERROR: {e}")42            bot.reply_to(message, "I am waking up. Please try again.")43 44    # 3. THE IMMORTAL LOOP (If it crashes, it restarts instantly)45    while True:46        try:47            print("🔄 Connecting to Telegram...")48            bot.remove_webhook()49            bot.infinity_polling(timeout=10, long_polling_timeout=5)50        except Exception as e:51            print(f"⚠️ Connection failed. Retrying in 5s... Error: {e}")52            time.sleep(5)53 54# Start Bot in background55threading.Thread(target=start_bot, daemon=True).start()56 57# 4. LAUNCH UI (Keeps Server Alive)58with gr.Blocks() as ui:59    gr.Markdown("# 🟢 OpenClaw is ONLINE")60    gr.Markdown("## System Status: Running")61    gr.Markdown("Please wait 30 seconds after startup for the bot to reply.")62 63ui.launch(server_name="0.0.0.0", server_port=7860)