Initial commit

This commit is contained in:
Andre Beging
2025-10-07 12:51:31 +02:00
commit c7f694d820
18 changed files with 1683 additions and 0 deletions

298
app/static/app.js Normal file
View File

@@ -0,0 +1,298 @@
const sessionStatusEl = document.querySelector("#session-status");
const sessionUserEl = document.querySelector("#session-user");
const loginFeedbackEl = document.querySelector("#login-feedback");
const startLoginForm = document.querySelector("#start-login-form");
const verifyLoginForm = document.querySelector("#verify-login-form");
const logoutButton = document.querySelector("#logout-button");
const hooksCountEl = document.querySelector("#hooks-count");
const hooksListEl = document.querySelector("#hooks-list");
const hookTemplate = document.querySelector("#hook-template");
const createHookForm = document.querySelector("#create-hook-form");
const toggleCreateBtn = document.querySelector("#toggle-create");
let createFormVisible = false;
let createFormTouched = false;
function setCreateFormVisibility(show, { fromUser = false } = {}) {
if (!toggleCreateBtn) return;
createFormVisible = show;
if (fromUser) {
createFormTouched = true;
}
createHookForm.classList.toggle("hidden", !show);
toggleCreateBtn.setAttribute("aria-expanded", String(show));
toggleCreateBtn.textContent = show ? "Hide form" : "New Hook";
if (show) {
const chatInput = document.querySelector("#hook-chat-id");
if (chatInput) {
chatInput.focus();
}
}
}
if (toggleCreateBtn) {
toggleCreateBtn.addEventListener("click", () => {
setCreateFormVisibility(!createFormVisible, { fromUser: true });
});
setCreateFormVisibility(false);
}
async function fetchJSON(url, options = {}) {
const response = await fetch(url, {
headers: { "Content-Type": "application/json" },
...options,
});
if (!response.ok) {
const message = await response.text();
throw new Error(message || "Request failed");
}
return response.status === 204 ? null : response.json();
}
function updateSessionUI(status) {
if (status.authorized) {
sessionStatusEl.textContent = "Authorized";
sessionStatusEl.style.background = "rgba(100, 221, 155, 0.12)";
sessionStatusEl.style.color = "#64dd9b";
sessionUserEl.textContent = `Logged in as ${status.user}`;
loginFeedbackEl.textContent = "Session ready. You can trigger hooks.";
startLoginForm.classList.add("hidden");
verifyLoginForm.classList.add("hidden");
logoutButton.classList.remove("hidden");
} else {
sessionStatusEl.textContent = status.code_sent ? "Awaiting code" : "Not authorized";
sessionStatusEl.style.background = "rgba(79, 140, 255, 0.15)";
sessionStatusEl.style.color = "#4f8cff";
sessionUserEl.textContent = status.phone_number
? `Phone number: ${status.phone_number}`
: "Set a phone number to begin";
logoutButton.classList.add("hidden");
if (status.code_sent) {
verifyLoginForm.classList.remove("hidden");
loginFeedbackEl.textContent = "Code sent. Check your Telegram messages.";
} else {
verifyLoginForm.classList.add("hidden");
loginFeedbackEl.textContent = "Start by sending a login code to your phone.";
}
startLoginForm.classList.remove("hidden");
}
}
async function refreshAll() {
try {
const status = await fetchJSON("/api/status");
updateSessionUI(status);
} catch (error) {
loginFeedbackEl.textContent = `Failed to refresh status: ${error.message}`;
}
await loadHooks();
}
async function loadHooks() {
try {
const hooks = await fetchJSON("/api/hooks");
hooksCountEl.textContent = hooks.length;
hooksListEl.innerHTML = "";
if (!hooks.length) {
if (!createFormTouched) {
setCreateFormVisibility(true);
}
const empty = document.createElement("p");
empty.className = "feedback";
empty.textContent = "No hooks yet. Use the New Hook button above to create one.";
hooksListEl.appendChild(empty);
return;
}
hooks.forEach((hook) => {
const node = hookTemplate.content.cloneNode(true);
node.querySelector("h3").textContent = hook.chat_id;
node.querySelector(".hook-date").textContent = new Date(hook.created_at).toLocaleString();
node.querySelector(".hook-message").textContent = hook.message;
node.querySelector(".hook-url").textContent = hook.action_url;
node.querySelector(".hook-id").textContent = hook.hook_id;
const feedbackEl = node.querySelector(".hook-feedback");
const copyBtn = node.querySelector(".copy");
copyBtn.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(hook.action_url);
copyBtn.textContent = "Copied!";
setTimeout(() => {
copyBtn.textContent = "Copy URL";
}, 2000);
} catch (err) {
copyBtn.textContent = "Copy failed";
setTimeout(() => {
copyBtn.textContent = "Copy URL";
}, 2000);
}
});
const triggerBtn = node.querySelector(".trigger");
triggerBtn.addEventListener("click", async () => {
const originalText = triggerBtn.textContent;
triggerBtn.disabled = true;
triggerBtn.textContent = "Sending…";
feedbackEl.textContent = "";
try {
const result = await fetchJSON(`/action/${hook.hook_id}`);
triggerBtn.textContent = "Sent";
feedbackEl.textContent = `Status: ${result.status}`;
feedbackEl.style.color = "#64dd9b";
} catch (err) {
triggerBtn.textContent = "Retry";
feedbackEl.textContent = `Failed: ${err.message}`;
feedbackEl.style.color = "#ffbac7";
} finally {
setTimeout(() => {
triggerBtn.textContent = originalText;
triggerBtn.disabled = false;
feedbackEl.style.color = "";
}, 2000);
}
});
const editIdBtn = node.querySelector(".edit-id");
const editIconMarkup = editIdBtn.innerHTML;
editIdBtn.addEventListener("click", async () => {
const newId = prompt("Enter new hook ID", hook.hook_id);
if (newId === null) {
return;
}
const sanitized = newId.trim();
if (!sanitized || sanitized === hook.hook_id) {
return;
}
editIdBtn.disabled = true;
editIdBtn.textContent = "Saving…";
feedbackEl.textContent = "";
try {
await fetchJSON(`/api/hooks/${hook.hook_id}`, {
method: "PATCH",
body: JSON.stringify({ hook_id: sanitized }),
});
feedbackEl.textContent = "Hook ID updated.";
feedbackEl.style.color = "#64dd9b";
await loadHooks();
} catch (err) {
feedbackEl.textContent = `Update failed: ${err.message}`;
feedbackEl.style.color = "#ffbac7";
} finally {
editIdBtn.disabled = false;
editIdBtn.innerHTML = editIconMarkup;
setTimeout(() => {
feedbackEl.textContent = "";
feedbackEl.style.color = "";
}, 2500);
}
});
const deleteBtn = node.querySelector(".delete");
deleteBtn.addEventListener("click", async () => {
if (!confirm("Delete this hook?")) return;
try {
await fetchJSON(`/api/hooks/${hook.hook_id}`, { method: "DELETE" });
await loadHooks();
} catch (err) {
alert(`Failed to delete hook: ${err.message}`);
}
});
hooksListEl.appendChild(node);
});
} catch (error) {
hooksListEl.innerHTML = "";
const para = document.createElement("p");
para.className = "feedback";
para.textContent = `Failed to load hooks: ${error.message}`;
hooksListEl.appendChild(para);
}
}
startLoginForm.addEventListener("submit", async (event) => {
event.preventDefault();
const phoneNumber = document.querySelector("#phone-number").value || null;
loginFeedbackEl.textContent = "Sending code…";
try {
const status = await fetchJSON("/api/login/start", {
method: "POST",
body: JSON.stringify({ phone_number: phoneNumber }),
});
updateSessionUI(status);
loginFeedbackEl.textContent = "Code sent successfully.";
} catch (error) {
loginFeedbackEl.textContent = `Failed to send code: ${error.message}`;
}
});
verifyLoginForm.addEventListener("submit", async (event) => {
event.preventDefault();
const code = document.querySelector("#verification-code").value;
const password = document.querySelector("#twofactor-password").value || null;
loginFeedbackEl.textContent = "Verifying…";
try {
const status = await fetchJSON("/api/login/verify", {
method: "POST",
body: JSON.stringify({ code, password }),
});
updateSessionUI(status);
loginFeedbackEl.textContent = status.authorized
? "Login completed."
: "Enter your password to finish.";
} catch (error) {
loginFeedbackEl.textContent = `Verification failed: ${error.message}`;
}
});
logoutButton.addEventListener("click", async () => {
try {
const status = await fetchJSON("/api/logout", { method: "POST" });
updateSessionUI(status);
loginFeedbackEl.textContent = "Logged out.";
} catch (error) {
loginFeedbackEl.textContent = `Logout failed: ${error.message}`;
}
});
createHookForm.addEventListener("submit", async (event) => {
event.preventDefault();
const chatId = document.querySelector("#hook-chat-id").value.trim();
const message = document.querySelector("#hook-message").value.trim();
if (!chatId || !message) {
alert("Chat ID and message are required.");
return;
}
const submitBtn = createHookForm.querySelector("button[type='submit']");
const originalText = submitBtn.textContent;
submitBtn.disabled = true;
submitBtn.textContent = "Creating…";
try {
await fetchJSON("/api/hooks", {
method: "POST",
body: JSON.stringify({ chat_id: chatId, message }),
});
createHookForm.reset();
await loadHooks();
} catch (error) {
alert(`Failed to create hook: ${error.message}`);
} finally {
submitBtn.disabled = false;
submitBtn.textContent = originalText;
}
});
window.addEventListener("DOMContentLoaded", async () => {
await refreshAll();
try {
const status = await fetchJSON("/api/status");
const phoneInput = document.querySelector("#phone-number");
if (status.phone_number && !phoneInput.value) {
phoneInput.value = status.phone_number;
}
} catch (err) {
console.warn("Unable to preload phone number", err);
}
});

