ai-chatbot / templates /index.html
Utkarsh Verma
Changes in UI
1ff88ce
raw
history blame
3.93 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>AI Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
background-color: #f4f4f4;
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode {
background-color: #121212;
color: white;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
background-color: #333;
color: white;
}
header h1 {
margin: 0;
font-size: 1.5em;
}
.controls {
display: flex;
gap: 10px;
}
#chat-box {
flex-grow: 1;
padding: 20px;
overflow-y: auto;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
background-color: #fff;
}
body.dark-mode #chat-box {
background-color: #1e1e1e;
}
#input-container {
display: flex;
padding: 10px;
background-color: #f9f9f9;
border-top: 1px solid #ccc;
}
body.dark-mode #input-container {
background-color: #1a1a1a;
}
#user-input {
flex-grow: 1;
padding: 10px;
font-size: 1em;
border: 1px solid #ccc;
border-radius: 4px;
}
#send-btn {
margin-left: 10px;
padding: 10px 15px;
font-size: 1em;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
#send-btn:hover {
background-color: #45a049;
}
.message {
margin: 10px 0;
}
.message strong {
display: inline-block;
width: 60px;
}
</style>
</head>
<body>
<header>
<h1>AI Chatbot</h1>
<div class="controls">
<button onclick="toggleMode()">πŸŒ“ Theme</button>
<button onclick="clearChat()">πŸ—‘οΈ Clear</button>
<button onclick="exportChat()">πŸ“ Export</button>
</div>
</header>
<div id="chat-box"></div>
<div id="input-container">
<input type="text" id="user-input" placeholder="Type a message..." onkeypress="handleEnter(event)">
<button id="send-btn" onclick="sendMessage()">Send</button>
</div>
<script>
function handleEnter(e) {
if (e.key === 'Enter') sendMessage();
}
async function sendMessage() {
const userInput = document.getElementById("user-input").value.trim();
const chatBox = document.getElementById("chat-box");
if (!userInput) return;
chatBox.innerHTML += `<div class="message"><strong>You:</strong> ${userInput}</div>`;
scrollToBottom();
document.getElementById("user-input").value = "";
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: userInput })
});
const data = await response.json();
const reply = data.reply || "Error in response";
chatBox.innerHTML += `<div class="message"><strong>Bot:</strong> ${reply}</div>`;
scrollToBottom();
}
function toggleMode() {
document.body.classList.toggle("dark-mode");
}
function clearChat() {
document.getElementById("chat-box").innerHTML = "";
}
function scrollToBottom() {
const chatBox = document.getElementById("chat-box");
chatBox.scrollTop = chatBox.scrollHeight;
}
function exportChat() {
const chat = document.getElementById("chat-box").innerText;
const blob = new Blob([chat], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "chat_history.txt";
a.click();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>