Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,16 @@ import time
|
|
8 |
from telegram import Update
|
9 |
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
10 |
|
11 |
-
|
|
|
12 |
|
|
|
13 |
bot_app = None
|
14 |
-
bot_thread = None
|
15 |
bot_running = False
|
16 |
log_queue = Queue()
|
17 |
telegram_cmd_logs = []
|
18 |
|
19 |
-
# Telegram
|
20 |
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
21 |
cmd = ' '.join(context.args)
|
22 |
if not cmd:
|
@@ -24,7 +25,6 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
24 |
return
|
25 |
|
26 |
log_queue.put(f"[Telegram] Received command: {cmd}")
|
27 |
-
|
28 |
try:
|
29 |
proc = await asyncio.create_subprocess_shell(
|
30 |
cmd,
|
@@ -43,63 +43,54 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
if bot_running:
|
49 |
log_queue.put("[Bot] Already running.")
|
50 |
return
|
51 |
|
52 |
-
async def
|
53 |
global bot_app, bot_running
|
54 |
bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
|
55 |
bot_app.add_handler(CommandHandler("bash", bash_command))
|
56 |
bot_running = True
|
57 |
-
log_queue.put("[Bot]
|
58 |
-
await bot_app.
|
|
|
|
|
59 |
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
asyncio.set_event_loop(loop)
|
64 |
-
try:
|
65 |
-
loop.run_until_complete(run_bot())
|
66 |
-
except Exception as e:
|
67 |
-
log_queue.put(f"[Bot Error] {e}")
|
68 |
-
|
69 |
-
bot_thread = threading.Thread(target=runner, daemon=True)
|
70 |
-
bot_thread.start()
|
71 |
-
|
72 |
-
def stop_bot():
|
73 |
global bot_app, bot_running
|
74 |
if not bot_running:
|
75 |
-
log_queue.put("[Bot]
|
76 |
return
|
77 |
|
78 |
async def shutdown():
|
79 |
-
await bot_app.
|
80 |
await bot_app.stop()
|
81 |
-
|
|
|
82 |
|
83 |
asyncio.run(shutdown())
|
84 |
bot_running = False
|
85 |
|
86 |
-
#
|
87 |
-
def live_terminal(cmd):
|
88 |
-
if not cmd.strip():
|
89 |
-
yield gr.TextArea.update(value="$ ")
|
90 |
-
return
|
91 |
-
yield gr.TextArea.update(value=f"$ {cmd}\n")
|
92 |
-
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
93 |
-
output = f"$ {cmd}\n"
|
94 |
-
for line in iter(process.stdout.readline, ''):
|
95 |
-
output += line
|
96 |
-
yield gr.TextArea.update(value=output)
|
97 |
-
process.stdout.close()
|
98 |
-
process.wait()
|
99 |
-
output += "$ "
|
100 |
-
yield gr.TextArea.update(value=output)
|
101 |
-
|
102 |
-
# Update log output
|
103 |
def update_log_box():
|
104 |
logs = []
|
105 |
try:
|
@@ -110,41 +101,33 @@ def update_log_box():
|
|
110 |
logs.extend(telegram_cmd_logs[-20:])
|
111 |
return "\n".join(logs[-20:])
|
112 |
|
113 |
-
# UI
|
114 |
-
with gr.Blocks(
|
115 |
gr.Markdown("## π₯οΈ Live Terminal + Telegram Bot Interface")
|
116 |
|
117 |
with gr.Row():
|
118 |
-
terminal_output = gr.
|
119 |
-
cmd_input = gr.Textbox(
|
120 |
-
run_btn = gr.Button("βΆοΈ Run")
|
121 |
|
122 |
with gr.Row():
|
|
|
123 |
start_btn = gr.Button("π€ Start Telegram Bot")
|
124 |
stop_btn = gr.Button("π Stop Telegram Bot")
|
125 |
|
126 |
-
log_output = gr.Textbox(label="π Bot Logs", lines=15, interactive=False)
|
127 |
|
128 |
-
#
|
129 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
130 |
|
131 |
-
# Start/Stop
|
132 |
-
|
133 |
-
|
134 |
-
return update_log_box()
|
135 |
-
|
136 |
-
def stop_click():
|
137 |
-
stop_bot()
|
138 |
-
return update_log_box()
|
139 |
-
|
140 |
-
start_btn.click(start_click, outputs=log_output)
|
141 |
-
stop_btn.click(stop_click, outputs=log_output)
|
142 |
|
143 |
-
#
|
144 |
def background_log_updater():
|
145 |
while True:
|
146 |
time.sleep(3)
|
147 |
-
yield
|
148 |
|
149 |
demo.load(background_log_updater, outputs=log_output)
|
150 |
|
|
|
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 Bot Command Handler ---
|
21 |
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
22 |
cmd = ' '.join(context.args)
|
23 |
if not cmd:
|
|
|
25 |
return
|
26 |
|
27 |
log_queue.put(f"[Telegram] Received command: {cmd}")
|
|
|
28 |
try:
|
29 |
proc = await asyncio.create_subprocess_shell(
|
30 |
cmd,
|
|
|
43 |
if len(telegram_cmd_logs) > 50:
|
44 |
telegram_cmd_logs.pop(0)
|
45 |
|
46 |
+
# --- Terminal Command Function ---
|
47 |
+
def live_terminal(cmd):
|
48 |
+
if not cmd.strip():
|
49 |
+
yield "$ "
|
50 |
+
return
|
51 |
+
yield f"$ {cmd}\n"
|
52 |
+
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
53 |
+
for line in iter(process.stdout.readline, ''):
|
54 |
+
yield line
|
55 |
+
process.stdout.close()
|
56 |
+
process.wait()
|
57 |
+
yield "$ "
|
58 |
+
|
59 |
+
# --- Bot Start/Stop Functions ---
|
60 |
+
def start_bot_sync():
|
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:
|
|
|
101 |
logs.extend(telegram_cmd_logs[-20:])
|
102 |
return "\n".join(logs[-20:])
|
103 |
|
104 |
+
# --- Gradio UI ---
|
105 |
+
with gr.Blocks() as demo:
|
106 |
gr.Markdown("## π₯οΈ Live Terminal + Telegram Bot Interface")
|
107 |
|
108 |
with gr.Row():
|
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 |
with gr.Row():
|
113 |
+
run_btn = gr.Button("βΆοΈ Run")
|
114 |
start_btn = gr.Button("π€ Start Telegram Bot")
|
115 |
stop_btn = gr.Button("π Stop Telegram Bot")
|
116 |
|
117 |
+
log_output = gr.Textbox(label="π Bot & Telegram Logs", lines=15, interactive=False)
|
118 |
|
119 |
+
# Streaming terminal output
|
120 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
121 |
|
122 |
+
# Start/Stop bot
|
123 |
+
start_btn.click(lambda: update_log_box() or start_bot_sync(), outputs=log_output)
|
124 |
+
stop_btn.click(lambda: update_log_box() or stop_bot_sync(), outputs=log_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 update_log_box()
|
131 |
|
132 |
demo.load(background_log_updater, outputs=log_output)
|
133 |
|