Sure! Here’s a very simple rule-based chatbot you can embed directly into your website without any API. It handles a few example phrases and replies accordingly.
You can copy-paste this full HTML file into your website or adapt the code snippet inside your page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
max-width: 400px;
margin: auto;
background: #f5f5f5;
}
#chatbox {
border: 1px solid #ccc;
background: white;
padding: 10px;
height: 300px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 10px;
}
.message {
padding: 8px 12px;
border-radius: 15px;
max-width: 75%;
word-wrap: break-word;
}
.user {
background-color: #0084ff;
color: white;
align-self: flex-end;
}
.bot {
background-color: #e5e5ea;
color: black;
align-self: flex-start;
}
#input-area {
margin-top: 10px;
display: flex;
gap: 10px;
}
#userInput {
flex-grow: 1;
padding: 10px;
border-radius: 15px;
border: 1px solid #ccc;
font-size: 16px;
}
#sendBtn {
padding: 0 20px;
font-size: 16px;
border: none;
border-radius: 15px;
background-color: #0084ff;
color: white;
cursor: pointer;
}
#sendBtn:disabled {
background-color: #a0cfff;
cursor: default;
}
</style>
</head>
<body>
<h2>Simple Chatbot</h2>
<div id="chatbox"></div>
<div id="input-area">
<input type="text" id="userInput" placeholder="Type your message here..." autocomplete="off" />
<button id="sendBtn" disabled>Send</button>
</div>
<script>
const chatbox = document.getElementById('chatbox');
const userInput = document.getElementById('userInput');
const sendBtn = document.getElementById('sendBtn');
// Enable send button only if input is not empty
userInput.addEventListener('input', () => {
sendBtn.disabled = userInput.value.trim() === '';
});
// Basic rule-based bot responses
function getBotReply(message) {
message = message.toLowerCase();
if (message.includes('hello') || message.includes('hi')) {
return 'Hello! How can I help you today?';
}
if (message.includes('how are you')) {
return "I'm just a bot, but I'm doing great, thanks!";
}
if (message.includes('help')) {
return 'Sure, ask me anything you want.';
}
if (message.includes('price')) {
return 'Our prices start at $99. Let me know if you want more details.';
}
if (message.includes('bye') || message.includes('goodbye')) {
return 'Goodbye! Have a great day!';
}
return "Sorry, I didn't understand that. Can you try asking in a different way?";
}
// Add chat message to the chatbox
function addMessage(text, sender) {
const msgDiv = document.createElement('div');
msgDiv.textContent = text;
msgDiv.classList.add('message');
if (sender === 'user') {
msgDiv.classList.add('user');
} else {
msgDiv.classList.add('bot');
}
chatbox.appendChild(msgDiv);
chatbox.scrollTop = chatbox.scrollHeight;
}
// Handle sending message
function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
addMessage(message, 'user');
const botReply = getBotReply(message);
setTimeout(() => {
addMessage(botReply, 'bot');
}, 500);
userInput.value = '';
sendBtn.disabled = true;
}
sendBtn.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !sendBtn.disabled) {
sendMessage();
}
});
</script>
</body>
</html>
How it works:
- The user types a message and clicks Send or presses Enter.
- The message appears in the chat window.
- The bot replies after a short delay with a predefined answer depending on keywords.
- If the bot doesn’t recognize the message, it sends a default fallback reply.
You can customize it by:
- Adding more keywords and responses in the
getBotReply function.
- Styling it differently using CSS.
Let me know if you want help adding more features!