<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatbot Tool</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="chat-container">
<h1>Chatbot</h1>
<div class="chat-box" id="chatBox"></div>
<div class="chat-input">
<input type="text" id="userInput" placeholder="Type a message...">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
background: linear-gradient(to right, #36D1DC, #5B86E5);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
text-align: center;
padding: 10px;
}
.chat-container {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
width: 350px;
display: flex;
flex-direction: column;
}
h1 {
margin-bottom: 10px;
color: #333;
}
.chat-box {
height: 300px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
background: #f9f9f9;
border-radius: 5px;
}
.chat-message {
margin: 5px 0;
padding: 8px;
border-radius: 5px;
}
.user {
background: #36D1DC;
color: #fff;
text-align: right;
}
.bot {
background: #5B86E5;
color: #fff;
text-align: left;
}
.chat-input {
display: flex;
}
.chat-input input {
flex: 1;
padding: 10px;
border: 2px solid #36D1DC;
border-radius: 5px;
}
.chat-input button {
background: #36D1DC;
color: #fff;
border: none;
padding: 10px;
margin-left: 5px;
border-radius: 5px;
cursor: pointer;
}
.chat-input button:hover {
background: #2bafc6;
}
async function sendMessage() {
let userInput = document.getElementById("userInput").value.trim();
if (userInput === "") return;
appendMessage("user", userInput);
document.getElementById("userInput").value = "";
// Fetch AI response from a free API (replace with your API)
let botResponse = await getBotResponse(userInput);
appendMessage("bot", botResponse);
}
function appendMessage(sender, message) {
let chatBox = document.getElementById("chatBox");
let messageDiv = document.createElement("div");
messageDiv.classList.add("chat-message", sender);
messageDiv.innerText = message;
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
// Free AI API (replace with OpenAI, Dialogflow, or a free chatbot API)
async function getBotResponse(userMessage) {
try {
let response = await fetch(`https://api.something.com/chat?message=${encodeURIComponent(userMessage)}`);
let data = await response.json();
return data.reply || "Sorry, I don't understand.";
} catch {
return "Error fetching response.";
}
}
