Luka’s Smart Chatbot
Ask a question I couldn’t answer:
Submit
Thanks! Your question is ready to send.
(function(){
const chatlog = document.getElementById(‘chatlog’);
const input = document.getElementById(‘userInput’);
const suggestionInput = document.getElementById(‘userSuggestion’);
const submitBtn = document.getElementById(‘submitSuggestion’);
const thanksMsg = document.getElementById(‘thanksMessage’);
function addMessage(sender, text) {
const div = document.createElement(‘div’);
div.textContent = sender + “: ” + text;
div.style.margin = ‘8px 0’;
div.style.fontWeight = sender === ‘AI’ ? ‘700’ : ‘400’;
div.style.color = sender === ‘AI’ ? ‘#10a37f’ : ‘#d4d4d8’;
chatlog.appendChild(div);
chatlog.scrollTop = chatlog.scrollHeight;
}
function getResponse(msg) {
msg = msg.toLowerCase();
if(msg.includes(‘hello’) || msg.includes(‘hi’)) return “Hello! How can I help you today?”;
if(msg.includes(‘who are you’)) return “I’m Luka’s smart chatbot, here to assist you!”;
if(msg.includes(‘help’)) return “Feel free to ask me anything about Luka or this website.”;
if(msg.includes(‘website’)) return “This site is powered by AI showcasing Luka’s work.”;
if(msg.includes(‘your name’)) return “Just Luka’s chatbot for now!”;
if(msg.includes(‘thank’)) return “You’re welcome! Glad I could help.”;
if(msg.includes(‘bye’) || msg.includes(‘goodbye’)) return “Goodbye! Have a great day!”;
return null; // no good answer found
}
input.addEventListener(‘keydown’, function(e){
if(e.key === ‘Enter’ && input.value.trim() !== ”) {
const userText = input.value.trim();
addMessage(‘You’, userText);
input.value = ”;
setTimeout(() => {
const aiReply = getResponse(userText);
if(aiReply) {
addMessage(‘AI’, aiReply);
} else {
addMessage(‘AI’, “Sorry, I don’t understand that yet, please type question below.”);
}
}, 500);
}
});
submitBtn.addEventListener(‘click’, function() {
const suggestion = suggestionInput.value.trim();
if(suggestion === ”) {
alert(‘Please enter a question to submit.’);
return;
}
const subject = encodeURIComponent(“Unanswered question from Luka’s Chatbot”);
const body = encodeURIComponent(suggestion);
const mailtoLink = `mailto:lukagallagher@icloud.com?subject=${subject}&body=${body}`;
window.location.href = mailtoLink;
thanksMsg.style.display = ‘block’;
suggestionInput.value = ”;
setTimeout(() => {
thanksMsg.style.display = ‘none’;
}, 4000);
});
// Initial greeting
addMessage(‘AI’, “Hi! I’m Luka’s smart chatbot. Ask me anything.”);
})();