303
app/static/style.css Normal file
View File

@@ -0,0 +1,303 @@
:root {
color-scheme: light dark;
--bg: #10131a;
--surface: rgba(255, 255, 255, 0.06);
--surface-light: rgba(10, 14, 25, 0.5);
--text: #f4f6fb;
--muted: #adb5d6;
--accent: #4f8cff;
--accent-dark: #3f6ed6;
--danger: #ff5c7a;
--font-base: "Inter", "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
--border-radius: 18px;
--shadow: 0 18px 40px rgba(15, 20, 30, 0.35);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: var(--font-base);
background: radial-gradient(circle at top, #192040, #0a0d18);
color: var(--text);
display: grid;
grid-template-rows: auto 1fr auto;
}
header,
footer {
padding: 2rem clamp(1rem, 6vw, 4rem);
}
header h1 {
margin: 0;
font-size: clamp(2rem, 5vw, 3.5rem);
letter-spacing: -0.04em;
}
.subtitle {
margin-top: 0.5rem;
color: var(--muted);
max-width: 40rem;
}
main {
display: grid;
gap: 2rem;
padding: 0 clamp(1rem, 6vw, 4rem) 4rem;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}
.card {
background: linear-gradient(145deg, rgba(23, 30, 50, 0.92), rgba(10, 12, 22, 0.9));
border-radius: var(--border-radius);
padding: clamp(1.5rem, 3vw, 2rem);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
border: 1px solid rgba(255, 255, 255, 0.06);
}
.section-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1.5rem;
}
.header-actions {
display: inline-flex;
align-items: center;
gap: 0.75rem;
flex-wrap: wrap;
justify-content: flex-end;
}
.status-pill {
display: inline-flex;
align-items: center;
padding: 0.35rem 0.9rem;
border-radius: 999px;
background: rgba(79, 140, 255, 0.15);
color: var(--accent);
font-weight: 600;
margin-bottom: 0.75rem;
}
.user-info {
margin-bottom: 1.5rem;
color: var(--muted);
}
.form-grid {
display: grid;
gap: 0.75rem;
margin-bottom: 1.5rem;
}
label {
font-size: 0.9rem;
color: var(--muted);
}
input,
textarea,
button {
font-family: inherit;
font-size: 1rem;
border-radius: 12px;
}
input,
textarea {
border: 1px solid rgba(255, 255, 255, 0.08);
background: rgba(9, 13, 24, 0.65);
color: var(--text);
padding: 0.75rem 1rem;
transition: border 0.2s ease, background 0.2s ease;
}
input:focus,
textarea:focus {
outline: none;
border-color: rgba(79, 140, 255, 0.5);
background: rgba(15, 20, 35, 0.85);
}
textarea {
resize: vertical;
min-height: 150px;
}
button {
padding: 0.8rem 1.2rem;
border: none;
cursor: pointer;
font-weight: 600;
transition: transform 0.18s ease, box-shadow 0.18s ease;
}
button:active {
transform: translateY(2px);
}
.icon-button {
padding: 0.6rem;
min-width: 0;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.25rem;
}
.icon {
width: 1rem;
height: 1rem;
display: block;
fill: currentColor;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.primary {
background: linear-gradient(135deg, var(--accent), var(--accent-dark));
color: white;
box-shadow: 0 14px 30px rgba(79, 140, 255, 0.35);
}
.secondary {
background: rgba(255, 255, 255, 0.08);
color: var(--text);
border: 1px solid rgba(255, 255, 255, 0.06);
}
.danger {
background: rgba(255, 92, 122, 0.2);
color: #ffbac7;
border: 1px solid rgba(255, 92, 122, 0.4);
}
.badge {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
padding: 0.35rem 0.65rem;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.feedback {
min-height: 1.25rem;
color: var(--muted);
}
.hook-list {
display: grid;
gap: 1rem;
}
.hook-card {
padding: 1rem 1.25rem;
background: rgba(10, 13, 24, 0.75);
border-radius: 16px;
border: 1px solid rgba(255, 255, 255, 0.04);
display: grid;
gap: 0.75rem;
}
.hook-id-row {
display: flex;
align-items: center;
gap: 0.5rem;
flex-wrap: wrap;
}
.hook-id-label {
font-size: 0.85rem;
color: var(--muted);
}
.hook-id {
padding: 0.3rem 0.6rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
}
.hook-meta {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 1rem;
}
.hook-meta h3 {
margin: 0;
font-size: 1rem;
color: var(--text);
}
.hook-date {
font-size: 0.85rem;
color: var(--muted);
}
.hook-message {
white-space: pre-wrap;
font-family: "JetBrains Mono", "Fira Code", monospace;
background: rgba(255, 255, 255, 0.04);
padding: 0.75rem;
border-radius: 12px;
margin: 0;
}
.hook-url {
background: rgba(255, 255, 255, 0.04);
border-radius: 12px;
padding: 0.6rem 0.8rem;
display: inline-block;
user-select: all;
}
.hook-feedback {
margin: 0.35rem 0 0;
min-height: 1rem;
font-size: 0.85rem;
color: var(--muted);
}
.hook-actions {
display: flex;
gap: 0.75rem;
}
.hidden {
display: none !important;
}
.mono {
font-family: "JetBrains Mono", "Fira Code", monospace;
}
@media (max-width: 640px) {
header,
footer {
text-align: center;
}
.hook-actions {
flex-direction: column;
}
}