Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,8 +11,8 @@ BOT_TOKEN = os.environ.get("BOT_TOKEN") # Set it only via environment for safet
|
|
11 |
|
12 |
log_queue = Queue()
|
13 |
MAX_LOGS = 20000
|
14 |
-
|
15 |
-
|
16 |
bot_app = None
|
17 |
bot_running = False
|
18 |
bot_thread = None
|
@@ -24,9 +24,9 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
24 |
await update.message.reply_text("Usage: /bash <command>")
|
25 |
return
|
26 |
|
27 |
-
|
28 |
-
log_queue.put(
|
29 |
-
|
30 |
|
31 |
try:
|
32 |
proc = await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
|
@@ -38,22 +38,57 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
38 |
output = f"Error: {e}"
|
39 |
|
40 |
result = f"$ {cmd}\n{output}"
|
41 |
-
|
42 |
-
|
43 |
await update.message.reply_text(result)
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def start_bot():
|
46 |
global bot_app, bot_running, bot_thread
|
47 |
|
48 |
if bot_running or not BOT_TOKEN:
|
49 |
-
log_queue.put("[Bot]
|
50 |
return
|
51 |
|
52 |
async def run_bot():
|
53 |
global bot_app
|
54 |
bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
|
55 |
bot_app.add_handler(CommandHandler("bash", bash_command))
|
56 |
-
log_queue.put("[Bot] Starting
|
57 |
await bot_app.run_polling()
|
58 |
|
59 |
def runner():
|
@@ -67,7 +102,7 @@ def start_bot():
|
|
67 |
def stop_bot():
|
68 |
global bot_app, bot_running
|
69 |
if not bot_running:
|
70 |
-
log_queue.put("[Bot] Bot
|
71 |
return
|
72 |
|
73 |
async def shutdown():
|
@@ -78,76 +113,53 @@ def stop_bot():
|
|
78 |
asyncio.run(shutdown())
|
79 |
bot_running = False
|
80 |
|
81 |
-
# ---
|
82 |
-
def
|
83 |
-
|
84 |
-
if len(all_logs) > MAX_LOGS:
|
85 |
-
del all_logs[0]
|
86 |
-
|
87 |
-
# --- Terminal Handler ---
|
88 |
-
def live_terminal(cmd):
|
89 |
-
if not cmd.strip():
|
90 |
-
yield "$ "
|
91 |
-
return
|
92 |
-
yield f"$ {cmd}\n"
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
for line in iter(proc.stdout.readline, ''):
|
97 |
-
add_log(line.strip())
|
98 |
-
yield line
|
99 |
-
proc.stdout.close()
|
100 |
-
proc.wait()
|
101 |
-
except Exception as e:
|
102 |
-
error = f"[Error] {e}"
|
103 |
-
add_log(error)
|
104 |
-
yield error
|
105 |
-
yield "$ "
|
106 |
|
107 |
-
# ---
|
108 |
-
def update_logs():
|
109 |
-
logs = []
|
110 |
-
try:
|
111 |
-
while True:
|
112 |
-
logs.append(log_queue.get_nowait())
|
113 |
-
except Empty:
|
114 |
-
pass
|
115 |
-
logs.extend(telegram_cmd_logs[-20:])
|
116 |
-
logs = all_logs[-100:] # Only show latest 100 logs in UI
|
117 |
-
return "\n".join(logs)
|
118 |
-
|
119 |
-
# --- UI ---
|
120 |
with gr.Blocks() as demo:
|
121 |
-
gr.Markdown("## π₯οΈ
|
122 |
|
123 |
with gr.Row():
|
124 |
-
terminal_output = gr.Textbox(label="
|
125 |
-
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
|
131 |
-
|
|
|
|
|
132 |
|
133 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
134 |
|
135 |
def handle_start():
|
136 |
start_bot()
|
137 |
-
return
|
138 |
|
139 |
def handle_stop():
|
140 |
stop_bot()
|
141 |
-
return
|
|
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
|
|
|
|
|
145 |
|
146 |
-
def
|
147 |
while True:
|
148 |
-
yield update_logs()
|
149 |
import time; time.sleep(3)
|
|
|
150 |
|
151 |
-
demo.load(fn=
|
|
|
152 |
|
153 |
demo.launch()
|
|
|
11 |
|
12 |
log_queue = Queue()
|
13 |
MAX_LOGS = 20000
|
14 |
+
terminal_logs = []
|
15 |
+
bot_logs = []
|
16 |
bot_app = None
|
17 |
bot_running = False
|
18 |
bot_thread = None
|
|
|
24 |
await update.message.reply_text("Usage: /bash <command>")
|
25 |
return
|
26 |
|
27 |
+
entry = f"[Bot] $ {cmd}"
|
28 |
+
log_queue.put(entry)
|
29 |
+
add_bot_log(entry)
|
30 |
|
31 |
try:
|
32 |
proc = await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
|
|
|
38 |
output = f"Error: {e}"
|
39 |
|
40 |
result = f"$ {cmd}\n{output}"
|
41 |
+
log_queue.put(result)
|
42 |
+
add_bot_log(result)
|
43 |
await update.message.reply_text(result)
|
44 |
|
45 |
+
# --- Logging Functions ---
|
46 |
+
def add_terminal_log(entry):
|
47 |
+
terminal_logs.append(entry)
|
48 |
+
if len(terminal_logs) > MAX_LOGS:
|
49 |
+
del terminal_logs[0]
|
50 |
+
|
51 |
+
def add_bot_log(entry):
|
52 |
+
bot_logs.append(entry)
|
53 |
+
if len(bot_logs) > MAX_LOGS:
|
54 |
+
del bot_logs[0]
|
55 |
+
|
56 |
+
# --- Terminal ---
|
57 |
+
def live_terminal(cmd):
|
58 |
+
if not cmd.strip():
|
59 |
+
yield "$ "
|
60 |
+
return
|
61 |
+
yield f"$ {cmd}\n"
|
62 |
+
|
63 |
+
try:
|
64 |
+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
65 |
+
for line in iter(proc.stdout.readline, ''):
|
66 |
+
line = line.strip()
|
67 |
+
log_queue.put(line)
|
68 |
+
add_terminal_log(line)
|
69 |
+
yield line + "\n"
|
70 |
+
proc.stdout.close()
|
71 |
+
proc.wait()
|
72 |
+
except Exception as e:
|
73 |
+
error = f"[Error] {e}"
|
74 |
+
log_queue.put(error)
|
75 |
+
add_terminal_log(error)
|
76 |
+
yield error + "\n"
|
77 |
+
yield "$ "
|
78 |
+
|
79 |
+
# --- Bot ---
|
80 |
def start_bot():
|
81 |
global bot_app, bot_running, bot_thread
|
82 |
|
83 |
if bot_running or not BOT_TOKEN:
|
84 |
+
log_queue.put("[Bot] Already running or token missing.")
|
85 |
return
|
86 |
|
87 |
async def run_bot():
|
88 |
global bot_app
|
89 |
bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
|
90 |
bot_app.add_handler(CommandHandler("bash", bash_command))
|
91 |
+
log_queue.put("[Bot] Starting bot...")
|
92 |
await bot_app.run_polling()
|
93 |
|
94 |
def runner():
|
|
|
102 |
def stop_bot():
|
103 |
global bot_app, bot_running
|
104 |
if not bot_running:
|
105 |
+
log_queue.put("[Bot] Bot not running.")
|
106 |
return
|
107 |
|
108 |
async def shutdown():
|
|
|
113 |
asyncio.run(shutdown())
|
114 |
bot_running = False
|
115 |
|
116 |
+
# --- UI Log Updates ---
|
117 |
+
def update_terminal_logs():
|
118 |
+
return "\n".join(terminal_logs[-100:])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
def update_bot_logs():
|
121 |
+
return "\n".join(bot_logs[-100:])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
# --- Gradio UI ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
with gr.Blocks() as demo:
|
125 |
+
gr.Markdown("## π₯οΈ Terminal + π€ Telegram Bot (Live Logs)")
|
126 |
|
127 |
with gr.Row():
|
128 |
+
terminal_output = gr.Textbox(label="π Terminal Output", lines=20, interactive=False)
|
129 |
+
bot_output = gr.Textbox(label="π€ Telegram Bot Logs", lines=20, interactive=False)
|
130 |
|
131 |
+
with gr.Row():
|
132 |
+
cmd_input = gr.Textbox(placeholder="Enter shell command", label="Command Input")
|
133 |
+
run_btn = gr.Button("βΆοΈ Run Command")
|
134 |
|
135 |
+
with gr.Row():
|
136 |
+
start_btn = gr.Button("π Start Telegram Bot")
|
137 |
+
stop_btn = gr.Button("π Stop Telegram Bot")
|
138 |
|
139 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
140 |
|
141 |
def handle_start():
|
142 |
start_bot()
|
143 |
+
return update_bot_logs()
|
144 |
|
145 |
def handle_stop():
|
146 |
stop_bot()
|
147 |
+
return update_bot_logs()
|
148 |
+
|
149 |
+
start_btn.click(fn=handle_start, outputs=bot_output)
|
150 |
+
stop_btn.click(fn=handle_stop, outputs=bot_output)
|
151 |
|
152 |
+
def auto_update_terminal():
|
153 |
+
while True:
|
154 |
+
import time; time.sleep(3)
|
155 |
+
yield update_terminal_logs()
|
156 |
|
157 |
+
def auto_update_bot():
|
158 |
while True:
|
|
|
159 |
import time; time.sleep(3)
|
160 |
+
yield update_bot_logs()
|
161 |
|
162 |
+
demo.load(fn=auto_update_terminal, outputs=terminal_output)
|
163 |
+
demo.load(fn=auto_update_bot, outputs=bot_output)
|
164 |
|
165 |
demo.launch()
|