Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -91,32 +91,34 @@ bot_app = None
|
|
91 |
bot_running = False
|
92 |
bot_thread = None
|
93 |
bot_logs = []
|
94 |
-
|
95 |
MAX_LOGS = 20000
|
96 |
|
97 |
-
# --- Logging
|
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 |
-
# ---
|
|
|
|
|
|
|
|
|
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
|
109 |
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
110 |
cmd = ' '.join(context.args)
|
111 |
if not cmd:
|
112 |
await update.message.reply_text("Usage: /bash <command>")
|
113 |
return
|
114 |
|
115 |
-
|
116 |
-
add_bot_log(prefix)
|
117 |
|
118 |
try:
|
119 |
-
result = execute_command(cmd)
|
120 |
except Exception as e:
|
121 |
result = f"Error: {e}"
|
122 |
|
@@ -131,32 +133,27 @@ def start_bot():
|
|
131 |
return
|
132 |
|
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 |
-
|
139 |
-
|
140 |
-
|
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.
|
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 |
-
|
159 |
-
add_bot_log("[Bot] Bot started.")
|
160 |
|
161 |
# --- Stop Bot ---
|
162 |
def stop_bot():
|
@@ -166,22 +163,21 @@ def stop_bot():
|
|
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 |
-
# ---
|
|
|
|
|
179 |
# --- UI Update Functions ---
|
180 |
def update_terminal_logs():
|
181 |
return "\n".join(terminal_logs[-100:])
|
182 |
|
183 |
-
def update_bot_logs():
|
184 |
-
return "\n".join(bot_logs[-100:])
|
185 |
|
186 |
def live_terminal(cmd):
|
187 |
result = [f"$ {cmd}"]
|
|
|
91 |
bot_running = False
|
92 |
bot_thread = None
|
93 |
bot_logs = []
|
|
|
94 |
MAX_LOGS = 20000
|
95 |
|
96 |
+
# --- Logging ---
|
97 |
def add_bot_log(entry):
|
98 |
bot_logs.append(entry)
|
99 |
if len(bot_logs) > MAX_LOGS:
|
100 |
bot_logs.pop(0)
|
101 |
|
102 |
+
# --- Placeholder: You must define this elsewhere ---
|
103 |
+
def execute_command(cmd):
|
104 |
+
return f"Executed: {cmd}"
|
105 |
+
|
106 |
+
# --- /start Handler ---
|
107 |
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
108 |
await update.message.reply_text("Hello! I'm your terminal bot.\nUse /bash <command> to execute shell commands.")
|
109 |
add_bot_log("[Bot] Received /start command.")
|
110 |
|
111 |
+
# --- /bash Handler ---
|
112 |
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
113 |
cmd = ' '.join(context.args)
|
114 |
if not cmd:
|
115 |
await update.message.reply_text("Usage: /bash <command>")
|
116 |
return
|
117 |
|
118 |
+
add_bot_log(f"[Bot] $ {cmd}")
|
|
|
119 |
|
120 |
try:
|
121 |
+
result = execute_command(cmd)
|
122 |
except Exception as e:
|
123 |
result = f"Error: {e}"
|
124 |
|
|
|
133 |
return
|
134 |
|
135 |
async def run_bot():
|
136 |
+
global bot_app, bot_running
|
137 |
+
add_bot_log("[Bot] Starting bot...")
|
138 |
bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
|
139 |
bot_app.add_handler(CommandHandler("start", start_command))
|
140 |
bot_app.add_handler(CommandHandler("bash", bash_command))
|
141 |
+
bot_running = True
|
142 |
+
await bot_app.run_polling()
|
143 |
+
add_bot_log("[Bot] Bot stopped.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
bot_running = False
|
145 |
|
146 |
def runner():
|
147 |
try:
|
148 |
+
asyncio.run(run_bot())
|
149 |
except Exception as e:
|
150 |
add_bot_log(f"[Bot Error] {e}")
|
151 |
+
global bot_running
|
152 |
+
bot_running = False
|
153 |
|
154 |
bot_thread = threading.Thread(target=runner, daemon=True)
|
155 |
bot_thread.start()
|
156 |
+
add_bot_log("[Bot] Bot thread started.")
|
|
|
157 |
|
158 |
# --- Stop Bot ---
|
159 |
def stop_bot():
|
|
|
163 |
return
|
164 |
|
165 |
async def shutdown():
|
|
|
|
|
166 |
await bot_app.shutdown()
|
167 |
+
await bot_app.stop()
|
168 |
add_bot_log("[Bot] Bot stopped.")
|
169 |
global bot_running
|
170 |
bot_running = False
|
171 |
|
172 |
threading.Thread(target=lambda: asyncio.run(shutdown()), daemon=True).start()
|
173 |
|
174 |
+
# --- Optional: Live UI logs if used elsewhere ---
|
175 |
+
def update_bot_logs():
|
176 |
+
return "\n".join(bot_logs[-100:])
|
177 |
# --- UI Update Functions ---
|
178 |
def update_terminal_logs():
|
179 |
return "\n".join(terminal_logs[-100:])
|
180 |
|
|
|
|
|
181 |
|
182 |
def live_terminal(cmd):
|
183 |
result = [f"$ {cmd}"]
|