-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex.html
134 lines (122 loc) · 3.15 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
}
#chat-container {
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
width: 400px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
#chat-history {
height: 300px;
overflow-y: scroll;
}
.user-message {
text-align: right;
padding: 10px;
background-color: #f0f0f0;
border-radius: 10px;
margin-bottom: 5px;
}
.bot-message {
text-align: left;
padding: 10px;
background-color: #e0f0e0;
border-radius: 10px;
margin-bottom: 5px;
}
form {
display: flex;
}
input {
flex-grow: 1;
margin-right: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
}
#loader {
display: none; /* Hide by default */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div id="chat-container">
<h1>Coding Money Chatbot</h1>
<div id="chat-history"></div>
<form id="chat-form">
<input type="text" id="user-input" placeholder="Enter your message">
<button type="submit">Send</button>
</form>
</div>
<div id="loader">
<img src="loader.gif" width="150px" alt="Loading...">
</div>
<script>
const chatHistory = document.getElementById('chat-history');
const userInput = document.getElementById('user-input');
const form = document.getElementById('chat-form');
async function sendMessage() {
const userMessage = userInput.value;
userInput.value = ''; // Clear input field
console.log(userMessage)
try {
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userInput: userMessage }),
});
const data = await response.json();
console.log(data)
const botMessage = data.response;
console.log(botMessage)
// Add chat message to the chat history
chatHistory.innerHTML += `<div class="user-message">${userMessage}</div>`;
chatHistory.innerHTML += `<div class="bot-message">${botMessage}</div>`;
// Scroll to the bottom of the chat history
chatHistory.scrollTop = chatHistory.scrollHeight;
} catch (error) {
console.error('Error:', error);
// Handle errors gracefully, e.g., display an error message to the user
}
}
form.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent form submission
const loader = document.getElementById('loader');
loader.style.display = 'block'; // Show the loader
sendMessage().finally(() => {
loader.style.display = 'none'; // Hide the loader after the message is sent
});;
});
</script>
</body>
</html>