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