-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitbot.html
452 lines (380 loc) · 15 KB
/
fitbot.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<!-- This file contains the code for the selected section. -->
<!-- It is responsible for performing a specific task or functionality. -->
<!-- Please refer to the code comments for detailed explanations of the implementation. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FitBot Chat</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link rel="icon" type="image/x-icon" href="/assets/images/Favicon/Regular/favicon-32x32.png" />
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background-color: #f4f4f4;
background-image: url("assets/images/abstract-luxury-gradient-blue-background-smooth-dark-blue-with-black-vignette-studio-banner.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
.submit-button {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
border-radius: 50%;
width: 34px;
height: 34px;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
border: none;
background-color: #686868;
}
.submit-button .fa {
color: white;
font-size: 16px;
}
.chat-container {
margin: auto;
}
.chat-footer {
position: sticky;
bottom: 0;
background-color: white;
padding: 10px;
border-top: 1px solid #ccc;
}
.chat-container {
background-color: white;
border: 1px solid #ccc;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
border-radius: 15px;
overflow: hidden;
display: flex;
flex-direction: column;
height: 700px;
}
#chat-display {
overflow-y: auto;
flex-grow: 1;
padding: 15px;
border-bottom: 1px solid #ccc;
}
#chat-form {
padding: 10px;
background-color: white;
}
#chat-input {
border: 1px solid #ccc;
border-radius: 20px;
padding: 10px 20px 10px 20px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
width: 100%;
padding-right: 60px;
height: 110%;
}
#chat-form .btn {
border-radius: 50px;
}
.form-group {
position: relative;
}
.message-row {
display: flex;
}
.message {
display: inline-block;
max-width: 70%;
margin-bottom: 12px;
padding: 12px;
border-radius: 15px;
line-height: 1.6;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.user-message {
background-color: #dcf8c6;
border-bottom-left-radius: 0;
}
.bot-message {
background-color: #ebebeb;
border-bottom-right-radius: 0;
margin-left: auto;
}
.typing-indicator {
display: flex;
justify-content: flex-start;
margin-left: 5px;
}
.typing-indicator .dot {
height: 10px;
width: 10px;
background-color: #333;
border-radius: 50%;
margin: 6px 3px;
opacity: 0;
animation: typing 1.5s infinite;
}
.typing-indicator .dot:nth-child(1) {
animation-delay: 0s;
}
.typing-indicator .dot:nth-child(2) {
animation-delay: 0.25s;
}
.typing-indicator .dot:nth-child(3) {
animation-delay: 0.5s;
}
@keyframes typing {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
body {
background-color: #000000;
padding: 0px;
margin: 0px;
}
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
#gradient {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
position: fixed;
}
.container {
width: 70%;
}
</style>
</head>
<body>
<div id="gradient">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<!-- Chat Container -->
<div class="chat-container">
<!-- Chat Display -->
<div id="chat-display">
<!-- Chat messages will be displayed here -->
</div>
<!-- Chat Input -->
<form id="chat-form" class="form-inline">
<div class="form-group flex-grow-1">
<input type="text" id="chat-input" class="form-control" placeholder="Ask FitBot...">
<button type="submit" class="btn btn-dark submit-button">
<i class="fa fa-paper-plane"></i>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div id="typing-indicator" class="typing-indicator" style="display: none;">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<div id="close-icon" class="position-fixed" style="top: 30px; right: 30px; font-size: 20px; cursor: pointer;">
<a href="index.html" style="text-decoration: none;"><span class="fa-stack fa-lg" style="color:#343a40;">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-times fa-stack-1x fa-inverse"></i>
</span></a>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</div>
</body>
</html>
<script>
document.addEventListener('DOMContentLoaded', function () {
displayInitialBotMessage();
});
// Display the initial message from the bot
function displayInitialBotMessage() {
const typingRow = showTypingIndicator();
setTimeout(() => {
const initialMessage = "FitBot: Hey there! I'm FitBot, your friendly fitness advisor on AthletiLink. 🤖💪 I'm here to provide guidance and support when it comes to fitness, workouts, nutrition, and general wellness. I can help you with workout routines, healthy eating tips, and motivation to keep you on track towards your fitness goals. Just think of me as your virtual workout buddy! How can I assist you today?";
replaceTypingIndicatorWithMessage(typingRow, initialMessage, 'bot');
}, 1500); // Delay of 1.5 seconds
}
// Listen for the form submit event
document.getElementById('chat-form').addEventListener('submit', function (event) {
event.preventDefault();
sendChatMessage();
});
// Send the message to the server
async function sendChatMessage() {
const inputField = document.getElementById('chat-input');
const message = inputField.value.trim();
if (message) {
displayMessage('You: ' + message, 'user');
const typingRow = showTypingIndicator(); // Show typing indicator and get the row element
inputField.value = ''; // Clear the input field
try {
const response = await fetch('http://localhost:5500/sendToOpenAI', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message })
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
if (data.reply) {
replaceTypingIndicatorWithMessage(typingRow, 'FitBot: ' + data.reply, 'bot');
}
} catch (error) {
replaceTypingIndicatorWithMessage(typingRow, 'Error: Could not reach the server.', 'bot');
console.error('Fetch error:', error);
}
}
}
// Show the typing indicator and return the row element
function showTypingIndicator() {
const chatDisplay = document.getElementById('chat-display');
const typingRow = document.createElement('div');
typingRow.className = 'message-row';
typingRow.innerHTML = '<div class="message bot-message"><div class="typing-indicator"><span class="dot"></span><span class="dot"></span><span class="dot"></span></div></div>';
chatDisplay.appendChild(typingRow);
chatDisplay.scrollTop = chatDisplay.scrollHeight;
return typingRow;
}
function replaceTypingIndicatorWithMessage(typingRow, message, sender) {
typingRow.innerHTML = '';
const messageElement = document.createElement('div');
messageElement.className = `message ${sender}-message`;
// Extract the sender text and the actual message
const senderText = 'FitBot: ';
const actualMessage = message.substring(senderText.length);
// Create elements for the sender text and the message
const senderElement = document.createElement('strong');
senderElement.textContent = senderText;
const messageTextElement = document.createElement('span');
messageElement.appendChild(senderElement);
messageElement.appendChild(messageTextElement);
typingRow.appendChild(messageElement);
// Call the function to type the message letter by letter
typeMessageLetterByLetter(messageTextElement, actualMessage, 0);
}
// Process the message for display
function processMessageForDisplay(message) {
// Convert new lines to HTML breaks
return message.replace(/\n/g, '<br>');
}
// Type the message letter by letter effect
function typeMessageLetterByLetter(element, message, index, currentMessage = "", isBold = false) {
if (index < message.length) {
const char = message.charAt(index);
if (char === '\n') {
currentMessage += '<br>';
} else if (char === '*' && message.slice(index, index + 2) === '**') {
// Check for the closing **
if (isBold) {
currentMessage += '</strong>';
isBold = false;
} else {
currentMessage += '<strong>';
isBold = true;
}
index += 1; // Skip the closing **
} else {
currentMessage += char;
}
element.innerHTML = currentMessage;
// Dynamic typing speed calculation
const baseSpeed = 20; // Base speed in milliseconds
const maxSpeed = 30; // Maximum speed in milliseconds
const lengthFactor = 300; // Adjust this factor to control how much the length affects the speed
let delay = Math.max(maxSpeed, baseSpeed - (message.length / lengthFactor));
setTimeout(() => {
typeMessageLetterByLetter(element, message, index + 1, currentMessage, isBold);
}, delay);
} else if (isBold) {
// If the message ends with an open bold tag, close it
currentMessage += '</strong>';
element.innerHTML = currentMessage;
}
}
// Display a message in the chat
function displayMessage(message, sender) {
const chatDisplay = document.getElementById('chat-display');
const messageRow = document.createElement('div');
messageRow.className = 'message-row';
const messageElement = document.createElement('div');
messageElement.className = `message ${sender}-message`;
const boldPart = document.createElement('strong');
const senderText = sender === 'user' ? 'You: ' : 'FitBot: ';
boldPart.textContent = senderText;
const normalPart = document.createTextNode(message.substring(senderText.length));
messageElement.appendChild(boldPart);
messageElement.appendChild(normalPart);
messageRow.appendChild(messageElement);
chatDisplay.appendChild(messageRow);
chatDisplay.scrollTop = chatDisplay.scrollHeight;
}
var colors = new Array(
[62, 35, 255],
[60, 255, 60],
[255, 35, 98],
[45, 175, 230],
[255, 0, 255],
[255, 128, 0]);
var step = 0;
var colorIndices = [0, 1, 2, 3];
//transition speed
var gradientSpeed = 0.002;
function updateGradient() {
if ($ === undefined) return;
var c0_0 = colors[colorIndices[0]];
var c0_1 = colors[colorIndices[1]];
var c1_0 = colors[colorIndices[2]];
var c1_1 = colors[colorIndices[3]];
var istep = 1 - step;
var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
var color1 = "rgb(" + r1 + "," + g1 + "," + b1 + ")";
var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
var color2 = "rgb(" + r2 + "," + g2 + "," + b2 + ")";
$('#gradient').css({
background: "-webkit-gradient(linear, left top, right top, from(" + color1 + "), to(" + color2 + "))"
}).css({
background: "-moz-linear-gradient(left, " + color1 + " 0%, " + color2 + " 100%)"
});
step += gradientSpeed;
if (step >= 1) {
step %= 1;
colorIndices[0] = colorIndices[1];
colorIndices[2] = colorIndices[3];
colorIndices[1] = (colorIndices[1] + Math.floor(1 + Math.random() * (colors.length - 1))) % colors.length;
colorIndices[3] = (colorIndices[3] + Math.floor(1 + Math.random() * (colors.length - 1))) % colors.length;
}
}
setInterval(updateGradient, 10);
</script>