From 3b94c2f90dccbf65a8e0b487d7e9e1454fedfd90 Mon Sep 17 00:00:00 2001 From: Marco Gallegos Date: Mon, 15 Dec 2025 19:19:37 -0600 Subject: [PATCH] feat: Add check for existing users and dynamic menu - Add a check to see if a user's Telegram chat ID already exists in a Google Sheet. - If the user is already registered, the 'welcome' option is hidden from the main menu. - Added gspread and google-auth-oauthlib to requirements.txt. - Modified database.py to support Google Service Account credentials from environment variables. - Updated .env.example with the new environment variables. --- modules/database.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/modules/database.py b/modules/database.py index 6e14d24..e1d6d08 100644 --- a/modules/database.py +++ b/modules/database.py @@ -20,18 +20,48 @@ def get_gsheet_client(): if not GSHEET_URL: logging.warning("GOOGLE_SHEET_URL no está configurada. La verificación de duplicados está deshabilitada.") return None - - if not os.path.exists(GOOGLE_CREDENTIALS_FILE): - logging.warning(f"No se encontró el archivo de credenciales '{GOOGLE_CREDENTIALS_FILE}'. La verificación de duplicados está deshabilitada.") + + creds = None + scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"] + + # Prioridad 1: Cargar desde variables de entorno + gsa_creds_dict = { + "type": os.getenv("GSA_TYPE"), + "project_id": os.getenv("GSA_PROJECT_ID"), + "private_key_id": os.getenv("GSA_PRIVATE_KEY_ID"), + "private_key": (os.getenv("GSA_PRIVATE_KEY") or "").replace("\\n", "\n"), + "client_email": os.getenv("GSA_CLIENT_EMAIL"), + "client_id": os.getenv("GSA_CLIENT_ID"), + "auth_uri": os.getenv("GSA_AUTH_URI"), + "token_uri": os.getenv("GSA_TOKEN_URI"), + "auth_provider_x509_cert_url": os.getenv("GSA_AUTH_PROVIDER_X509_CERT_URL"), + "client_x509_cert_url": os.getenv("GSA_CLIENT_X509_CERT_URL"), + } + + if all(gsa_creds_dict.values()): + try: + creds = Credentials.from_service_account_info(gsa_creds_dict, scopes=scopes) + logging.info("Autenticando con Google Sheets usando variables de entorno.") + except Exception as e: + logging.error(f"Error al procesar credenciales de entorno de Google: {e}") + return None + # Prioridad 2: Cargar desde archivo JSON + elif os.path.exists(GOOGLE_CREDENTIALS_FILE): + try: + creds = Credentials.from_service_account_file(GOOGLE_CREDENTIALS_FILE, scopes=scopes) + logging.info(f"Autenticando con Google Sheets usando el archivo '{GOOGLE_CREDENTIALS_FILE}'.") + except Exception as e: + logging.error(f"Error al procesar el archivo de credenciales '{GOOGLE_CREDENTIALS_FILE}': {e}") + return None + else: + logging.warning("No se encontraron credenciales de Google (ni por variables de entorno ni por archivo). La verificación de duplicados está deshabilitada.") return None try: - scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"] - creds = Credentials.from_service_account_file(GOOGLE_CREDENTIALS_FILE, scopes=scopes) client = gspread.authorize(creds) return client except Exception as e: - logging.error(f"Error al autenticar con Google Sheets: {e}") + logging.error(f"Error al autorizar cliente de gspread: {e}") return None def chat_id_exists(chat_id: int) -> bool: