Kanhshsh commited on
Commit
479d580
·
verified ·
1 Parent(s): 09b450c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -4,19 +4,19 @@ import asyncio
4
  import threading
5
  from queue import Queue, Empty
6
  import os
 
7
  from telegram import Update
8
  from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
9
 
10
- # Env vars or hardcode your Telegram bot token here
11
  BOT_TOKEN = os.environ.get("BOT_TOKEN", "7510817339:AAHFZoFzPkUO_-nAwfh9bjY_qHsWMprM2PI")
12
- # Globals
13
- bot_app = None # Telegram Application instance
14
  bot_thread = None
15
  bot_running = False
16
  log_queue = Queue()
17
  telegram_cmd_logs = []
18
 
19
- # -- Telegram Bot Handlers --
20
 
21
  async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
22
  cmd = ' '.join(context.args)
@@ -26,7 +26,6 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
26
 
27
  log_queue.put(f"[Telegram] Received command: {cmd}")
28
 
29
- # Run command and collect output (limit max 4000 chars for Telegram)
30
  try:
31
  proc = await asyncio.create_subprocess_shell(
32
  cmd,
@@ -41,14 +40,12 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
41
  output = f"Error running command: {e}"
42
 
43
  await update.message.reply_text(f"$ {cmd}\n{output}")
44
-
45
  telegram_cmd_logs.append(f"$ {cmd}\n{output}")
46
- # Limit logs to last 50 commands
47
  if len(telegram_cmd_logs) > 50:
48
  telegram_cmd_logs.pop(0)
49
 
50
  def start_bot():
51
- global bot_app, bot_running
52
 
53
  if bot_running:
54
  log_queue.put("[Bot] Already running.")
@@ -68,12 +65,12 @@ def start_bot():
68
  asyncio.set_event_loop(loop)
69
  loop.run_until_complete(run_bot())
70
 
71
- global bot_thread
72
  bot_thread = threading.Thread(target=runner, daemon=True)
73
  bot_thread.start()
74
 
75
  def stop_bot():
76
  global bot_app, bot_running
 
77
  if not bot_running:
78
  log_queue.put("[Bot] Bot is not running.")
79
  return
@@ -86,7 +83,7 @@ def stop_bot():
86
  asyncio.run(shutdown())
87
  bot_running = False
88
 
89
- # -- Gradio Web Terminal --
90
 
91
  def live_terminal(cmd):
92
  if not cmd.strip():
@@ -105,8 +102,9 @@ def live_terminal(cmd):
105
  def run_terminal(cmd):
106
  return live_terminal(cmd)
107
 
 
 
108
  def update_log_box():
109
- # Show last 20 logs
110
  logs = []
111
  try:
112
  while True:
@@ -118,25 +116,23 @@ def update_log_box():
118
  logs.extend(telegram_cmd_logs[-20:])
119
  return "\n".join(logs[-20:])
120
 
121
- # -- Gradio UI --
122
 
123
  with gr.Blocks() as demo:
124
- gr.Markdown("### Live Terminal with Telegram Bot Control")
125
 
126
  with gr.Row():
127
  terminal_output = gr.Textbox(value="$ ", label="Live Terminal", lines=20, interactive=False)
128
- cmd_input = gr.Textbox(placeholder="Type command here and press Enter", label="Command Input")
129
 
130
  with gr.Row():
131
- start_btn = gr.Button("Start Telegram Bot")
132
- stop_btn = gr.Button("Stop Telegram Bot")
133
 
134
- log_output = gr.Textbox(value="", label="Bot Logs & Telegram Bash Command Logs", lines=15, interactive=False)
135
 
136
- # Command input triggers terminal
137
  cmd_input.submit(run_terminal, inputs=cmd_input, outputs=terminal_output)
138
 
139
- # Start/stop buttons update logs
140
  def start_click():
141
  start_bot()
142
  return update_log_box()
@@ -148,10 +144,13 @@ with gr.Blocks() as demo:
148
  start_btn.click(start_click, outputs=log_output)
149
  stop_btn.click(stop_click, outputs=log_output)
150
 
151
- # Auto-update logs every 3 seconds
152
- def auto_update():
153
- return update_log_box()
 
154
 
155
- demo.load(auto_update, None, log_output, every=3)
 
 
156
 
157
  demo.launch()
 
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
  BOT_TOKEN = os.environ.get("BOT_TOKEN", "7510817339:AAHFZoFzPkUO_-nAwfh9bjY_qHsWMprM2PI")
12
+
13
+ bot_app = None
14
  bot_thread = None
15
  bot_running = False
16
  log_queue = Queue()
17
  telegram_cmd_logs = []
18
 
19
+ # --- Telegram Bot Handlers ---
20
 
21
  async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
22
  cmd = ' '.join(context.args)
 
26
 
27
  log_queue.put(f"[Telegram] Received command: {cmd}")
28
 
 
29
  try:
30
  proc = await asyncio.create_subprocess_shell(
31
  cmd,
 
40
  output = f"Error running command: {e}"
41
 
42
  await update.message.reply_text(f"$ {cmd}\n{output}")
 
43
  telegram_cmd_logs.append(f"$ {cmd}\n{output}")
 
44
  if len(telegram_cmd_logs) > 50:
45
  telegram_cmd_logs.pop(0)
46
 
47
  def start_bot():
48
+ global bot_app, bot_running, bot_thread
49
 
50
  if bot_running:
51
  log_queue.put("[Bot] Already running.")
 
65
  asyncio.set_event_loop(loop)
66
  loop.run_until_complete(run_bot())
67
 
 
68
  bot_thread = threading.Thread(target=runner, daemon=True)
69
  bot_thread.start()
70
 
71
  def stop_bot():
72
  global bot_app, bot_running
73
+
74
  if not bot_running:
75
  log_queue.put("[Bot] Bot is not running.")
76
  return
 
83
  asyncio.run(shutdown())
84
  bot_running = False
85
 
86
+ # --- Terminal Execution ---
87
 
88
  def live_terminal(cmd):
89
  if not cmd.strip():
 
102
  def run_terminal(cmd):
103
  return live_terminal(cmd)
104
 
105
+ # --- Log Update Function ---
106
+
107
  def update_log_box():
 
108
  logs = []
109
  try:
110
  while True:
 
116
  logs.extend(telegram_cmd_logs[-20:])
117
  return "\n".join(logs[-20:])
118
 
119
+ # --- Gradio UI ---
120
 
121
  with gr.Blocks() as demo:
122
+ gr.Markdown("## 🖥️ Live Terminal + Telegram Bot Interface")
123
 
124
  with gr.Row():
125
  terminal_output = gr.Textbox(value="$ ", label="Live Terminal", lines=20, interactive=False)
126
+ cmd_input = gr.Textbox(placeholder="Type command here", label="Command Input")
127
 
128
  with gr.Row():
129
+ start_btn = gr.Button("▶️ Start Telegram Bot")
130
+ stop_btn = gr.Button("⏹️ Stop Telegram Bot")
131
 
132
+ log_output = gr.Textbox(value="", label="📜 Bot & Telegram Logs", lines=15, interactive=False)
133
 
 
134
  cmd_input.submit(run_terminal, inputs=cmd_input, outputs=terminal_output)
135
 
 
136
  def start_click():
137
  start_bot()
138
  return update_log_box()
 
144
  start_btn.click(start_click, outputs=log_output)
145
  stop_btn.click(stop_click, outputs=log_output)
146
 
147
+ def background_log_updater():
148
+ while True:
149
+ time.sleep(3)
150
+ yield update_log_box()
151
 
152
+ demo.load(None, outputs=log_output, every=None).then(
153
+ background_log_updater, outputs=log_output
154
+ )
155
 
156
  demo.launch()