-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
script.js
33 lines (29 loc) · 1.11 KB
/
script.js
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
const chatForm = document.getElementById("chat-form");
const chatInput = document.getElementById("chat-input");
const chatOutput = document.getElementById("chat-output");
chatForm.addEventListener("submit", async (e) => {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) return;
chatOutput.innerHTML += `<p class="user-message">${message}</p>`;
chatInput.value = "";
chatOutput.scrollTop = chatOutput.scrollHeight;
const response = await fetch("gptchat.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
});
if (response.ok) {
const data = await response.json();
if (data.choices && data.choices[0] && data.choices[0].text) {
chatOutput.innerHTML += `<p class="bot-message">${data.choices[0].text}</p>`;
} else {
console.error("Error: Unexpected response format", data);
}
chatOutput.scrollTop = chatOutput.scrollHeight;
} else {
console.error("Error communicating with GPTChat API");
}
});