Update app.py
Browse files
app.py
CHANGED
|
@@ -49,7 +49,7 @@ live_log_thread = None
|
|
| 49 |
|
| 50 |
# Live logging globals
|
| 51 |
live_log_lines = [] # Rolling list (max 50 log lines)
|
| 52 |
-
live_log_display = "" # Global variable updated every second by live_log_updater
|
| 53 |
error_notification = "" # Global variable to hold error details if any
|
| 54 |
|
| 55 |
# -------------------------------------------------------------------
|
|
@@ -155,6 +155,17 @@ def validate_inputs():
|
|
| 155 |
return False, f"Missing fields: {', '.join(missing)}"
|
| 156 |
return True, ""
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# -------------------------------------------------------------------
|
| 159 |
# Error Notification Helper
|
| 160 |
# -------------------------------------------------------------------
|
|
@@ -170,7 +181,7 @@ def live_log_updater():
|
|
| 170 |
global live_log_display, streaming_state
|
| 171 |
try:
|
| 172 |
while streaming_state in ["streaming", "paused"]:
|
| 173 |
-
# Update
|
| 174 |
live_log_display = "<pre>" + "\n".join(live_log_lines[-15:]) + "</pre>"
|
| 175 |
time.sleep(1)
|
| 176 |
except Exception as e:
|
|
@@ -181,9 +192,7 @@ def live_log_updater():
|
|
| 181 |
# -------------------------------------------------------------------
|
| 182 |
def logs_history(chat_id):
|
| 183 |
global live_log_display, error_notification
|
| 184 |
-
# Use the global live_log_display if available; otherwise, show a default message.
|
| 185 |
log_text = live_log_display if live_log_display else "<pre>No logs available yet.</pre>"
|
| 186 |
-
# If an error occurred, prepend the error notification.
|
| 187 |
if error_notification:
|
| 188 |
if log_text.startswith("<pre>"):
|
| 189 |
log_text = f"<pre>ERROR: {error_notification}\n\n" + log_text[5:]
|
|
@@ -196,39 +205,28 @@ def logs_history(chat_id):
|
|
| 196 |
"parse_mode": "HTML"
|
| 197 |
}
|
| 198 |
|
| 199 |
-
# -------------------------------------------------------------------
|
| 200 |
-
# Helper to Compose the Streaming Message
|
| 201 |
-
# -------------------------------------------------------------------
|
| 202 |
-
def compose_streaming_message():
|
| 203 |
-
global live_log_display, error_notification
|
| 204 |
-
msg = ""
|
| 205 |
-
if error_notification:
|
| 206 |
-
msg += "<b>ERROR:</b> " + error_notification + "\n\n"
|
| 207 |
-
msg += "π <b>Streaming in progress!</b>\n\nLive Logs:\n" + live_log_display + "\n\nUse the inline keyboard to control the stream."
|
| 208 |
-
return msg
|
| 209 |
-
|
| 210 |
# -------------------------------------------------------------------
|
| 211 |
# Conversation Handlers
|
| 212 |
# -------------------------------------------------------------------
|
| 213 |
def handle_start(chat_id):
|
| 214 |
global current_step, user_inputs, conversation_fields, advanced_mode
|
| 215 |
-
#
|
| 216 |
user_inputs = {}
|
| 217 |
if not advanced_mode:
|
| 218 |
conversation_fields = ["input_url", "output_url"]
|
| 219 |
else:
|
| 220 |
conversation_fields = ["input_url", "quality_settings", "video_codec", "audio_codec", "output_url"]
|
| 221 |
current_step = conversation_fields[0]
|
| 222 |
-
text = ("π
|
| 223 |
"Let's set up your stream.\n"
|
| 224 |
-
f"Please enter the
|
| 225 |
-
f"{' (no default)' if current_step not in default_settings else f'
|
| 226 |
logging.info(f"/start command from chat {chat_id} (advanced_mode={advanced_mode})")
|
| 227 |
return {
|
| 228 |
"method": "sendMessage",
|
| 229 |
"chat_id": chat_id,
|
| 230 |
"text": text,
|
| 231 |
-
"parse_mode": "
|
| 232 |
}
|
| 233 |
|
| 234 |
def handle_setting(chat_id):
|
|
@@ -237,14 +235,14 @@ def handle_setting(chat_id):
|
|
| 237 |
conversation_fields = ["input_url", "quality_settings", "video_codec", "audio_codec", "output_url"]
|
| 238 |
user_inputs = {}
|
| 239 |
current_step = conversation_fields[0]
|
| 240 |
-
text = ("βοΈ
|
| 241 |
-
"Please enter the
|
| 242 |
logging.info(f"/setting command from chat {chat_id} - advanced mode enabled")
|
| 243 |
return {
|
| 244 |
"method": "sendMessage",
|
| 245 |
"chat_id": chat_id,
|
| 246 |
"text": text,
|
| 247 |
-
"parse_mode": "
|
| 248 |
}
|
| 249 |
|
| 250 |
def handle_help(chat_id):
|
|
@@ -268,10 +266,15 @@ def handle_conversation(chat_id, text):
|
|
| 268 |
idx = conversation_fields.index(current_step)
|
| 269 |
if idx < len(conversation_fields) - 1:
|
| 270 |
current_step = conversation_fields[idx + 1]
|
| 271 |
-
prompt = f"Please enter the
|
| 272 |
if current_step in default_settings:
|
| 273 |
-
prompt += f"
|
| 274 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
else:
|
| 276 |
current_step = None
|
| 277 |
valid, msg = validate_inputs()
|
|
@@ -284,9 +287,9 @@ def handle_conversation(chat_id, text):
|
|
| 284 |
return {
|
| 285 |
"method": "sendMessage",
|
| 286 |
"chat_id": chat_id,
|
| 287 |
-
"text": "All inputs received. Press
|
| 288 |
"reply_markup": get_inline_keyboard_for_start(),
|
| 289 |
-
"parse_mode": "
|
| 290 |
}
|
| 291 |
else:
|
| 292 |
return send_guide_message(chat_id, "Unrecognized input. Type /help for available commands.")
|
|
@@ -393,7 +396,6 @@ def start_streaming(chat_id):
|
|
| 393 |
stream_thread.daemon = True
|
| 394 |
stream_thread.start()
|
| 395 |
logging.info("Streaming thread started.")
|
| 396 |
-
# Return a message that includes live log display and inline keyboard.
|
| 397 |
return {
|
| 398 |
"method": "sendMessage",
|
| 399 |
"chat_id": chat_id,
|
|
@@ -510,13 +512,11 @@ async def telegram_webhook(request: Request):
|
|
| 510 |
response = start_streaming(chat_id)
|
| 511 |
else:
|
| 512 |
response = send_guide_message(chat_id, "β Unknown callback command.")
|
| 513 |
-
# For inline queries that control streaming, update the message text
|
| 514 |
if callback_data in ["pause", "resume", "abort", "status", "start_stream"]:
|
| 515 |
response["method"] = "editMessageText"
|
| 516 |
response["message_id"] = message_id
|
| 517 |
response["text"] = compose_streaming_message()
|
| 518 |
response["parse_mode"] = "HTML"
|
| 519 |
-
# Always include the inline keyboard if streaming is active
|
| 520 |
if streaming_state in ["streaming", "paused"]:
|
| 521 |
response["reply_markup"] = get_inline_keyboard_for_stream()
|
| 522 |
return response
|
|
|
|
| 49 |
|
| 50 |
# Live logging globals
|
| 51 |
live_log_lines = [] # Rolling list (max 50 log lines)
|
| 52 |
+
live_log_display = "" # Global variable updated every second by live_log_updater()
|
| 53 |
error_notification = "" # Global variable to hold error details if any
|
| 54 |
|
| 55 |
# -------------------------------------------------------------------
|
|
|
|
| 155 |
return False, f"Missing fields: {', '.join(missing)}"
|
| 156 |
return True, ""
|
| 157 |
|
| 158 |
+
# -------------------------------------------------------------------
|
| 159 |
+
# Helper to Compose the Streaming Message (HTML)
|
| 160 |
+
# -------------------------------------------------------------------
|
| 161 |
+
def compose_streaming_message():
|
| 162 |
+
global live_log_display, error_notification
|
| 163 |
+
msg = ""
|
| 164 |
+
if error_notification:
|
| 165 |
+
msg += "<b>ERROR:</b> " + error_notification + "\n\n"
|
| 166 |
+
msg += "π <b>Streaming in progress!</b>\n\nLive Logs:\n" + live_log_display + "\n\nUse the inline keyboard to control the stream."
|
| 167 |
+
return msg
|
| 168 |
+
|
| 169 |
# -------------------------------------------------------------------
|
| 170 |
# Error Notification Helper
|
| 171 |
# -------------------------------------------------------------------
|
|
|
|
| 181 |
global live_log_display, streaming_state
|
| 182 |
try:
|
| 183 |
while streaming_state in ["streaming", "paused"]:
|
| 184 |
+
# Update live_log_display with the last 15 log lines (HTML formatted)
|
| 185 |
live_log_display = "<pre>" + "\n".join(live_log_lines[-15:]) + "</pre>"
|
| 186 |
time.sleep(1)
|
| 187 |
except Exception as e:
|
|
|
|
| 192 |
# -------------------------------------------------------------------
|
| 193 |
def logs_history(chat_id):
|
| 194 |
global live_log_display, error_notification
|
|
|
|
| 195 |
log_text = live_log_display if live_log_display else "<pre>No logs available yet.</pre>"
|
|
|
|
| 196 |
if error_notification:
|
| 197 |
if log_text.startswith("<pre>"):
|
| 198 |
log_text = f"<pre>ERROR: {error_notification}\n\n" + log_text[5:]
|
|
|
|
| 205 |
"parse_mode": "HTML"
|
| 206 |
}
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
# -------------------------------------------------------------------
|
| 209 |
# Conversation Handlers
|
| 210 |
# -------------------------------------------------------------------
|
| 211 |
def handle_start(chat_id):
|
| 212 |
global current_step, user_inputs, conversation_fields, advanced_mode
|
| 213 |
+
# Use simple mode unless advanced_mode is set via /setting
|
| 214 |
user_inputs = {}
|
| 215 |
if not advanced_mode:
|
| 216 |
conversation_fields = ["input_url", "output_url"]
|
| 217 |
else:
|
| 218 |
conversation_fields = ["input_url", "quality_settings", "video_codec", "audio_codec", "output_url"]
|
| 219 |
current_step = conversation_fields[0]
|
| 220 |
+
text = ("π <b>Welcome to the Stream Bot!</b>\n\n"
|
| 221 |
"Let's set up your stream.\n"
|
| 222 |
+
f"Please enter the <b>{current_step.replace('_', ' ')}</b>"
|
| 223 |
+
f"{' (no default)' if current_step not in default_settings else f' (default: {default_settings[current_step]})'}:")
|
| 224 |
logging.info(f"/start command from chat {chat_id} (advanced_mode={advanced_mode})")
|
| 225 |
return {
|
| 226 |
"method": "sendMessage",
|
| 227 |
"chat_id": chat_id,
|
| 228 |
"text": text,
|
| 229 |
+
"parse_mode": "HTML"
|
| 230 |
}
|
| 231 |
|
| 232 |
def handle_setting(chat_id):
|
|
|
|
| 235 |
conversation_fields = ["input_url", "quality_settings", "video_codec", "audio_codec", "output_url"]
|
| 236 |
user_inputs = {}
|
| 237 |
current_step = conversation_fields[0]
|
| 238 |
+
text = ("βοΈ <b>Advanced Mode Activated!</b>\n\n"
|
| 239 |
+
"Please enter the <b>input url</b>:")
|
| 240 |
logging.info(f"/setting command from chat {chat_id} - advanced mode enabled")
|
| 241 |
return {
|
| 242 |
"method": "sendMessage",
|
| 243 |
"chat_id": chat_id,
|
| 244 |
"text": text,
|
| 245 |
+
"parse_mode": "HTML"
|
| 246 |
}
|
| 247 |
|
| 248 |
def handle_help(chat_id):
|
|
|
|
| 266 |
idx = conversation_fields.index(current_step)
|
| 267 |
if idx < len(conversation_fields) - 1:
|
| 268 |
current_step = conversation_fields[idx + 1]
|
| 269 |
+
prompt = f"Please enter the <b>{current_step.replace('_', ' ')}</b>"
|
| 270 |
if current_step in default_settings:
|
| 271 |
+
prompt += f" (default: {default_settings[current_step]})"
|
| 272 |
+
return {
|
| 273 |
+
"method": "sendMessage",
|
| 274 |
+
"chat_id": chat_id,
|
| 275 |
+
"text": prompt,
|
| 276 |
+
"parse_mode": "HTML"
|
| 277 |
+
}
|
| 278 |
else:
|
| 279 |
current_step = None
|
| 280 |
valid, msg = validate_inputs()
|
|
|
|
| 287 |
return {
|
| 288 |
"method": "sendMessage",
|
| 289 |
"chat_id": chat_id,
|
| 290 |
+
"text": "All inputs received. Press <b>π Start Streaming</b> to begin.",
|
| 291 |
"reply_markup": get_inline_keyboard_for_start(),
|
| 292 |
+
"parse_mode": "HTML"
|
| 293 |
}
|
| 294 |
else:
|
| 295 |
return send_guide_message(chat_id, "Unrecognized input. Type /help for available commands.")
|
|
|
|
| 396 |
stream_thread.daemon = True
|
| 397 |
stream_thread.start()
|
| 398 |
logging.info("Streaming thread started.")
|
|
|
|
| 399 |
return {
|
| 400 |
"method": "sendMessage",
|
| 401 |
"chat_id": chat_id,
|
|
|
|
| 512 |
response = start_streaming(chat_id)
|
| 513 |
else:
|
| 514 |
response = send_guide_message(chat_id, "β Unknown callback command.")
|
|
|
|
| 515 |
if callback_data in ["pause", "resume", "abort", "status", "start_stream"]:
|
| 516 |
response["method"] = "editMessageText"
|
| 517 |
response["message_id"] = message_id
|
| 518 |
response["text"] = compose_streaming_message()
|
| 519 |
response["parse_mode"] = "HTML"
|
|
|
|
| 520 |
if streaming_state in ["streaming", "paused"]:
|
| 521 |
response["reply_markup"] = get_inline_keyboard_for_stream()
|
| 522 |
return response
|