-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatClient.html
209 lines (195 loc) · 6.67 KB
/
ChatClient.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatbot Application</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
}
.chat-container {
width: 400px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
display: flex;
flex-direction: column;
}
.chat-header {
background-color: #6200ea;
color: #fff;
padding: 16px;
font-size: 18px;
text-align: center;
}
.chat-messages {
flex: 1;
padding: 16px;
overflow-y: auto;
border-bottom: 1px solid #ddd;
}
.message {
margin-bottom: 12px;
padding: 8px 12px;
border-radius: 8px;
max-width: 70%;
}
.user-message {
background-color: #6200ea;
color: #fff;
align-self: flex-end;
}
.bot-message {
background-color: #f1f1f1;
color: #333;
align-self: flex-start;
}
.chat-input {
display: flex;
padding: 8px;
background-color: #f9f9f9;
}
.chat-input input {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
margin-right: 8px;
}
.chat-input button {
padding: 8px 16px;
background-color: #6200ea;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
.chat-input button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.chat-input button:hover:not(:disabled) {
background-color: #3700b3;
}
.loading-animation {
display: flex;
justify-content: center;
align-items: center;
padding: 8px;
}
.loading-dot {
width: 8px;
height: 8px;
margin: 0 4px;
background-color: #6200ea;
border-radius: 50%;
animation: bounce 1.4s infinite ease-in-out;
}
.loading-dot:nth-child(2) {
animation-delay: 0.2s;
}
.loading-dot:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes bounce {
0%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-10px);
}
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
Chatbot
</div>
<div class="chat-messages" id="chat-messages">
<!-- Chat messages will appear here -->
</div>
<div class="chat-input">
<input type="text" id="user-input" placeholder="Type a message...">
<button id="send-button" onclick="sendMessage()">Send</button>
</div>
</div>
<script>
// Function to send a message to the chatbot
async function sendMessage() {
const userInput = document.getElementById('user-input').value.trim();
if (!userInput) return;
// Add user message to the chat
appendMessage('user', userInput);
// Clear the input field and disable the send button
document.getElementById('user-input').value = '';
document.getElementById('send-button').disabled = true;
// Show loading animation
const loadingAnimation = createLoadingAnimation();
document.getElementById('chat-messages').appendChild(loadingAnimation);
try {
// Send the message to the Flask backend
const response = await fetch('http://127.0.0.1:5000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: userInput }),
credentials: 'include', // Include credentials (cookies)
});
if (!response.ok) {
throw new Error('Failed to get a response from the chatbot');
}
const data = await response.json();
const botMessage = data.response;
// Remove loading animation and add bot message to the chat
document.getElementById('chat-messages').removeChild(loadingAnimation);
appendMessage('bot', botMessage);
} catch (error) {
console.error('Error:', error);
document.getElementById('chat-messages').removeChild(loadingAnimation);
appendMessage('bot', 'Sorry, something went wrong. Please try again.');
} finally {
// Re-enable the send button
document.getElementById('send-button').disabled = false;
}
}
// Function to append a message to the chat window
function appendMessage(sender, message) {
const chatMessages = document.getElementById('chat-messages');
const messageElement = document.createElement('div');
messageElement.classList.add('message', `${sender}-message`);
messageElement.textContent = message;
chatMessages.appendChild(messageElement);
// Scroll to the bottom of the chat
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Function to create a loading animation
function createLoadingAnimation() {
const loadingContainer = document.createElement('div');
loadingContainer.classList.add('loading-animation');
for (let i = 0; i < 3; i++) {
const dot = document.createElement('div');
dot.classList.add('loading-dot');
loadingContainer.appendChild(dot);
}
return loadingContainer;
}
// Allow pressing Enter to send a message
document.getElementById('user-input').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>