Spaces:
Running
Running
File size: 3,596 Bytes
1e0c9d6 ba34f84 1e0c9d6 479d580 67cfbd5 1e0c9d6 7ce64c9 9f65335 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 1e0c9d6 2142971 7d376c9 2142971 1e0c9d6 eceb86e 67cfbd5 9f65335 67cfbd5 1e0c9d6 2142971 1e0c9d6 67cfbd5 9f65335 479d580 ba34f84 9f65335 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 1e0c9d6 67cfbd5 ba34f84 479d580 67cfbd5 ba34f84 eece125 ba34f84 67cfbd5 ba34f84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import gradio as gr
import subprocess
import asyncio
import threading
import os
import time
from queue import Queue, Empty
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
# --- Config ---
BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN_HERE")
log_queue = Queue()
telegram_cmd_logs = []
bot_should_run = True # Thread-safe flag
# --- Telegram Command Handler ---
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not bot_should_run:
await update.message.reply_text("Bot is disabled.")
return
cmd = ' '.join(context.args)
if not cmd:
await update.message.reply_text("Usage: /bash <command>")
return
log_queue.put(f"[Telegram] /bash {cmd}")
try:
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
)
stdout, _ = await proc.communicate()
output = stdout.decode().strip()
except Exception as e:
output = f"Error: {e}"
await update.message.reply_text(f"$ {cmd}\n{output}")
telegram_cmd_logs.append(f"$ {cmd}\n{output}")
if len(telegram_cmd_logs) > 50:
telegram_cmd_logs.pop(0)
# --- Start bot once in main thread ---
def launch_bot():
async def main():
app = ApplicationBuilder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler("bash", bash_command))
log_queue.put("[Bot] Running...")
await app.run_polling()
def bot_thread():
asyncio.run(main())
thread = threading.Thread(target=bot_thread, daemon=True)
thread.start()
# --- Terminal Function ---
def live_terminal(cmd):
if not cmd.strip():
yield "$ "
return
yield f"$ {cmd}\n"
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in iter(process.stdout.readline, ''):
yield line
process.stdout.close()
process.wait()
yield "$ "
# --- Log Updater ---
def update_logs():
logs = []
try:
while True:
logs.append(log_queue.get_nowait())
except Empty:
pass
logs.extend(telegram_cmd_logs[-20:])
return "\n".join(logs[-20:])
# --- Start/Stop Bot Command Execution Only ---
def enable_bot():
global bot_should_run
bot_should_run = True
log_queue.put("[Bot] Enabled")
return update_logs()
def disable_bot():
global bot_should_run
bot_should_run = False
log_queue.put("[Bot] Disabled")
return update_logs()
# --- Gradio Interface ---
with gr.Blocks() as demo:
gr.Markdown("## π₯οΈ Live Terminal + Telegram Bot Interface")
with gr.Row():
terminal_output = gr.Textbox(label="Live Terminal", lines=20, interactive=False)
cmd_input = gr.Textbox(placeholder="Type shell command", label="Command Input")
run_btn = gr.Button("βΆοΈ Run Command")
with gr.Row():
start_btn = gr.Button("β
Enable Bot")
stop_btn = gr.Button("π Disable Bot")
log_output = gr.Textbox(label="π Logs", lines=15, interactive=False)
run_btn.click(live_terminal, inputs=cmd_input, outputs=terminal_output)
start_btn.click(fn=enable_bot, outputs=log_output)
stop_btn.click(fn=disable_bot, outputs=log_output)
def background_log_updater():
while True:
time.sleep(3)
yield update_logs()
demo.load(background_log_updater, outputs=log_output)
# --- Launch everything ---
launch_bot()
demo.launch()
|