-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage3.html
80 lines (72 loc) · 2.82 KB
/
page3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatbox</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.chatbox { max-width: 600px; margin: 0 auto; }
.messages { border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: auto; margin-bottom: 10px; }
.message { margin-bottom: 10px; }
.user-message { text-align: right; color: blue; }
.bot-message { text-align: left; color: green; }
input[type="text"] { width: calc(100% - 60px); padding: 10px; }
button { padding: 10px; width: 50px; }
</style>
</head>
<body>
<div class="chatbox">
<div class="messages" id="messages"></div>
<div>
<input type="text" id="questionInput" placeholder="Type your question here...">
<button onclick="sendQuestion()">Ask</button>
</div>
</div>
<script>
async function sendQuestion() {
const questionInput = document.getElementById('questionInput');
const question = questionInput.value.trim();
if (question === '') return;
displayMessage(question, 'user-message');
questionInput.value = '';
const loadingMessageId = displayMessage('Thinking...', 'bot-message');
try {
console.log('Sending question:', question); // Debugging log
const response = await fetch('https://app-598234327672.us-central1.run.app/ask', {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ question: question }), // Corrected body format
});
if (!response.ok) {
const errorDetails = await response.text();
console.error('API Error:', errorDetails); // Debugging log
throw new Error(`Error ${response.status}: ${response.statusText}`);
}
const data = await response.json();
console.log('API Response:', data); // Debugging log
updateMessage(loadingMessageId, data.response, 'bot-message');
} catch (error) {
console.error('Error:', error); // Debugging log
updateMessage(loadingMessageId, 'An error occurred. Please try again.', 'bot-message');
}
}
function displayMessage(message, className) {
const messagesDiv = document.getElementById('messages');
const messageDiv = document.createElement('div');
messageDiv.textContent = message;
messageDiv.className = `message ${className}`;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
return messageDiv;
}
function updateMessage(messageDiv, newMessage, className) {
messageDiv.textContent = newMessage;
messageDiv.className = `message ${className}`;
}
</script>
</body>
</html>