Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +33 -75
static/script.js
CHANGED
|
@@ -43,45 +43,26 @@ function handleResponse(userInput) {
|
|
| 43 |
|
| 44 |
// Handle name input (first user response after bot's greeting)
|
| 45 |
if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
|
| 46 |
-
botResponse = `Nice to meet you, ${userInput}! 😊
|
| 47 |
options = [
|
| 48 |
-
{ text: '
|
| 49 |
-
{ text: '
|
|
|
|
| 50 |
];
|
| 51 |
-
} else if (lastMessage.includes('
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
console.log("Food preference selected: Vegetarian");
|
| 58 |
-
botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
|
| 59 |
-
fetchIngredients('vegetarian');
|
| 60 |
-
return;
|
| 61 |
-
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
|
| 62 |
-
console.log(`Non-veg option selected: ${lastMessage}`);
|
| 63 |
-
botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
|
| 64 |
-
fetchIngredients(lastMessage.toLowerCase());
|
| 65 |
-
return;
|
| 66 |
-
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
| 67 |
-
botResponse = 'Here are some ingredients to customize your dish:';
|
| 68 |
-
fetchIngredients('both');
|
| 69 |
-
return;
|
| 70 |
-
} else if (lastMessage.includes('no') && selectedMenuItem) {
|
| 71 |
-
addToCart(selectedMenuItem);
|
| 72 |
-
botResponse = 'Item added to cart! Would you like to order more?';
|
| 73 |
-
options = [
|
| 74 |
-
{ text: 'Yes', class: 'green' },
|
| 75 |
-
{ text: 'No', class: 'red' }
|
| 76 |
-
];
|
| 77 |
-
selectedMenuItem = null;
|
| 78 |
} else {
|
| 79 |
// Fallback for unrecognized input
|
| 80 |
botResponse = "Sorry, I didn't understand that. Could you please clarify or choose an option?";
|
| 81 |
if (conversation.length === 2) {
|
| 82 |
options = [
|
| 83 |
-
{ text: '
|
| 84 |
-
{ text: '
|
|
|
|
| 85 |
];
|
| 86 |
}
|
| 87 |
}
|
|
@@ -92,6 +73,26 @@ function handleResponse(userInput) {
|
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
function fetchIngredients(foodPreference) {
|
| 96 |
console.log(`Fetching ingredients for dietary preference: ${foodPreference}`);
|
| 97 |
fetch('/get_ingredients', {
|
|
@@ -358,49 +359,6 @@ function addToCart(item) {
|
|
| 358 |
console.log('Cart:', cart);
|
| 359 |
}
|
| 360 |
|
| 361 |
-
function displayOptions(options) {
|
| 362 |
-
const chatMessages = document.getElementById('chatMessages');
|
| 363 |
-
if (!chatMessages) {
|
| 364 |
-
console.error('Chat messages container not found for options!');
|
| 365 |
-
return;
|
| 366 |
-
}
|
| 367 |
-
options.forEach(opt => {
|
| 368 |
-
const button = document.createElement('button');
|
| 369 |
-
button.textContent = opt.text;
|
| 370 |
-
button.className = `option-button ${opt.class}`;
|
| 371 |
-
button.onclick = () => {
|
| 372 |
-
addMessage('user', opt.text);
|
| 373 |
-
conversation.push({ role: 'user', message: opt.text });
|
| 374 |
-
setTimeout(() => handleResponse(opt.text), 500);
|
| 375 |
-
};
|
| 376 |
-
chatMessages.appendChild(button);
|
| 377 |
-
});
|
| 378 |
-
chatMessages.appendChild(document.createElement('br'));
|
| 379 |
-
if (conversation.length > 1) { // Ensure back button appears only when there's a previous step
|
| 380 |
-
const backButton = document.createElement('button');
|
| 381 |
-
backButton.textContent = 'Go Back';
|
| 382 |
-
backButton.className = 'option-button';
|
| 383 |
-
backButton.onclick = () => {
|
| 384 |
-
if (conversation.length > 1) {
|
| 385 |
-
conversation.pop(); // Remove the last conversation entry
|
| 386 |
-
selectedIngredients = []; // Clear selected ingredients if going back
|
| 387 |
-
selectedMenuItem = null; // Clear selected menu item
|
| 388 |
-
chatMessages.innerHTML = ''; // Clear current messages
|
| 389 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render conversation
|
| 390 |
-
// Trigger response based on the now-last message
|
| 391 |
-
if (conversation.length > 0) {
|
| 392 |
-
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
| 393 |
-
}
|
| 394 |
-
}
|
| 395 |
-
};
|
| 396 |
-
chatMessages.appendChild(backButton);
|
| 397 |
-
}
|
| 398 |
-
}
|
| 399 |
|
| 400 |
-
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
| 401 |
-
if (e.key === 'Enter') {
|
| 402 |
-
sendMessage();
|
| 403 |
-
}
|
| 404 |
-
});
|
| 405 |
|
| 406 |
console.log('Script loaded successfully');
|
|
|
|
| 43 |
|
| 44 |
// Handle name input (first user response after bot's greeting)
|
| 45 |
if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
|
| 46 |
+
botResponse = `Nice to meet you, ${userInput}! 😊 How can I assist you today?`;
|
| 47 |
options = [
|
| 48 |
+
{ text: 'How do I contact customer support?', class: 'blue' },
|
| 49 |
+
{ text: 'What are your business hours?', class: 'blue' },
|
| 50 |
+
{ text: 'How do I reset my password?', class: 'blue' }
|
| 51 |
];
|
| 52 |
+
} else if (lastMessage.includes('how do i contact customer support?')) {
|
| 53 |
+
botResponse = 'You can email us at support@company.com.';
|
| 54 |
+
} else if (lastMessage.includes('what are your business hours?')) {
|
| 55 |
+
botResponse = 'We are open from 9 AM to 6 PM, Monday to Friday.';
|
| 56 |
+
} else if (lastMessage.includes('how do i reset my password?')) {
|
| 57 |
+
botResponse = 'Click on "Forgot Password" on the login page.';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
} else {
|
| 59 |
// Fallback for unrecognized input
|
| 60 |
botResponse = "Sorry, I didn't understand that. Could you please clarify or choose an option?";
|
| 61 |
if (conversation.length === 2) {
|
| 62 |
options = [
|
| 63 |
+
{ text: 'How do I contact customer support?', class: 'blue' },
|
| 64 |
+
{ text: 'What are your business hours?', class: 'blue' },
|
| 65 |
+
{ text: 'How do I reset my password?', class: 'blue' }
|
| 66 |
];
|
| 67 |
}
|
| 68 |
}
|
|
|
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
+
function displayOptions(options) {
|
| 77 |
+
// Function to display options on the UI
|
| 78 |
+
options.forEach(option => {
|
| 79 |
+
const button = document.createElement('button');
|
| 80 |
+
button.classList.add(option.class);
|
| 81 |
+
button.textContent = option.text;
|
| 82 |
+
button.onclick = () => handleResponse(option.text);
|
| 83 |
+
document.getElementById('options-container').appendChild(button);
|
| 84 |
+
});
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
function addMessage(role, message) {
|
| 88 |
+
// Function to add the bot or user message to the chat window
|
| 89 |
+
const messageContainer = document.createElement('div');
|
| 90 |
+
messageContainer.classList.add(role);
|
| 91 |
+
messageContainer.textContent = message;
|
| 92 |
+
document.getElementById('chat').appendChild(messageContainer);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
function fetchIngredients(foodPreference) {
|
| 97 |
console.log(`Fetching ingredients for dietary preference: ${foodPreference}`);
|
| 98 |
fetch('/get_ingredients', {
|
|
|
|
| 359 |
console.log('Cart:', cart);
|
| 360 |
}
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
console.log('Script loaded successfully');
|