Kanhshsh commited on
Commit
2142971
Β·
verified Β·
1 Parent(s): 7d376c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -42
app.py CHANGED
@@ -8,7 +8,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
@@ -16,8 +16,7 @@ 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)
23
  if not cmd:
@@ -37,19 +36,15 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
37
  if len(output) > 4000:
38
  output = output[:4000] + "\n...[truncated]"
39
  except Exception as e:
40
- output = f"Error running command: {e}"
41
-
42
- result = f"$ {cmd}\n{output}"
43
- await update.message.reply_text(result)
44
 
45
- log_queue.put(result)
46
- telegram_cmd_logs.append(result)
47
  if len(telegram_cmd_logs) > 50:
48
  telegram_cmd_logs.pop(0)
49
 
50
  def start_bot():
51
  global bot_app, bot_running, bot_thread
52
-
53
  if bot_running:
54
  log_queue.put("[Bot] Already running.")
55
  return
@@ -60,20 +55,22 @@ def start_bot():
60
  bot_app.add_handler(CommandHandler("bash", bash_command))
61
  bot_running = True
62
  log_queue.put("[Bot] Bot started.")
63
- await bot_app.run_polling(stop_signals=None)
64
 
65
  loop = asyncio.new_event_loop()
66
 
67
  def runner():
68
  asyncio.set_event_loop(loop)
69
- loop.run_until_complete(run_bot())
 
 
 
70
 
71
  bot_thread = threading.Thread(target=runner, daemon=True)
72
  bot_thread.start()
73
 
74
  def stop_bot():
75
  global bot_app, bot_running
76
-
77
  if not bot_running:
78
  log_queue.put("[Bot] Bot is not running.")
79
  return
@@ -86,57 +83,52 @@ def stop_bot():
86
  asyncio.run(shutdown())
87
  bot_running = False
88
 
89
- # --- Terminal Execution ---
90
-
91
  def live_terminal(cmd):
92
  if not cmd.strip():
93
- yield "$ "
94
  return
95
- yield f"$ {cmd}\n"
96
- process = subprocess.Popen(
97
- cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
98
- )
99
  for line in iter(process.stdout.readline, ''):
100
- yield line
 
101
  process.stdout.close()
102
  process.wait()
103
- yield "$ "
104
-
105
- def run_terminal(cmd):
106
- return live_terminal(cmd)
107
-
108
- # --- Log Update Function ---
109
 
 
110
  def update_log_box():
111
  logs = []
112
  try:
113
  while True:
114
- log = log_queue.get_nowait()
115
- logs.append(log)
116
  except Empty:
117
  pass
118
-
119
  logs.extend(telegram_cmd_logs[-20:])
120
  return "\n".join(logs[-20:])
121
 
122
- # --- Gradio UI ---
123
-
124
- with gr.Blocks() as demo:
125
  gr.Markdown("## πŸ–₯️ Live Terminal + Telegram Bot Interface")
126
 
127
  with gr.Row():
128
- terminal_output = gr.Textbox(value="$ ", label="Live Terminal", lines=20, interactive=False)
129
- cmd_input = gr.Textbox(placeholder="Type command here", label="Command Input")
 
130
 
131
  with gr.Row():
132
- start_btn = gr.Button("▢️ Start Telegram Bot")
133
- stop_btn = gr.Button("⏹️ Stop Telegram Bot")
134
 
135
- log_output = gr.Textbox(value="", label="πŸ“œ Bot & Telegram Logs", lines=15, interactive=False)
136
 
137
- # Use stream for generator-based live output
138
- cmd_input.stream(run_terminal, inputs=cmd_input, outputs=terminal_output)
139
 
 
140
  def start_click():
141
  start_bot()
142
  return update_log_box()
@@ -148,11 +140,11 @@ 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-updating logs using Gradio v5-style load
152
  def background_log_updater():
153
  while True:
154
  time.sleep(3)
155
- yield update_log_box()
156
 
157
  demo.load(background_log_updater, outputs=log_output)
158
 
 
8
  from telegram import Update
9
  from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
10
 
11
+ BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN")
12
 
13
  bot_app = None
14
  bot_thread = None
 
16
  log_queue = Queue()
17
  telegram_cmd_logs = []
18
 
19
+ # Telegram bot handler
 
20
  async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
21
  cmd = ' '.join(context.args)
22
  if not cmd:
 
36
  if len(output) > 4000:
37
  output = output[:4000] + "\n...[truncated]"
38
  except Exception as e:
39
+ output = f"Error: {e}"
 
 
 
40
 
41
+ await update.message.reply_text(f"$ {cmd}\n{output}")
42
+ telegram_cmd_logs.append(f"$ {cmd}\n{output}")
43
  if len(telegram_cmd_logs) > 50:
44
  telegram_cmd_logs.pop(0)
45
 
46
  def start_bot():
47
  global bot_app, bot_running, bot_thread
 
48
  if bot_running:
49
  log_queue.put("[Bot] Already running.")
50
  return
 
55
  bot_app.add_handler(CommandHandler("bash", bash_command))
56
  bot_running = True
57
  log_queue.put("[Bot] Bot started.")
58
+ await bot_app.run_polling()
59
 
60
  loop = asyncio.new_event_loop()
61
 
62
  def runner():
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] Bot is not running.")
76
  return
 
83
  asyncio.run(shutdown())
84
  bot_running = False
85
 
86
+ # Real live terminal generator
 
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:
106
  while True:
107
+ logs.append(log_queue.get_nowait())
 
108
  except Empty:
109
  pass
 
110
  logs.extend(telegram_cmd_logs[-20:])
111
  return "\n".join(logs[-20:])
112
 
113
+ # UI
114
+ with gr.Blocks(title="πŸ“š TheMovieProviderBot Interface") as demo:
 
115
  gr.Markdown("## πŸ–₯️ Live Terminal + Telegram Bot Interface")
116
 
117
  with gr.Row():
118
+ terminal_output = gr.TextArea(label="πŸ“Ÿ Live Terminal", lines=20)
119
+ cmd_input = gr.Textbox(label="⌨️ Enter Command")
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
+ # Stream terminal output
129
+ run_btn.stream(live_terminal, inputs=cmd_input, outputs=terminal_output)
130
 
131
+ # Start/Stop handlers
132
  def start_click():
133
  start_bot()
134
  return update_log_box()
 
140
  start_btn.click(start_click, outputs=log_output)
141
  stop_btn.click(stop_click, outputs=log_output)
142
 
143
+ # Background log auto-updater
144
  def background_log_updater():
145
  while True:
146
  time.sleep(3)
147
+ yield gr.Textbox.update(value=update_log_box())
148
 
149
  demo.load(background_log_updater, outputs=log_output)
150