<div class="chat-container">
<div class="chat-header">
<div class="bot-avatar">🤖</div>
<div class="header-info">
<h2>AI Assistant</h2>
<p>Online • Always ready to help</p>
</div>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot">
<div class="message-avatar">🤖</div>
<div>
<div class="message-content">
Hello! I'm your AI assistant. How can I help you today?
</div>
<div class="message-time" id="initialTime"></div>
</div>
</div>
</div>
<div class="chat-input-container">
<div class="quick-replies">
<button class="quick-reply-btn" onclick="sendQuickReply('Tell me a joke')">Tell me a joke 😄</button>
<button class="quick-reply-btn" onclick="sendQuickReply('What can you do?')">What can you do? 🤔</button>
<button class="quick-reply-btn" onclick="sendQuickReply('Help me')">Help me 💡</button>
</div>
<div class="chat-input-wrapper">
<input type="text" class="chat-input" id="userInput" placeholder="Type your message..." onkeypress="handleKeyPress(event)">
<button class="send-button" id="sendBtn" onclick="sendMessage()">➤</button>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.chat-container {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 800px;
height: 600px;
display: flex;
flex-direction: column;
overflow: hidden;
}
.chat-header {
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
color: white;
padding: 20px 24px;
display: flex;
align-items: center;
gap: 15px;
}
.bot-avatar {
width: 50px;
height: 50px;
background: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.header-info h2 {
font-size: 20px;
font-weight: 600;
margin-bottom: 2px;
}
.header-info p {
font-size: 13px;
opacity: 0.9;
}
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 24px;
background: #f8f9fa;
display: flex;
flex-direction: column;
gap: 16px;
}
.chat-messages::-webkit-scrollbar {
width: 6px;
}
.chat-messages::-webkit-scrollbar-track {
background: #f1f1f1;
}
.chat-messages::-webkit-scrollbar-thumb {
background: #ee0979;
border-radius: 3px;
}
.message {
display: flex;
gap: 12px;
animation: fadeIn 0.3s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.message.user {
flex-direction: row-reverse;
}
.message-avatar {
width: 36px;
height: 36px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
flex-shrink: 0;
}
.bot .message-avatar {
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
color: white;
}
.user .message-avatar {
background: #6c757d;
color: white;
}
.message-content {
max-width: 70%;
padding: 12px 16px;
border-radius: 12px;
line-height: 1.5;
font-size: 15px;
}
.bot .message-content {
background: white;
color: #2d3748;
border: 1px solid #e9ecef;
}
.user .message-content {
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
color: white;
}
.message-time {
font-size: 11px;
opacity: 0.6;
margin-top: 4px;
}
.typing-indicator {
display: none;
gap: 4px;
padding: 12px 16px;
}
.typing-indicator.active {
display: flex;
}
.typing-dot {
width: 8px;
height: 8px;
background: #6c757d;
border-radius: 50%;
animation: typing 1.4s infinite;
}
.typing-dot:nth-child(2) {
animation-delay: 0.2s;
}
.typing-dot:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes typing {
0%, 60%, 100% {
transform: translateY(0);
opacity: 0.7;
}
30% {
transform: translateY(-10px);
opacity: 1;
}
}
.chat-input-container {
padding: 20px 24px;
background: white;
border-top: 1px solid #e9ecef;
}
.chat-input-wrapper {
display: flex;
gap: 12px;
align-items: center;
}
.chat-input {
flex: 1;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 24px;
font-size: 15px;
outline: none;
transition: all 0.3s;
}
.chat-input:focus {
border-color: #ee0979;
}
.send-button {
width: 48px;
height: 48px;
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
border: none;
border-radius: 50%;
color: white;
font-size: 20px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s;
box-shadow: 0 4px 12px rgba(238, 9, 121, 0.3);
}
.send-button:hover {
transform: scale(1.05);
box-shadow: 0 6px 16px rgba(238, 9, 121, 0.4);
}
.send-button:active {
transform: scale(0.95);
}
.send-button:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: scale(1);
}
.quick-replies {
display: flex;
gap: 8px;
margin-top: 12px;
flex-wrap: wrap;
}
.quick-reply-btn {
padding: 8px 16px;
background: white;
border: 1px solid #ee0979;
color: #ee0979;
border-radius: 20px;
font-size: 13px;
cursor: pointer;
transition: all 0.3s;
}
.quick-reply-btn:hover {
background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
color: white;
transform: translateY(-2px);
}
const chatMessages = document.getElementById('chatMessages');
const userInput = document.getElementById('userInput');
const sendBtn = document.getElementById('sendBtn');
// Predefined responses for the chatbot
const responses = {
'hello': 'Hi there! How can I assist you today?',
'hi': 'Hello! What would you like to know?',
'how are you': 'I\'m doing great, thank you for asking! How can I help you?',
'what can you do': 'I can answer questions, provide information, tell jokes, and have conversations with you!',
'tell me a joke': 'Why don\'t scientists trust atoms? Because they make up everything! 😄',
'joke': 'What do you call a bear with no teeth? A gummy bear! 🐻',
'help': 'I\'m here to help! You can ask me questions, request information, or just chat. What do you need?',
'bye': 'Goodbye! Have a wonderful day! Feel free to come back anytime.',
'thank you': 'You\'re welcome! Is there anything else I can help you with?',
'thanks': 'My pleasure! Let me know if you need anything else.',
'weather': 'I don\'t have access to real-time weather data, but you can check your local weather service for accurate information!',
'time': `The current time is ${new Date().toLocaleTimeString()}.`,
'date': `Today is ${new Date().toLocaleDateString()}.`,
'name': 'I\'m your AI Assistant! You can call me Bot. What\'s your name?',
'who are you': 'I\'m an AI chatbot created to assist and chat with you!',
'default': 'I understand you said: "[USER_INPUT]". I\'m still learning! Can you rephrase that or ask something else?'
};
// Initialize with current time
document.getElementById('initialTime').textContent = getCurrentTime();
function getCurrentTime() {
return new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
addMessage(message, 'user');
userInput.value = '';
showTypingIndicator();
setTimeout(() => {
hideTypingIndicator();
const response = getBotResponse(message);
addMessage(response, 'bot');
}, 1000 + Math.random() * 1000);
}
function sendQuickReply(message) {
addMessage(message, 'user');
showTypingIndicator();
setTimeout(() => {
hideTypingIndicator();
const response = getBotResponse(message);
addMessage(response, 'bot');
}, 1000);
}
function addMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}`;
const avatar = document.createElement('div');
avatar.className = 'message-avatar';
avatar.textContent = sender === 'bot' ? '🤖' : '👤';
const contentWrapper = document.createElement('div');
const content = document.createElement('div');
content.className = 'message-content';
content.textContent = text;
const time = document.createElement('div');
time.className = 'message-time';
time.textContent = getCurrentTime();
contentWrapper.appendChild(content);
contentWrapper.appendChild(time);
messageDiv.appendChild(avatar);
messageDiv.appendChild(contentWrapper);
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function showTypingIndicator() {
const typingDiv = document.createElement('div');
typingDiv.className = 'message bot';
typingDiv.id = 'typingIndicator';
const avatar = document.createElement('div');
avatar.className = 'message-avatar';
avatar.textContent = '🤖';
const indicator = document.createElement('div');
indicator.className = 'typing-indicator active';
indicator.innerHTML = '<div class="typing-dot"></div><div class="typing-dot"></div><div class="typing-dot"></div>';
typingDiv.appendChild(avatar);
typingDiv.appendChild(indicator);
chatMessages.appendChild(typingDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function hideTypingIndicator() {
const indicator = document.getElementById('typingIndicator');
if (indicator) {
indicator.remove();
}
}
function getBotResponse(userMessage) {
const message = userMessage.toLowerCase();
for (let key in responses) {
if (message.includes(key)) {
return responses[key];
}
}
return responses['default'].replace('[USER_INPUT]', userMessage);
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendMessage();
}
}
userInput.focus();
Login to leave a comment
Login
No comments yet. Be the first!