Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# main.py (
|
2 |
import os
|
3 |
import re
|
4 |
import logging
|
@@ -61,10 +61,17 @@ ptb_app: Optional[Application] = None
|
|
61 |
# --- Environment Variable Loading & Configuration ---
|
62 |
logger.info("Attempting to load secrets and configuration...")
|
63 |
def get_secret(secret_name):
|
|
|
64 |
value = os.environ.get(secret_name)
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
return value
|
69 |
|
70 |
TELEGRAM_TOKEN = get_secret('TELEGRAM_TOKEN')
|
@@ -72,17 +79,20 @@ OPENROUTER_API_KEY = get_secret('OPENROUTER_API_KEY')
|
|
72 |
URLTOTEXT_API_KEY = get_secret('URLTOTEXT_API_KEY')
|
73 |
SUPADATA_API_KEY = get_secret('SUPADATA_API_KEY')
|
74 |
APIFY_API_TOKEN = get_secret('APIFY_API_TOKEN')
|
75 |
-
WEBHOOK_SECRET = get_secret('WEBHOOK_SECRET')
|
76 |
|
77 |
OPENROUTER_MODEL = os.environ.get("OPENROUTER_MODEL", "deepseek/deepseek-chat-v3-0324:free")
|
78 |
APIFY_ACTOR_ID = os.environ.get("APIFY_ACTOR_ID", "karamelo/youtube-transcripts")
|
79 |
|
|
|
80 |
if not TELEGRAM_TOKEN: logger.critical("β FATAL: TELEGRAM_TOKEN not found."); raise RuntimeError("Exiting: Telegram token missing.")
|
81 |
if not OPENROUTER_API_KEY: logger.error("β ERROR: OPENROUTER_API_KEY not found. Summarization will fail.")
|
82 |
|
83 |
-
|
84 |
-
if not
|
85 |
-
if not
|
|
|
|
|
86 |
|
87 |
logger.info("Secret loading and configuration check finished.")
|
88 |
logger.info(f"Using OpenRouter Model: {OPENROUTER_MODEL}")
|
@@ -426,23 +436,16 @@ async def handle_summary_type_callback(update: Update, context: ContextTypes.DEF
|
|
426 |
context.user_data.pop('url_to_summarize', None); context.user_data.pop('original_message_id', None); logger.debug(f"Cleared URL context for user {user.id}")
|
427 |
|
428 |
global TELEGRAM_TOKEN, OPENROUTER_API_KEY
|
429 |
-
# --- FIX: Correct Syntax for key checks ---
|
430 |
if not TELEGRAM_TOKEN:
|
431 |
logger.critical("TG TOKEN missing!")
|
432 |
-
try:
|
433 |
-
|
434 |
-
|
435 |
-
pass
|
436 |
-
return # Stop processing
|
437 |
-
|
438 |
if not OPENROUTER_API_KEY:
|
439 |
logger.error("OpenRouter key missing!")
|
440 |
-
try:
|
441 |
-
|
442 |
-
|
443 |
-
pass
|
444 |
-
return # Stop processing
|
445 |
-
# --- End FIX ---
|
446 |
|
447 |
logger.info(f"Scheduling task for user {user.id}, chat {query.message.chat_id}, msg {message_id_to_edit}")
|
448 |
asyncio.create_task( process_summary_task( user_id=user.id, chat_id=query.message.chat_id, message_id_to_edit=message_id_to_edit, url=url, summary_type=summary_type, bot_token=TELEGRAM_TOKEN ), name=f"SummaryTask-{user.id}-{message_id_to_edit}" )
|
|
|
1 |
+
# main.py (Fixing get_secret for optional secrets)
|
2 |
import os
|
3 |
import re
|
4 |
import logging
|
|
|
61 |
# --- Environment Variable Loading & Configuration ---
|
62 |
logger.info("Attempting to load secrets and configuration...")
|
63 |
def get_secret(secret_name):
|
64 |
+
"""Gets a secret from environment variables and logs status safely."""
|
65 |
value = os.environ.get(secret_name)
|
66 |
+
if value:
|
67 |
+
status = "Found"
|
68 |
+
# Safely log the beginning of the value
|
69 |
+
log_length = min(len(value), 8)
|
70 |
+
value_start = value[:log_length]
|
71 |
+
logger.info(f"Secret '{secret_name}': {status} (Value starts with: {value_start}...)")
|
72 |
+
else:
|
73 |
+
status = "Not Found"
|
74 |
+
logger.warning(f"Secret '{secret_name}': {status}") # Changed to warning for not found
|
75 |
return value
|
76 |
|
77 |
TELEGRAM_TOKEN = get_secret('TELEGRAM_TOKEN')
|
|
|
79 |
URLTOTEXT_API_KEY = get_secret('URLTOTEXT_API_KEY')
|
80 |
SUPADATA_API_KEY = get_secret('SUPADATA_API_KEY')
|
81 |
APIFY_API_TOKEN = get_secret('APIFY_API_TOKEN')
|
82 |
+
WEBHOOK_SECRET = get_secret('WEBHOOK_SECRET') # This will now correctly return None if not set, without erroring
|
83 |
|
84 |
OPENROUTER_MODEL = os.environ.get("OPENROUTER_MODEL", "deepseek/deepseek-chat-v3-0324:free")
|
85 |
APIFY_ACTOR_ID = os.environ.get("APIFY_ACTOR_ID", "karamelo/youtube-transcripts")
|
86 |
|
87 |
+
# Critical checks remain the same
|
88 |
if not TELEGRAM_TOKEN: logger.critical("β FATAL: TELEGRAM_TOKEN not found."); raise RuntimeError("Exiting: Telegram token missing.")
|
89 |
if not OPENROUTER_API_KEY: logger.error("β ERROR: OPENROUTER_API_KEY not found. Summarization will fail.")
|
90 |
|
91 |
+
# Optional checks remain the same (logging handled by get_secret)
|
92 |
+
if not URLTOTEXT_API_KEY: pass # Logged as warning by get_secret
|
93 |
+
if not SUPADATA_API_KEY: pass # Logged as warning by get_secret
|
94 |
+
if not APIFY_API_TOKEN: pass # Logged as warning by get_secret
|
95 |
+
if not WEBHOOK_SECRET: logger.info("Optional secret 'WEBHOOK_SECRET' not found. Webhook security disabled.") # Explicit info log
|
96 |
|
97 |
logger.info("Secret loading and configuration check finished.")
|
98 |
logger.info(f"Using OpenRouter Model: {OPENROUTER_MODEL}")
|
|
|
436 |
context.user_data.pop('url_to_summarize', None); context.user_data.pop('original_message_id', None); logger.debug(f"Cleared URL context for user {user.id}")
|
437 |
|
438 |
global TELEGRAM_TOKEN, OPENROUTER_API_KEY
|
|
|
439 |
if not TELEGRAM_TOKEN:
|
440 |
logger.critical("TG TOKEN missing!")
|
441 |
+
try: await query.edit_message_text(text="β Bot config error.")
|
442 |
+
except Exception: pass
|
443 |
+
return
|
|
|
|
|
|
|
444 |
if not OPENROUTER_API_KEY:
|
445 |
logger.error("OpenRouter key missing!")
|
446 |
+
try: await query.edit_message_text(text="β AI config error.")
|
447 |
+
except Exception: pass
|
448 |
+
return
|
|
|
|
|
|
|
449 |
|
450 |
logger.info(f"Scheduling task for user {user.id}, chat {query.message.chat_id}, msg {message_id_to_edit}")
|
451 |
asyncio.create_task( process_summary_task( user_id=user.id, chat_id=query.message.chat_id, message_id_to_edit=message_id_to_edit, url=url, summary_type=summary_type, bot_token=TELEGRAM_TOKEN ), name=f"SummaryTask-{user.id}-{message_id_to_edit}" )
|