File size: 1,684 Bytes
fede00a
 
a61644d
 
fede00a
 
 
a61644d
 
 
 
 
 
 
 
 
fede00a
a61644d
214727a
a61644d
 
 
 
 
 
fede00a
 
a61644d
fede00a
 
a61644d
fede00a
 
 
 
 
 
 
a61644d
 
 
 
 
 
 
 
 
 
 
 
 
fede00a
 
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
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... <span class="loading-spinner"></span>';

    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 = '<span class="button-content">Send</span>';
    };
  }
}

function handleKeyPress(event) {
  const sendButton = document.querySelector("button");
  if (event.key === "Enter" && !sendButton.disabled) {
    sendQuestion();
  }
}