-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
135 lines (117 loc) · 4.28 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
135
<!DOCTYPE html>
<html>
<head>
<title>Chat Interface</title>
<!-- Material-UI CSS -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/material-design-icons/4.0.0/iconfont/material-icons.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" />
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f5f5f5;
}
.chat-container {
width: 400px;
max-height: 500px;
border: 1px solid #ccc;
overflow-y: auto;
padding: 20px;
margin: 20px auto;
background-color: #fff;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
}
.message {
padding: 10px;
margin-bottom: 5px;
background-color: #f2f2f2;
border-radius: 5px;
}
.user-message {
text-align: right;
background-color: #e0f7fa;
}
.input-container {
margin-top: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.message-input {
flex: 1;
padding: 8px;
border-radius: 5px;
border: 1px solid #ccc;
}
.send-button {
padding: 8px 16px;
border-radius: 5px;
background-color: #2196f3;
color: #fff;
border: none;
cursor: pointer;
}
.send-button:hover {
background-color: #1976d2;
}
</style>
</head>
<body>
<div class="chat-container" id="chatContainer">
<div class="message">Welcome to the chat!</div>
</div>
<div class="input-container">
<input type="text" id="messageInput" class="message-input" placeholder="Type your message..."
onkeypress="handleKeyPress(event)" />
<button class="send-button" onclick="sendMessage()">Send</button>
</div>
<!-- Material-UI JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
function handleKeyPress(event) {
if (event.keyCode === 13) { // 13 represents the Enter key
sendMessage();
}
}
function sendMessage() {
// Existing code for sending a message
}
function sendMessage() {
var messageInput = document.getElementById("messageInput");
var message = messageInput.value.trim();
if (message !== "") {
var chatContainer = document.getElementById("chatContainer");
var newMessage = document.createElement("div");
newMessage.className = "message user-message";
newMessage.innerText = message;
chatContainer.appendChild(newMessage);
messageInput.value = "";
// Scroll to the bottom of the chat container
chatContainer.scrollTop = chatContainer.scrollHeight;
// Send the message as an API request to localhost
fetch("http://localhost:8000/chat-response", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ text: message })
})
.then(response => response.json())
.then(message => {
// Handle the API response as needed
var newMessage = document.createElement("div");
newMessage.className = "message";
newMessage.innerText = message;
chatContainer.appendChild(newMessage);
})
.catch(error => {
// Handle errors
console.error("Error:", error);
});
}
}
</script>
</body>
</html>