Update app.py
Browse files
app.py
CHANGED
|
@@ -15,8 +15,11 @@ app = FastAPI()
|
|
| 15 |
# -------------------------------------------------------------------
|
| 16 |
# Configuration & Global Variables
|
| 17 |
# -------------------------------------------------------------------
|
| 18 |
-
#
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Conversation state
|
| 22 |
user_inputs = {}
|
|
@@ -37,7 +40,7 @@ default_settings = {
|
|
| 37 |
|
| 38 |
# Streaming state & statistics
|
| 39 |
streaming_state = "idle" # "idle", "streaming", "paused", "stopped"
|
| 40 |
-
stream_chat_id = None
|
| 41 |
stream_start_time = None
|
| 42 |
frames_encoded = 0
|
| 43 |
bytes_sent = 0
|
|
@@ -55,6 +58,27 @@ live_log_thread = None
|
|
| 55 |
live_log_lines = [] # Rolling list (max 50 lines)
|
| 56 |
live_log_message_id = None
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# -------------------------------------------------------------------
|
| 59 |
# Enhanced Logging Setup
|
| 60 |
# -------------------------------------------------------------------
|
|
@@ -84,11 +108,11 @@ logger.addHandler(list_handler)
|
|
| 84 |
# Utility Functions & UI Helpers
|
| 85 |
# -------------------------------------------------------------------
|
| 86 |
def create_html_message(text: str):
|
| 87 |
-
# Wrap text in <pre> tags for monospaced output
|
| 88 |
return {"parse_mode": "HTML", "text": f"<pre>{text}</pre>"}
|
| 89 |
|
| 90 |
def get_inline_keyboard_for_stream():
|
| 91 |
-
# Inline keyboard for streaming controls after stream has started
|
| 92 |
keyboard = {
|
| 93 |
"inline_keyboard": [
|
| 94 |
[
|
|
@@ -163,13 +187,10 @@ def validate_inputs():
|
|
| 163 |
def notify_error(chat_id, error_message):
|
| 164 |
payload = {
|
| 165 |
"chat_id": chat_id,
|
| 166 |
-
"text": f"⚠️ *Streaming Error Occurred:*\n\n{error_message}\n\nPlease check logs for details.",
|
| 167 |
"parse_mode": "Markdown"
|
| 168 |
}
|
| 169 |
-
|
| 170 |
-
requests.post(f"{TELEGRAM_API_URL}/sendMessage", json=payload)
|
| 171 |
-
except Exception as e:
|
| 172 |
-
logging.error(f"Failed to notify error to user: {e}")
|
| 173 |
|
| 174 |
# -------------------------------------------------------------------
|
| 175 |
# Live Logging Updater (Background Thread)
|
|
@@ -183,8 +204,8 @@ def live_log_updater(chat_id):
|
|
| 183 |
"text": "<pre>Live Logs:\nInitializing...</pre>",
|
| 184 |
"parse_mode": "HTML"
|
| 185 |
}
|
| 186 |
-
resp =
|
| 187 |
-
if resp.ok:
|
| 188 |
live_log_message_id = resp.json()["result"]["message_id"]
|
| 189 |
logging.info(f"Live log message sent with id {live_log_message_id}")
|
| 190 |
else:
|
|
@@ -201,7 +222,7 @@ def live_log_updater(chat_id):
|
|
| 201 |
"text": log_text,
|
| 202 |
"parse_mode": "HTML"
|
| 203 |
}
|
| 204 |
-
|
| 205 |
time.sleep(1)
|
| 206 |
except Exception as e:
|
| 207 |
logging.error(f"Error in live log updater: {e}")
|
|
@@ -297,7 +318,7 @@ def handle_conversation(chat_id, text):
|
|
| 297 |
user_inputs.setdefault("quality_settings", default_settings["quality_settings"])
|
| 298 |
user_inputs.setdefault("video_codec", default_settings["video_codec"])
|
| 299 |
user_inputs.setdefault("audio_codec", default_settings["audio_codec"])
|
| 300 |
-
# Instead of asking user to type "start", send an inline button.
|
| 301 |
return {
|
| 302 |
"method": "sendMessage",
|
| 303 |
"chat_id": chat_id,
|
|
@@ -396,7 +417,7 @@ def stream_to_youtube(input_url, quality_settings, video_codec, audio_codec, out
|
|
| 396 |
error_message = f"An error occurred during streaming: {str(e)}\n\n{traceback.format_exc()}"
|
| 397 |
logging.error(error_message)
|
| 398 |
streaming_state = "idle"
|
| 399 |
-
# Notify the user about the error using the logs
|
| 400 |
notify_error(chat_id, error_message)
|
| 401 |
|
| 402 |
def start_streaming(chat_id):
|
|
@@ -429,7 +450,7 @@ def start_streaming(chat_id):
|
|
| 429 |
live_log_thread.start()
|
| 430 |
logging.info("Live log updater started.")
|
| 431 |
|
| 432 |
-
# Immediately
|
| 433 |
return {
|
| 434 |
"method": "sendMessage",
|
| 435 |
"chat_id": chat_id,
|
|
|
|
| 15 |
# -------------------------------------------------------------------
|
| 16 |
# Configuration & Global Variables
|
| 17 |
# -------------------------------------------------------------------
|
| 18 |
+
# We no longer use a global TELEGRAM_API_URL variable.
|
| 19 |
+
# The bot token is obtained on demand via the helper function below.
|
| 20 |
+
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 21 |
+
if not TELEGRAM_BOT_TOKEN:
|
| 22 |
+
raise ValueError("Please set the TELEGRAM_BOT_TOKEN environment variable.")
|
| 23 |
|
| 24 |
# Conversation state
|
| 25 |
user_inputs = {}
|
|
|
|
| 40 |
|
| 41 |
# Streaming state & statistics
|
| 42 |
streaming_state = "idle" # "idle", "streaming", "paused", "stopped"
|
| 43 |
+
stream_chat_id = None # Chat ID for periodic updates
|
| 44 |
stream_start_time = None
|
| 45 |
frames_encoded = 0
|
| 46 |
bytes_sent = 0
|
|
|
|
| 58 |
live_log_lines = [] # Rolling list (max 50 lines)
|
| 59 |
live_log_message_id = None
|
| 60 |
|
| 61 |
+
# -------------------------------------------------------------------
|
| 62 |
+
# Helper: Send Telegram API Request
|
| 63 |
+
# -------------------------------------------------------------------
|
| 64 |
+
def send_telegram_request(method: str, payload: dict):
|
| 65 |
+
"""
|
| 66 |
+
Helper to send a request to the Telegram API.
|
| 67 |
+
"""
|
| 68 |
+
bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 69 |
+
if not bot_token:
|
| 70 |
+
logging.error("Missing TELEGRAM_BOT_TOKEN environment variable.")
|
| 71 |
+
return None
|
| 72 |
+
url = f"https://api.telegram.org/bot{bot_token}/{method}"
|
| 73 |
+
try:
|
| 74 |
+
response = requests.post(url, json=payload)
|
| 75 |
+
if not response.ok:
|
| 76 |
+
logging.error(f"Request to {url} failed with {response.text}")
|
| 77 |
+
return response
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logging.error(f"Error sending request to Telegram: {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
# -------------------------------------------------------------------
|
| 83 |
# Enhanced Logging Setup
|
| 84 |
# -------------------------------------------------------------------
|
|
|
|
| 108 |
# Utility Functions & UI Helpers
|
| 109 |
# -------------------------------------------------------------------
|
| 110 |
def create_html_message(text: str):
|
| 111 |
+
# Wrap text in <pre> tags for monospaced output (HTML parse mode)
|
| 112 |
return {"parse_mode": "HTML", "text": f"<pre>{text}</pre>"}
|
| 113 |
|
| 114 |
def get_inline_keyboard_for_stream():
|
| 115 |
+
# Inline keyboard for streaming controls after the stream has started
|
| 116 |
keyboard = {
|
| 117 |
"inline_keyboard": [
|
| 118 |
[
|
|
|
|
| 187 |
def notify_error(chat_id, error_message):
|
| 188 |
payload = {
|
| 189 |
"chat_id": chat_id,
|
| 190 |
+
"text": f"⚠️ *Streaming Error Occurred:*\n\n{error_message}\n\nPlease check the live logs for details.",
|
| 191 |
"parse_mode": "Markdown"
|
| 192 |
}
|
| 193 |
+
send_telegram_request("sendMessage", payload)
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
# -------------------------------------------------------------------
|
| 196 |
# Live Logging Updater (Background Thread)
|
|
|
|
| 204 |
"text": "<pre>Live Logs:\nInitializing...</pre>",
|
| 205 |
"parse_mode": "HTML"
|
| 206 |
}
|
| 207 |
+
resp = send_telegram_request("sendMessage", payload)
|
| 208 |
+
if resp and resp.ok:
|
| 209 |
live_log_message_id = resp.json()["result"]["message_id"]
|
| 210 |
logging.info(f"Live log message sent with id {live_log_message_id}")
|
| 211 |
else:
|
|
|
|
| 222 |
"text": log_text,
|
| 223 |
"parse_mode": "HTML"
|
| 224 |
}
|
| 225 |
+
send_telegram_request("editMessageText", edit_payload)
|
| 226 |
time.sleep(1)
|
| 227 |
except Exception as e:
|
| 228 |
logging.error(f"Error in live log updater: {e}")
|
|
|
|
| 318 |
user_inputs.setdefault("quality_settings", default_settings["quality_settings"])
|
| 319 |
user_inputs.setdefault("video_codec", default_settings["video_codec"])
|
| 320 |
user_inputs.setdefault("audio_codec", default_settings["audio_codec"])
|
| 321 |
+
# Instead of asking the user to type "start", send an inline button.
|
| 322 |
return {
|
| 323 |
"method": "sendMessage",
|
| 324 |
"chat_id": chat_id,
|
|
|
|
| 417 |
error_message = f"An error occurred during streaming: {str(e)}\n\n{traceback.format_exc()}"
|
| 418 |
logging.error(error_message)
|
| 419 |
streaming_state = "idle"
|
| 420 |
+
# Notify the user about the error using the live logs
|
| 421 |
notify_error(chat_id, error_message)
|
| 422 |
|
| 423 |
def start_streaming(chat_id):
|
|
|
|
| 450 |
live_log_thread.start()
|
| 451 |
logging.info("Live log updater started.")
|
| 452 |
|
| 453 |
+
# Immediately inform the user that streaming has started
|
| 454 |
return {
|
| 455 |
"method": "sendMessage",
|
| 456 |
"chat_id": chat_id,
|