-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
69 lines (63 loc) · 2.66 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with Thomas's Blog</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
#chat-container { border: 1px solid #ccc; height: 400px; overflow-y: auto; padding: 10px; margin-bottom: 10px; }
#user-input { width: 70%; padding: 5px; }
#send-button { padding: 5px 10px; }
</style>
</head>
<body>
<h1>Chat with Thomas's Blog</h1>
<div id="chat-container"></div>
<input type="text" id="user-input" placeholder="Type your message here...">
<button id="send-button">Send</button>
<script>
const chatContainer = document.getElementById('chat-container');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
function addMessage(sender, message) {
const messageElement = document.createElement('p');
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
async function sendMessage() {
const message = userInput.value.trim();
if (message) {
addMessage('You', message);
userInput.value = '';
try {
const response = await fetch('https://us-west1-thomasvn0.cloudfunctions.net/thomasvn-chat/Chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: message }),
});
if (response.ok) {
const data = await response.text();
addMessage('Thomas Bot', data);
} else {
console.error('Error:', response.statusText);
addMessage('Thomas Bot', 'Sorry, there was an error processing your request.');
}
} catch (error) {
console.error('Error:', error);
addMessage('ThomasBot', 'Sorry, there was an error processing your request.');
}
}
}
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>