Kanhshsh commited on
Commit
e9261eb
·
verified ·
1 Parent(s): 541eb7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -8
app.py CHANGED
@@ -81,6 +81,31 @@ def execute_command(cmd):
81
  return "\n".join(output)
82
 
83
  # --- Telegram Bot Handlers ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
85
  cmd = ' '.join(context.args)
86
  if not cmd:
@@ -91,14 +116,14 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
91
  add_bot_log(prefix)
92
 
93
  try:
94
- result = execute_command(cmd)
95
  except Exception as e:
96
  result = f"Error: {e}"
97
 
98
  add_bot_log(result)
99
  await update.message.reply_text(f"$ {cmd}\n{result}")
100
 
101
- # --- Bot Start/Stop Logic ---
102
  def start_bot():
103
  global bot_app, bot_running, bot_thread, BOT_TOKEN
104
  if bot_running or not BOT_TOKEN:
@@ -108,32 +133,49 @@ def start_bot():
108
  async def run_bot():
109
  global bot_app
110
  bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
 
111
  bot_app.add_handler(CommandHandler("bash", bash_command))
112
  add_bot_log("[Bot] Starting bot...")
113
- await bot_app.run_polling()
 
 
 
 
 
 
 
 
 
114
 
115
  def runner():
116
- asyncio.run(run_bot())
 
 
 
117
 
118
  bot_thread = threading.Thread(target=runner, daemon=True)
119
  bot_thread.start()
120
  bot_running = True
121
  add_bot_log("[Bot] Bot started.")
122
 
 
123
  def stop_bot():
124
  global bot_app, bot_running
125
- if not bot_running:
126
  add_bot_log("[Bot] Bot not running.")
127
  return
128
 
129
  async def shutdown():
130
- await bot_app.shutdown()
131
  await bot_app.stop()
 
132
  add_bot_log("[Bot] Bot stopped.")
 
 
133
 
134
- asyncio.run(shutdown())
135
- bot_running = False
136
 
 
137
  # --- UI Update Functions ---
138
  def update_terminal_logs():
139
  return "\n".join(terminal_logs[-100:])
 
81
  return "\n".join(output)
82
 
83
  # --- Telegram Bot Handlers ---
84
+ from telegram import Update
85
+ from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
86
+ import threading
87
+ import asyncio
88
+
89
+ BOT_TOKEN = ""
90
+ bot_app = None
91
+ bot_running = False
92
+ bot_thread = None
93
+ bot_logs = []
94
+
95
+ MAX_LOGS = 20000
96
+
97
+ # --- Logging for Bot ---
98
+ def add_bot_log(entry):
99
+ bot_logs.append(entry)
100
+ if len(bot_logs) > MAX_LOGS:
101
+ bot_logs.pop(0)
102
+
103
+ # --- /start Command ---
104
+ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
105
+ await update.message.reply_text("Hello! I'm your terminal bot.\nUse /bash <command> to execute shell commands.")
106
+ add_bot_log("[Bot] Received /start command.")
107
+
108
+ # --- /bash Command ---
109
  async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
110
  cmd = ' '.join(context.args)
111
  if not cmd:
 
116
  add_bot_log(prefix)
117
 
118
  try:
119
+ result = execute_command(cmd) # This must be defined elsewhere
120
  except Exception as e:
121
  result = f"Error: {e}"
122
 
123
  add_bot_log(result)
124
  await update.message.reply_text(f"$ {cmd}\n{result}")
125
 
126
+ # --- Start Bot ---
127
  def start_bot():
128
  global bot_app, bot_running, bot_thread, BOT_TOKEN
129
  if bot_running or not BOT_TOKEN:
 
133
  async def run_bot():
134
  global bot_app
135
  bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
136
+ bot_app.add_handler(CommandHandler("start", start_command))
137
  bot_app.add_handler(CommandHandler("bash", bash_command))
138
  add_bot_log("[Bot] Starting bot...")
139
+
140
+ await bot_app.initialize()
141
+ await bot_app.start()
142
+ await bot_app.updater.start_polling()
143
+ await bot_app.updater.idle()
144
+ await bot_app.stop()
145
+ await bot_app.shutdown()
146
+ add_bot_log("[Bot] Bot has fully stopped.")
147
+ global bot_running
148
+ bot_running = False
149
 
150
  def runner():
151
+ try:
152
+ asyncio.new_event_loop().run_until_complete(run_bot())
153
+ except Exception as e:
154
+ add_bot_log(f"[Bot Error] {e}")
155
 
156
  bot_thread = threading.Thread(target=runner, daemon=True)
157
  bot_thread.start()
158
  bot_running = True
159
  add_bot_log("[Bot] Bot started.")
160
 
161
+ # --- Stop Bot ---
162
  def stop_bot():
163
  global bot_app, bot_running
164
+ if not bot_running or not bot_app:
165
  add_bot_log("[Bot] Bot not running.")
166
  return
167
 
168
  async def shutdown():
169
+ await bot_app.updater.stop()
170
  await bot_app.stop()
171
+ await bot_app.shutdown()
172
  add_bot_log("[Bot] Bot stopped.")
173
+ global bot_running
174
+ bot_running = False
175
 
176
+ threading.Thread(target=lambda: asyncio.run(shutdown()), daemon=True).start()
 
177
 
178
+ # --- Bot
179
  # --- UI Update Functions ---
180
  def update_terminal_logs():
181
  return "\n".join(terminal_logs[-100:])