Robbin / script.js
Swathi6's picture
Create script.js
f34af72 verified
// Get the DOM elements
const chatBox = document.getElementById('chat-box');
const userInput = document.getElementById('user-input');
// Function to simulate a response from the chatbot
function botResponse(userMessage) {
const lowerCaseMessage = userMessage.toLowerCase();
let response = '';
// Basic responses based on user input
if (lowerCaseMessage.includes('hello') || lowerCaseMessage.includes('hi')) {
response = "Hello! Welcome to our restaurant. How can I assist you today?";
} else if (lowerCaseMessage.includes('menu')) {
response = "Our menu includes: Pizza, Pasta, Burger, Salad, and Desserts. What would you like to order?";
} else if (lowerCaseMessage.includes('order') || lowerCaseMessage.includes('buy')) {
response = "What would you like to order from the menu?";
} else if (lowerCaseMessage.includes('pizza')) {
response = "Great choice! Our pizzas are delicious. Would you like a small, medium, or large pizza?";
} else if (lowerCaseMessage.includes('pasta')) {
response = "Yum! Our pasta is freshly made. Would you like it with marinara sauce or Alfredo?";
} else if (lowerCaseMessage.includes('burger')) {
response = "Our burgers are served with fries. Would you like a vegetarian or beef burger?";
} else if (lowerCaseMessage.includes('salad')) {
response = "We have a variety of salads. Would you like a Caesar salad or a garden salad?";
} else if (lowerCaseMessage.includes('dessert')) {
response = "For dessert, we have cakes, ice cream, and pie. What would you like to try?";
} else {
response = "I'm sorry, I didn't quite get that. Can you please repeat?";
}
// Display the bot response
displayMessage(response, 'bot');
}
// Function to display a message in the chat box
function displayMessage(message, sender) {
const messageElement = document.createElement('div');
messageElement.classList.add(sender === 'bot' ? 'bot-message' : 'user-message');
messageElement.textContent = message;
// Append the message to the chat box
chatBox.appendChild(messageElement);
// Scroll to the bottom to see the latest message
chatBox.scrollTop = chatBox.scrollHeight;
}
// Function to handle the sending of user input
function sendMessage() {
const userMessage = userInput.value.trim();
if (userMessage !== '') {
// Display user message
displayMessage(userMessage, 'user');
// Clear the user input field
userInput.value = '';
// Get bot's response after a slight delay
setTimeout(() => botResponse(userMessage), 500);
}
}
// Allow the user to press "Enter" to send a message
userInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
sendMessage();
}
});