Spaces:
Sleeping
Sleeping
let conversation = [ | |
{ role: 'bot', message: 'Hello! I’m Chef Bot, your culinary assistant! What’s your name?' } | |
]; | |
let selectedItems = []; | |
let selectionBoxVisible = false; | |
function addMessage(role, message) { | |
const chatMessages = document.getElementById('chatMessages'); | |
if (!chatMessages) { | |
console.error('Chat messages container not found!'); | |
return; | |
} | |
const messageDiv = document.createElement('div'); | |
messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message'; | |
messageDiv.textContent = message; | |
chatMessages.appendChild(messageDiv); | |
chatMessages.scrollTop = chatMessages.scrollHeight; | |
console.log(`Added ${role} message: ${message}`); | |
} | |
function sendMessage() { | |
const userInput = document.getElementById('userInput'); | |
if (!userInput) { | |
console.error('User input field not found!'); | |
return; | |
} | |
const message = userInput.value.trim(); | |
if (message) { | |
addMessage('user', message); | |
conversation.push({ role: 'user', message: message }); | |
selectionBoxVisible = true; | |
handleResponse(message); | |
} else { | |
addMessage('bot', 'Please type a dish or preference! 😄'); | |
} | |
userInput.value = ''; | |
updateSelectionBox(); | |
} | |
function handleResponse(userInput) { | |
const lowerInput = userInput.toLowerCase(); | |
let botResponse = ''; | |
if (conversation.length === 2) { | |
botResponse = `Hi ${userInput}! 🍳 Search for a dish or choose a preference below!`; | |
displayOptions([ | |
{ text: 'Vegetarian', class: 'green' }, | |
{ text: 'Non-Vegetarian', class: 'red' }, | |
{ text: 'Both', class: 'gray' } | |
]); | |
addMessage('bot', botResponse); | |
} else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') { | |
botResponse = `Fetching ${lowerInput} dishes...`; | |
addMessage('bot', botResponse); | |
fetchMenuItems(lowerInput); | |
} else { | |
botResponse = `Looking for "${userInput}"...`; | |
addMessage('bot', botResponse); | |
fetchMenuItems(null, userInput); | |
} | |
} | |
function updateSelectionBox() { | |
const chatMessages = document.getElementById('chatMessages'); | |
if (!chatMessages) return; | |
const existingBox = document.querySelector('.selection-box'); | |
if (existingBox) existingBox.remove(); | |
if (!selectionBoxVisible && selectedItems.length === 0) return; | |
const selectionBox = document.createElement('div'); | |
selectionBox.className = 'selection-box'; | |
const vegButton = document.createElement('button'); | |
vegButton.textContent = 'Veg'; | |
vegButton.className = 'dietary-button green'; | |
vegButton.onclick = () => { | |
addMessage('user', 'Vegetarian'); | |
conversation.push({ role: 'user', message: 'Vegetarian' }); | |
handleResponse('vegetarian'); | |
}; | |
selectionBox.appendChild(vegButton); | |
const nonVegButton = document.createElement('button'); | |
nonVegButton.textContent = 'Non-Veg'; | |
nonVegButton.className = 'dietary-button red'; | |
nonVegButton.onclick = () => { | |
addMessage('user', 'Non-Vegetarian'); | |
conversation.push({ role: 'user', message: 'Non-Vegetarian' }); | |
handleResponse('non-vegetarian'); | |
}; | |
selectionBox.appendChild(nonVegButton); | |
const bothButton = document.createElement('button'); | |
bothButton.textContent = 'Both'; | |
bothButton.className = 'dietary-button gray'; | |
bothButton.onclick = () => { | |
addMessage('user', 'Both'); | |
conversation.push({ role: 'user', message: 'Both' }); | |
handleResponse('both'); | |
}; | |
selectionBox.appendChild(bothButton); | |
const label = document.createElement('span'); | |
label.textContent = 'Selected:'; | |
selectionBox.appendChild(label); | |
selectedItems.forEach((item, index) => { | |
const itemContainer = document.createElement('div'); | |
itemContainer.className = 'selected-item'; | |
itemContainer.dataset.hidden = item.source === 'Sector_Detail__c' ? 'true' : 'false'; | |
const img = document.createElement('img'); | |
img.src = item.image_url || 'https://via.placeholder.com/30'; | |
img.alt = item.name; | |
img.className = 'selected-item-image'; | |
itemContainer.appendChild(img); | |
const contentDiv = document.createElement('div'); | |
contentDiv.className = 'selected-item-content'; | |
const itemSpan = document.createElement('span'); | |
itemSpan.textContent = `${item.name} (Qty: ${item.quantity || 1})`; | |
contentDiv.appendChild(itemSpan); | |
if (item.source === 'Sector_Detail__c') { | |
const showButton = document.createElement('button'); | |
showButton.textContent = 'Show'; | |
showButton.className = 'show-button'; | |
showButton.onclick = () => toggleDescription(itemContainer, item.description, item.name); | |
contentDiv.appendChild(showButton); | |
} | |
itemContainer.appendChild(contentDiv); | |
const removeButton = document.createElement('button'); | |
removeButton.textContent = 'X'; | |
removeButton.className = 'remove-button'; | |
removeButton.onclick = () => { | |
selectedItems.splice(index, 1); | |
addMessage('bot', `Removed "${item.name}".`); | |
updateSelectionBox(); | |
}; | |
itemContainer.appendChild(removeButton); | |
selectionBox.appendChild(itemContainer); | |
}); | |
const textInput = document.createElement('input'); | |
textInput.type = 'text'; | |
textInput.placeholder = 'Add item...'; | |
textInput.className = 'manual-input'; | |
textInput.addEventListener('keypress', (e) => { | |
if (e.key === 'Enter' && textInput.value.trim()) { | |
const itemName = textInput.value.trim(); | |
fetchSectorItemDetails(itemName); | |
textInput.value = ''; | |
} | |
}); | |
selectionBox.appendChild(textInput); | |
if (selectedItems.length > 0) { | |
const quantityInput = document.createElement('input'); | |
quantityInput.type = 'number'; | |
quantityInput.min = '1'; | |
quantityInput.value = '1'; | |
quantityInput.placeholder = 'Qty'; | |
quantityInput.className = 'quantity-input'; | |
selectionBox.appendChild(quantityInput); | |
const submitButton = document.createElement('button'); | |
submitButton.textContent = 'Submit'; | |
submitButton.className = 'submit-button'; | |
submitButton.onclick = () => promptAndSubmit(quantityInput.value); | |
selectionBox.appendChild(submitButton); | |
const orderNameInput = document.createElement('input'); | |
orderNameInput.type = 'text'; | |
orderNameInput.placeholder = 'Order Name'; | |
orderNameInput.className = 'order-name-input'; | |
selectionBox.appendChild(orderNameInput); | |
} | |
chatMessages.appendChild(selectionBox); | |
chatMessages.scrollTop = chatMessages.scrollHeight; | |
console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category }))); | |
} | |
function fetchMenuItems(dietaryPreference = '', searchTerm = '') { | |
const payload = {}; | |
if (dietaryPreference) payload.dietary_preference = dietaryPreference; | |
if (searchTerm) payload.search_term = searchTerm; | |
fetch('/get_menu_items', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify(payload) | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
if (data.error) { | |
addMessage('bot', `Error: ${data.error}. Try again!`); | |
} else if (data.menu_items.length > 0) { | |
addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`); | |
displayItemsList(data.menu_items); | |
} else { | |
addMessage('bot', `No matches for "${searchTerm || dietaryPreference}". Try "paneer"!`); | |
} | |
console.log(`Fetched items for ${searchTerm || dietaryPreference}:`, data.menu_items); | |
}) | |
.catch(error => { | |
addMessage('bot', `Connection issue: ${error.message}. Retrying...`); | |
setTimeout(() => fetchMenuItems(dietaryPreference, searchTerm), 2000); | |
}); | |
} | |
function fetchSectorItemDetails(itemName) { | |
fetch('/get_sector_item_details', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ item_name: itemName }) | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
if (data.error) { | |
addMessage('bot', `No "${itemName}" found. Try another!`); | |
} else { | |
const details = data.item_details; | |
if (selectedItems.some(item => item.name === details.name)) { | |
addMessage('bot', `"${details.name}" already selected!`); | |
} else { | |
selectedItems.push({ ...details, quantity: 1 }); | |
addMessage('bot', `Added "${details.name}"!`); | |
updateSelectionBox(); | |
} | |
} | |
}) | |
.catch(error => { | |
addMessage('bot', `Error for "${itemName}". Retrying...`); | |
setTimeout(() => fetchSectorItemDetails(itemName), 2000); | |
}); | |
} | |
function toggleDescription(itemContainer, description, itemName) { | |
let descElement = itemContainer.querySelector('.item-description'); | |
if (!descElement) { | |
descElement = document.createElement('p'); | |
descElement.className = 'item-description'; | |
descElement.textContent = description; | |
itemContainer.querySelector('.selected-item-content').appendChild(descElement); | |
itemContainer.dataset.hidden = 'false'; | |
console.log(`Showed description for ${itemName}`); | |
} else { | |
descElement.remove(); | |
itemContainer.dataset.hidden = 'true'; | |
console.log(`Hid description for ${itemName}`); | |
} | |
} | |
function displayItemsList(items) { | |
const chatMessages = document.getElementById('chatMessages'); | |
if (!chatMessages) { | |
console.error('Chat messages container not found!'); | |
addMessage('bot', 'Display issue. Try again?'); | |
return; | |
} | |
const itemsGrid = document.createElement('div'); | |
itemsGrid.className = 'items-grid'; | |
items.forEach(item => { | |
const itemDiv = document.createElement('div'); | |
itemDiv.className = 'item-card'; | |
const img = document.createElement('img'); | |
img.src = item.image_url || 'https://via.placeholder.com/60'; | |
img.alt = item.name; | |
img.className = 'item-image'; | |
itemDiv.appendChild(img); | |
const contentDiv = document.createElement('div'); | |
contentDiv.className = 'item-content'; | |
const nameDiv = document.createElement('div'); | |
nameDiv.textContent = item.name; | |
nameDiv.className = 'item-name'; | |
contentDiv.appendChild(nameDiv); | |
const fields = [ | |
{ label: 'Price', value: item.price ? `$${item.price.toFixed(2)}` : 'N/A' }, | |
{ label: 'Veg/Non-Veg', value: item.veg_nonveg }, | |
{ label: 'Spice', value: item.spice_levels }, | |
{ label: 'Category', value: item.category }, | |
{ label: 'Ingredients', value: item.ingredients }, | |
{ label: 'Nutrition', value: item.nutritional_info }, | |
{ label: 'Sector', value: item.sector }, | |
{ label: 'Dynamic', value: item.dynamic_dish ? 'Yes' : 'No' } | |
]; | |
fields.forEach(field => { | |
if (field.value) { | |
const p = document.createElement('p'); | |
p.className = 'item-field'; | |
p.innerHTML = `<strong>${field.label}:</strong> ${field.value}`; | |
contentDiv.appendChild(p); | |
} | |
}); | |
itemDiv.appendChild(contentDiv); | |
const buttonContainer = document.createElement('div'); | |
buttonContainer.className = 'button-container'; | |
const addButton = document.createElement('button'); | |
addButton.textContent = 'Add'; | |
addButton.className = 'add-button'; | |
addButton.onclick = () => { | |
const selectedItem = { | |
name: item.name, | |
image_url: item.image_url || '', | |
category: item.category || 'Not specified', | |
description: item.description || 'No description available', | |
source: item.source, | |
quantity: 1, | |
ingredients: item.ingredients, | |
nutritional_info: item.nutritional_info, | |
price: item.price, | |
sector: item.sector, | |
spice_levels: item.spice_levels, | |
veg_nonveg: item.veg_nonveg, | |
dynamic_dish: item.dynamic_dish | |
}; | |
if (selectedItems.some(existing => existing.name === selectedItem.name)) { | |
addMessage('bot', `"${selectedItem.name}" already selected!`); | |
} else { | |
selectedItems.push(selectedItem); | |
addMessage('bot', `Added "${selectedItem.name}"!`); | |
updateSelectionBox(); | |
} | |
}; | |
buttonContainer.appendChild(addButton); | |
itemDiv.appendChild(buttonContainer); | |
itemsGrid.appendChild(itemDiv); | |
}); | |
chatMessages.appendChild(itemsGrid); | |
chatMessages.scrollTop = chatMessages.scrollHeight; | |
} | |
function displayOptions(options) { | |
const chatMessages = document.getElementById('chatMessages'); | |
if (!chatMessages) { | |
console.error('Chat messages container not found!'); | |
return; | |
} | |
const optionsDiv = document.createElement('div'); | |
optionsDiv.className = 'options-container'; | |
options.forEach(opt => { | |
const button = document.createElement('button'); | |
button.textContent = opt.text; | |
button.className = `option-button ${opt.class}`; | |
button.onclick = () => { | |
addMessage('user', opt.text); | |
conversation.push({ role: 'user', message: opt.text }); | |
selectionBoxVisible = true; | |
handleResponse(opt.text); | |
updateSelectionBox(); | |
}; | |
optionsDiv.appendChild(button); | |
}); | |
const backButton = document.createElement('button'); | |
backButton.textContent = 'Back'; | |
backButton.className = 'option-button'; | |
backButton.onclick = () => resetConversation(); | |
optionsDiv.appendChild(backButton); | |
chatMessages.appendChild(optionsDiv); | |
} | |
function promptAndSubmit(quantity) { | |
const orderNameInput = document.querySelector('.order-name-input'); | |
const customOrderName = orderNameInput ? orderNameInput.value.trim() : ''; | |
if (confirm(`Submit ${selectedItems.length} items (Qty: ${quantity})?`)) { | |
submitToSalesforce(customOrderName, quantity); | |
} else { | |
addMessage('bot', 'Cancelled. Add more items?'); | |
} | |
} | |
function submitToSalesforce(customOrderName, quantity) { | |
if (selectedItems.length === 0) { | |
addMessage('bot', 'No items selected! Add some dishes! 😊'); | |
return; | |
} | |
const itemsToSubmit = selectedItems.map(item => ({ | |
name: item.name, | |
category: item.category || 'Not specified', | |
description: item.description || 'No description available', | |
image_url: item.image_url || '', | |
quantity: parseInt(quantity) || 1 | |
})); | |
fetch('/submit_items', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ items: itemsToSubmit, custom_order_name: customOrderName }) | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
if (data.error) { | |
addMessage('bot', `Submission failed: ${data.error}. Try again?`); | |
} else { | |
addMessage('bot', `Submitted ${data.ingredient_name}! What's next?`); | |
selectedItems = []; | |
updateSelectionBox(); | |
} | |
}) | |
.catch(error => { | |
addMessage('bot', `Submission error: ${error.message}. Retrying...`); | |
setTimeout(() => submitToSalesforce(customOrderName, quantity), 2000); | |
}); | |
} | |
function resetConversation() { | |
const userName = conversation.length > 1 ? conversation[1].message : 'Friend'; | |
conversation = [ | |
{ role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` }, | |
{ role: 'user', message: userName }, | |
{ role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish or choose a preference below!` } | |
]; | |
selectedItems = []; | |
selectionBoxVisible = true; | |
const chatMessages = document.getElementById('chatMessages'); | |
chatMessages.innerHTML = ''; | |
conversation.forEach(msg => addMessage(msg.role, msg.message)); | |
displayOptions([ | |
{ text: 'Vegetarian', class: 'green' }, | |
{ text: 'Non-Vegetarian', class: 'red' }, | |
{ text: 'Both', class: 'gray' } | |
]); | |
updateSelectionBox(); | |
} | |
document.getElementById('userInput').addEventListener('keypress', (e) => { | |
if (e.key === 'Enter') sendMessage(); | |
}); | |
console.log('Chef Bot script loaded!'); |