import os import time import asyncio from typing import Dict, Any from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import WebDriverException, TimeoutException from selenium_UnitedUcard_eligibilityCheckWorker import AutomationUnitedUcardEligibilityCheck # In-memory session store sessions: Dict[str, Dict[str, Any]] = {} SESSION_OTP_TIMEOUT = int(os.getenv("SESSION_OTP_TIMEOUT", "120")) # seconds def make_session_entry() -> str: """Create a new session entry and return its ID.""" import uuid sid = str(uuid.uuid4()) sessions[sid] = { "status": "created", # created -> running -> waiting_for_otp -> otp_submitted -> completed / error "created_at": time.time(), "last_activity": time.time(), "bot": None, # worker instance "driver": None, # selenium webdriver "otp_event": asyncio.Event(), "otp_value": None, "result": None, "message": None, "type": None, } return sid async def cleanup_session(sid: str, message: str | None = None): """ Close driver (if any), wake OTP waiter, set final state, and remove session entry. Idempotent: safe to call multiple times. """ s = sessions.get(sid) if not s: return try: try: if s.get("status") not in ("completed", "error", "not_found"): s["status"] = "error" if message: s["message"] = message except Exception: pass try: ev = s.get("otp_event") if ev and not ev.is_set(): ev.set() except Exception: pass # NOTE: Do NOT quit driver - keep browser alive for next patient # Browser manager handles the persistent browser instance finally: sessions.pop(sid, None) async def _remove_session_later(sid: str, delay: int = 20): await asyncio.sleep(delay) await cleanup_session(sid) def _minimize_browser(bot): """Hide the browser window so it doesn't stay in the user's way.""" try: if bot and bot.driver: try: bot.driver.get("about:blank") except Exception: pass try: bot.driver.minimize_window() print("[UnitedUcard] Browser minimized after error") return except Exception: pass try: bot.driver.set_window_position(-10000, -10000) print("[UnitedUcard] Browser moved off-screen after error") except Exception: pass except Exception as e: print(f"[UnitedUcard] Could not hide browser: {e}") async def start_uniteducard_run(sid: str, data: dict, url: str): """ Run the United Ucard workflow for a session (WITHOUT managing semaphore/counters). Called by agent.py inside a wrapper that handles queue/counters. """ s = sessions.get(sid) if not s: return {"status": "error", "message": "session not found"} s["status"] = "running" s["last_activity"] = time.time() try: bot = AutomationUnitedUcardEligibilityCheck({"data": data}) bot.config_driver() s["bot"] = bot s["driver"] = bot.driver s["last_activity"] = time.time() # Navigate to login URL try: if not url: raise ValueError("URL not provided for United Ucard run") bot.driver.maximize_window() bot.driver.get(url) await asyncio.sleep(1) except Exception as e: s["status"] = "error" s["message"] = f"Navigation failed: {e}" await cleanup_session(sid) return {"status": "error", "message": s["message"]} # Login try: login_result = bot.login(url) except WebDriverException as wde: s["status"] = "error" s["message"] = f"Selenium driver error during login: {wde}" await cleanup_session(sid, s["message"]) return {"status": "error", "message": s["message"]} except Exception as e: s["status"] = "error" s["message"] = f"Unexpected error during login: {e}" await cleanup_session(sid, s["message"]) return {"status": "error", "message": s["message"]} # Already logged in - session persisted from profile, skip to step1 if isinstance(login_result, str) and login_result == "ALREADY_LOGGED_IN": s["status"] = "running" s["message"] = "Session persisted" print("[start_uniteducard_run] Session persisted - skipping OTP") # OTP required path - POLL THE BROWSER to detect when user enters OTP elif isinstance(login_result, str) and login_result == "OTP_REQUIRED": s["status"] = "waiting_for_otp" s["message"] = "OTP required for login - please enter OTP in browser" s["last_activity"] = time.time() driver = s["driver"] max_polls = SESSION_OTP_TIMEOUT login_success = False print(f"[UnitedUcard OTP] Waiting for user to enter OTP (polling browser for {SESSION_OTP_TIMEOUT}s)...") for poll in range(max_polls): await asyncio.sleep(1) s["last_activity"] = time.time() try: # Check if OTP was submitted via API (from app) otp_value = s.get("otp_value") if otp_value: print(f"[UnitedUcard OTP] OTP received from app: {otp_value}") try: # Confirmed against live site (2026-09-13): #/rba/options/text page # # otp_input = driver.find_element(By.XPATH, "//input[@id='otpBox' or contains(@id,'otp') or contains(@name,'otp') or " "contains(@name,'code') or @type='tel' or contains(@aria-label,'Verification') or " "contains(@placeholder,'code')]" ) otp_input.clear() otp_input.send_keys(otp_value) # Check "Remember this device" so future logins skip OTP. # Confirmed against live site (2026-09-15): # try: remember_checkbox = driver.find_element(By.CSS_SELECTOR, "[data-cy='data-checkbox-rememberDevice']") if remember_checkbox.get_attribute("aria-checked") != "true": driver.execute_script( "arguments[0].scrollIntoView({block:'center'});", remember_checkbox) remember_checkbox.click() print("[UnitedUcard OTP] Checked 'Remember this device'") except Exception as remember_err: print(f"[UnitedUcard OTP] Could not check 'Remember this device': {remember_err}") try: # Try the confirmed id first — the broader text/type # fallback below can match the WRONG button if more # than one element on the page qualifies (find_element # returns the first DOM match, not necessarily the # right one), which was landing on a blank page. try: verify_btn = driver.find_element(By.ID, "continuebtn") except Exception: verify_btn = driver.find_element(By.XPATH, "//button[@type='submit' or contains(text(),'Verify') or " "contains(text(),'Submit') or contains(text(),'Continue')]" ) driver.execute_script("arguments[0].scrollIntoView({block:'center'});", verify_btn) verify_btn.click() print("[UnitedUcard OTP] Clicked verify button") except Exception: otp_input.send_keys("\n") # Press Enter as fallback print("[UnitedUcard OTP] Pressed Enter as fallback") print("[UnitedUcard OTP] OTP typed and submitted via app") s["otp_value"] = None # Clear so we don't submit again await asyncio.sleep(3) # Wait for verification except Exception as type_err: print(f"[UnitedUcard OTP] Failed to type OTP from app: {type_err}") # Check current URL - success lands on the UHC Dental provider dashboard # (secure.uhcdental.com/.../postloginhomescreen.html) current_url = driver.current_url.lower() print(f"[UnitedUcard OTP Poll {poll+1}/{max_polls}] URL: {current_url[:60]}...") if "secure.uhcdental.com" in current_url: print("[UnitedUcard OTP] Reached UHC Dental dashboard - login successful!") login_success = True break # Also check if OTP input is still visible try: driver.find_element(By.XPATH, "//input[@id='otpBox' or contains(@id,'otp') or contains(@name,'otp') or " "contains(@name,'code') or @type='tel' or contains(@aria-label,'Verification') or " "contains(@placeholder,'code') or contains(@placeholder,'Code')]" ) print(f"[UnitedUcard OTP Poll {poll+1}] OTP input still visible - waiting...") except Exception: print(f"[UnitedUcard OTP Poll {poll+1}] OTP input not found, continuing to poll...") except Exception as poll_err: print(f"[UnitedUcard OTP Poll {poll+1}] Error: {poll_err}") if not login_success: s["status"] = "error" s["message"] = "OTP timeout - login not completed" await cleanup_session(sid) return {"status": "error", "message": "OTP not completed in time"} if login_success: s["status"] = "running" s["message"] = "Login successful after OTP" print("[UnitedUcard OTP] Proceeding to step1...") elif isinstance(login_result, str) and login_result.startswith("ERROR"): s["status"] = "error" s["message"] = login_result await cleanup_session(sid) return {"status": "error", "message": login_result} # Login succeeded without OTP (SUCCESS) elif isinstance(login_result, str) and login_result == "SUCCESS": print("[start_uniteducard_run] Login succeeded without OTP") s["status"] = "running" s["message"] = "Login succeeded" # Step 1 step1_result = bot.step1() if isinstance(step1_result, str) and step1_result.startswith("ERROR"): s["status"] = "error" s["message"] = step1_result s["result"] = {"status": "error", "message": step1_result} _minimize_browser(bot) asyncio.create_task(_remove_session_later(sid, 30)) return {"status": "error", "message": step1_result} # Step 2 (PDF) step2_result = bot.step2() if isinstance(step2_result, dict) and step2_result.get("status") == "success": s["status"] = "completed" s["result"] = step2_result s["message"] = "completed" asyncio.create_task(_remove_session_later(sid, 30)) return step2_result else: s["status"] = "error" if isinstance(step2_result, dict): s["message"] = step2_result.get("message", "unknown error") else: s["message"] = str(step2_result) s["result"] = {"status": "error", "message": s["message"]} _minimize_browser(bot) asyncio.create_task(_remove_session_later(sid, 30)) return {"status": "error", "message": s["message"]} except Exception as e: s["status"] = "error" s["message"] = f"worker exception: {e}" try: if bot and bot.driver: bot.driver.minimize_window() except Exception: pass s["result"] = {"status": "error", "message": s["message"]} asyncio.create_task(_remove_session_later(sid, 30)) return {"status": "error", "message": s["message"]} def submit_otp(sid: str, otp: str) -> Dict[str, Any]: """Set OTP for a session and wake waiting runner.""" s = sessions.get(sid) if not s: return {"status": "error", "message": "session not found"} if s.get("status") != "waiting_for_otp": return {"status": "error", "message": f"session not waiting for otp (state={s.get('status')})"} s["otp_value"] = otp s["last_activity"] = time.time() try: s["otp_event"].set() except Exception: pass return {"status": "ok", "message": "otp accepted"} def get_session_status(sid: str) -> Dict[str, Any]: s = sessions.get(sid) if not s: return {"status": "not_found"} return { "session_id": sid, "status": s.get("status"), "message": s.get("message"), "created_at": s.get("created_at"), "last_activity": s.get("last_activity"), "result": s.get("result") if s.get("status") in ("completed", "error") else None, }