Kanhshsh commited on
Commit
7723ce0
Β·
verified Β·
1 Parent(s): 0a26dc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -61
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
- all_logs = []
15
- telegram_cmd_logs = []
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
- log_entry = f"[Telegram] $ {cmd}"
28
- log_queue.put(log_entry)
29
- add_log(log_entry)
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
- telegram_cmd_logs.append(result)
42
- add_log(result)
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] Bot already running or no token.")
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 Telegram bot...")
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 is not running.")
71
  return
72
 
73
  async def shutdown():
@@ -78,76 +113,53 @@ def stop_bot():
78
  asyncio.run(shutdown())
79
  bot_running = False
80
 
81
- # --- Logging ---
82
- def add_log(entry):
83
- all_logs.append(entry)
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
- try:
95
- proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
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
- # --- Log Updater ---
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("## πŸ–₯️ Live Terminal with Telegram Bot")
122
 
123
  with gr.Row():
124
- terminal_output = gr.Textbox(label="Live Terminal Output", lines=20, interactive=False)
125
- cmd_input = gr.Textbox(placeholder="Enter shell command", label="Command Input")
126
 
127
- run_btn = gr.Button("▢️ Run")
128
- start_btn = gr.Button("πŸš€ Start Telegram Bot")
129
- stop_btn = gr.Button("πŸ›‘ Stop Telegram Bot")
130
 
131
- log_output = gr.Textbox(label="πŸ“œ Logs (Recent 100 shown)", lines=20, interactive=False)
 
 
132
 
133
  run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
134
 
135
  def handle_start():
136
  start_bot()
137
- return update_logs()
138
 
139
  def handle_stop():
140
  stop_bot()
141
- return update_logs()
 
 
 
142
 
143
- start_btn.click(fn=handle_start, outputs=log_output)
144
- stop_btn.click(fn=handle_stop, outputs=log_output)
 
 
145
 
146
- def auto_log_updater():
147
  while True:
148
- yield update_logs()
149
  import time; time.sleep(3)
 
150
 
151
- demo.load(fn=auto_log_updater, outputs=log_output)
 
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()