import os import streamlit as st import requests import pandas as pd API_BASE_URL = os.getenv("BRAIN_API_BASE_URL") PROFILES_URL = f"{API_BASE_URL}/api/v1/callbacks/profiles" SHEETS_ROWS_URL = f"{API_BASE_URL}/api/v1/sheets/rows" TRIGGER_URL_TMPL = f"{API_BASE_URL}/api/v1/callbacks/profile/{{}}" BRAIN_API_TOKEN = os.getenv("BRAIN_API_TOKEN") APP_USERNAME = os.getenv("USERNAME") APP_PASSWORD = os.getenv("PASSWORD") HEADERS = { "Authorization": f"Bearer {BRAIN_API_TOKEN}", "Content-Type": "application/json", } st.set_page_config(page_title="Callback Profiles Dashboard", layout="wide") if "logged_in" not in st.session_state: st.session_state.logged_in = False def show_login(): with st.form("login_form"): st.subheader("🔒 Login") user = st.text_input("Username") pwd = st.text_input("Password", type="password") submit = st.form_submit_button("Login") if submit: if user == APP_USERNAME and pwd == APP_PASSWORD: st.session_state.logged_in = True st.success("Logged in ✅") else: st.error("Invalid credentials") if not st.session_state.logged_in: show_login() st.stop() st.sidebar.header("Select a Callback Profile") try: resp = requests.get(PROFILES_URL, headers=HEADERS, timeout=10) resp.raise_for_status() profiles = resp.json() except Exception as e: st.sidebar.error(f"Failed to load profiles: {e}") st.stop() profile_map = {p["key"]: p for p in profiles} selected_key = st.sidebar.selectbox("Profile", [""] + list(profile_map.keys())) if not selected_key: st.sidebar.info("Please select a profile") st.stop() profile = profile_map[selected_key] st.header(f"Profile: `{selected_key}`") cols = st.columns(2) with cols[0]: st.markdown("**Assistant ID**") st.text(profile["assistant_id"]) st.markdown("**Adapter Type**") st.text(profile["adapter_type"]) with cols[1]: st.markdown("**Phone Number ID**") st.text(profile["phone_number_id"]) st.markdown("---") st.subheader("📑 Current Google Sheet Rows") try: r = requests.get(SHEETS_ROWS_URL, headers=HEADERS, timeout=10) r.raise_for_status() data = r.json().get("rows", []) if len(data) >= 2: df = pd.DataFrame(data[1:], columns=data[0]) st.dataframe(df, use_container_width=True) else: st.info("No rows found in the sheet.") except Exception as e: st.error(f"Could not fetch sheet rows: {e}") st.markdown("---") trigger_url = TRIGGER_URL_TMPL.format(selected_key) if st.button("Trigger call", disabled=not selected_key): try: t = requests.post(trigger_url, headers=HEADERS, timeout=10) t.raise_for_status() st.success("✅ Call(s) triggered!") st.json(t.json()) except Exception as e: st.error(f"Failed to trigger: {e}")