Spaces:
Sleeping
Sleeping
File size: 2,825 Bytes
8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 f6d6416 8492c62 e3bce90 f6d6416 8492c62 f6d6416 e3bce90 8492c62 f6d6416 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 8492c62 e3bce90 f6d6416 e3bce90 a5cfa35 e3bce90 8492c62 f6d6416 8492c62 e3bce90 f6d6416 8492c62 e3bce90 8492c62 7552da4 f6d6416 8492c62 e3bce90 8492c62 f6d6416 e3bce90 |
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 94 95 96 97 98 |
const chatForm = document.getElementById("chat-form");
const userInput = document.getElementById("user-input");
const chatBox = document.getElementById("chat-box");
const orderIdEl = document.getElementById("order_id");
const categoryEl = document.getElementById("category");
const sentimentEl = document.getElementById("sentiment");
const negativeBox = document.getElementById("negative-box");
let sentimentChart = null;
// π§ Append User Message
function appendUser(text) {
chatBox.innerHTML += `<p class="user-msg">π§ You: ${text}</p>`;
chatBox.scrollTop = chatBox.scrollHeight;
}
// π€ Append Bot Message
function appendBot(text) {
chatBox.innerHTML += `<p class="bot-msg">π€ Bot: ${text}</p>`;
chatBox.scrollTop = chatBox.scrollHeight;
}
// β Send chat message
chatForm.addEventListener("submit", async (e) => {
e.preventDefault();
const text = userInput.value.trim();
const order_id = orderIdEl.value.trim();
if (!text) return;
appendUser(text);
userInput.value = "";
try {
const res = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ user_input: text, order_id })
});
const data = await res.json();
if (data.error) {
appendBot("Sorry, could not process your message.");
return;
}
appendBot(data.reply);
categoryEl.textContent = data.category || "β";
sentimentEl.textContent = `${data.sentiment || "β"} (${data.confidence || 0}%)`;
await updateAnalytics();
await updateNegatives();
} catch (err) {
console.error(err);
appendBot("Network error β try again.");
}
});
// π Update Analytics Charts
async function updateAnalytics() {
const res = await fetch("/analytics");
const data = await res.json();
const ctx = document.getElementById("sentimentChart");
if (sentimentChart) sentimentChart.destroy();
sentimentChart = new Chart(ctx, {
type: "pie",
data: {
labels: data.labels,
datasets: [{
data: data.values,
backgroundColor: ["#2ecc71", "#e74c3c", "#f1c40f"]
}]
}
});
}
// β οΈ Update Negative Messages Box
async function updateNegatives() {
if (!negativeBox) return; // Safety check
const res = await fetch("/admin/negatives-data");
const data = await res.json();
negativeBox.innerHTML = data.length === 0
? "<p>No negative messages yet.</p>"
: data.map(msg =>
`<p class="neg-item">β οΈ <b>${msg.username}</b>: ${msg.message}</p>`
).join("");
}
// auto-refresh negative messages
setInterval(updateNegatives, 8000);
|