async function sendQuestion() {
const questionInput = document.getElementById("questionInput");
const sendButton = document.querySelector("button");
const buttonContent = sendButton.querySelector(".button-content");
const audioResponse = document.getElementById("audioResponse");
if (questionInput.value) {
// Onemogućujemo samo dugme
sendButton.disabled = true;
buttonContent.innerHTML =
'Sending... ';
const question = questionInput.value;
// Brisanje teksta iz polja za unos
questionInput.value = "";
try {
const response = await fetch(
"https://wnuy6xzt3e.execute-api.eu-central-1.amazonaws.com/Prod/audio/question",
{
method: "POST",
body: JSON.stringify({ transcript: question }),
headers: { "Content-Type": "application/json" },
}
);
if (response.ok) {
const audioBase64 = await response.text();
const audioSrc = `data:audio/mpeg;base64,${audioBase64}`;
audioResponse.src = audioSrc;
audioResponse.style.display = "block";
audioResponse.play();
} else {
console.error("Server error:", response.status);
}
} catch (error) {
console.error("Network error:", error);
}
// Dodavanje 'ended' događaja na audio element
audioResponse.onended = () => {
sendButton.disabled = false;
sendButton.innerHTML = 'Send';
};
}
}
function handleKeyPress(event) {
const sendButton = document.querySelector("button");
if (event.key === "Enter" && !sendButton.disabled) {
sendQuestion();
}
}