File size: 2,900 Bytes
80d9185
ebd8c9c
9fba82d
 
 
62a90ad
 
 
 
9fba82d
62a90ad
 
 
9fba82d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62a90ad
 
9fba82d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebd8c9c
9fba82d
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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}")