Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +118 -80
static/script.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
let conversation = [
|
2 |
{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
|
3 |
];
|
4 |
-
let
|
5 |
|
6 |
function addMessage(role, message) {
|
7 |
const chatMessages = document.getElementById('chatMessages');
|
@@ -35,20 +35,28 @@ function sendMessage() {
|
|
35 |
}
|
36 |
|
37 |
function handleResponse(userInput) {
|
38 |
-
const
|
39 |
let botResponse = '';
|
40 |
let options = [];
|
41 |
|
42 |
-
|
|
|
43 |
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
44 |
options = [
|
45 |
{ text: 'Vegetarian', class: 'green' },
|
46 |
{ text: 'Non-Vegetarian', class: 'red' },
|
47 |
{ text: 'Both', class: 'gray' }
|
48 |
];
|
49 |
-
}
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
return;
|
53 |
}
|
54 |
|
@@ -58,104 +66,132 @@ function handleResponse(userInput) {
|
|
58 |
}
|
59 |
}
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
method: 'POST',
|
64 |
-
headers: {
|
65 |
-
|
66 |
-
},
|
67 |
-
body: JSON.stringify({ dietary_preference: dietaryPreference })
|
68 |
})
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
})
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
}
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
return;
|
91 |
}
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
ingredientsList.className = 'ingredients-list';
|
97 |
-
chatMessages.appendChild(ingredientsList);
|
98 |
-
} else {
|
99 |
-
ingredientsList.innerHTML = '';
|
100 |
-
}
|
101 |
-
|
102 |
-
ingredients.forEach(ingredient => {
|
103 |
-
const item = document.createElement('div');
|
104 |
-
item.className = 'ingredient-item';
|
105 |
const img = document.createElement('img');
|
106 |
-
img.src =
|
107 |
-
img.alt =
|
108 |
const name = document.createElement('div');
|
109 |
-
name.textContent =
|
110 |
name.style.textAlign = 'center';
|
111 |
name.style.marginTop = '5px';
|
112 |
name.style.fontSize = '12px';
|
113 |
const button = document.createElement('button');
|
114 |
button.textContent = 'Add';
|
115 |
-
button.onclick = () =>
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
};
|
121 |
-
item.appendChild(img);
|
122 |
-
item.appendChild(name);
|
123 |
-
item.appendChild(button);
|
124 |
-
ingredientsList.appendChild(item);
|
125 |
});
|
126 |
}
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
console.error('Chat messages container not found for selected ingredients!');
|
132 |
-
return;
|
133 |
-
}
|
134 |
-
|
135 |
-
let selectedArea = document.querySelector('.selected-ingredients');
|
136 |
if (!selectedArea) {
|
137 |
-
|
138 |
-
|
139 |
-
chatMessages.appendChild(selectedArea);
|
140 |
-
} else {
|
141 |
-
selectedArea.innerHTML = '';
|
142 |
}
|
|
|
143 |
|
144 |
-
|
145 |
const div = document.createElement('div');
|
146 |
-
div.
|
147 |
selectedArea.appendChild(div);
|
148 |
});
|
149 |
}
|
150 |
|
|
|
151 |
function displayOptions(options) {
|
152 |
const chatMessages = document.getElementById('chatMessages');
|
153 |
if (!chatMessages) {
|
154 |
console.error('Chat messages container not found for options!');
|
155 |
return;
|
156 |
}
|
157 |
-
// Clear existing content
|
158 |
chatMessages.innerHTML = '';
|
|
|
159 |
options.forEach(opt => {
|
160 |
const button = document.createElement('button');
|
161 |
button.textContent = opt.text;
|
@@ -163,19 +199,23 @@ function displayOptions(options) {
|
|
163 |
button.onclick = () => {
|
164 |
addMessage('user', opt.text);
|
165 |
conversation.push({ role: 'user', message: opt.text });
|
166 |
-
handleResponse(opt.text);
|
167 |
};
|
168 |
chatMessages.appendChild(button);
|
169 |
});
|
|
|
170 |
chatMessages.appendChild(document.createElement('br'));
|
171 |
const backButton = document.createElement('button');
|
172 |
backButton.textContent = 'Go Back';
|
173 |
backButton.className = 'option-button';
|
174 |
backButton.onclick = () => {
|
175 |
-
// Reset conversation to initial state
|
176 |
const userName = conversation.length > 1 ? conversation[1].message : 'User';
|
177 |
-
conversation = [
|
178 |
-
|
|
|
|
|
|
|
|
|
179 |
chatMessages.innerHTML = '';
|
180 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
181 |
displayOptions([
|
@@ -187,11 +227,9 @@ function displayOptions(options) {
|
|
187 |
chatMessages.appendChild(backButton);
|
188 |
}
|
189 |
|
190 |
-
//
|
191 |
-
document.getElementById('userInput').addEventListener('keypress',
|
192 |
-
if (e.key === 'Enter')
|
193 |
-
sendMessage();
|
194 |
-
}
|
195 |
});
|
196 |
|
197 |
// Initial load check
|
|
|
1 |
let conversation = [
|
2 |
{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
|
3 |
];
|
4 |
+
let selectedItems = [];
|
5 |
|
6 |
function addMessage(role, message) {
|
7 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
35 |
}
|
36 |
|
37 |
function handleResponse(userInput) {
|
38 |
+
const lowerInput = userInput.toLowerCase();
|
39 |
let botResponse = '';
|
40 |
let options = [];
|
41 |
|
42 |
+
// First message: Ask for dietary preference after name
|
43 |
+
if (conversation.length === 2) {
|
44 |
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
45 |
options = [
|
46 |
{ text: 'Vegetarian', class: 'green' },
|
47 |
{ text: 'Non-Vegetarian', class: 'red' },
|
48 |
{ text: 'Both', class: 'gray' }
|
49 |
];
|
50 |
+
}
|
51 |
+
// After dietary preference, fetch menu items
|
52 |
+
else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
53 |
+
botResponse = `Great choice! 🍽️ Here are some menu items for ${lowerInput}:`;
|
54 |
+
fetchMenuItems(lowerInput); // Fetch from Salesforce based on preference
|
55 |
+
return;
|
56 |
+
}
|
57 |
+
// Otherwise, treat input as a search term for suggestions
|
58 |
+
else {
|
59 |
+
suggestItems(lowerInput); // Fetch suggestions dynamically
|
60 |
return;
|
61 |
}
|
62 |
|
|
|
66 |
}
|
67 |
}
|
68 |
|
69 |
+
// Fetch menu items from Salesforce
|
70 |
+
function fetchMenuItems(dietaryPreference) {
|
71 |
+
fetch('/get_menu_items', { method: 'GET' })
|
72 |
+
.then(response => response.json())
|
73 |
+
.then(data => {
|
74 |
+
if (data.error) {
|
75 |
+
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
76 |
+
} else {
|
77 |
+
const filteredItems = data.menu_items.filter(item => {
|
78 |
+
if (dietaryPreference === 'both') return true;
|
79 |
+
// Assuming Salesforce has a Category__c field for filtering
|
80 |
+
return dietaryPreference === 'vegetarian' ? item.name.toLowerCase().includes('veg') : !item.name.toLowerCase().includes('veg');
|
81 |
+
});
|
82 |
+
addMessage('bot', 'Here are some available menu items:');
|
83 |
+
displayItemsList(filteredItems, 'menuItemsList');
|
84 |
+
console.log(`Menu items fetched for ${dietaryPreference}:`, filteredItems);
|
85 |
+
}
|
86 |
+
})
|
87 |
+
.catch(error => {
|
88 |
+
addMessage('bot', `Error: Unable to connect to the database. ${error.message}`);
|
89 |
+
});
|
90 |
+
}
|
91 |
+
|
92 |
+
// Fetch suggestions from Salesforce based on user input
|
93 |
+
function suggestItems(searchTerm) {
|
94 |
+
fetch('/suggest_items', {
|
95 |
method: 'POST',
|
96 |
+
headers: { 'Content-Type': 'application/json' },
|
97 |
+
body: JSON.stringify({ search_term: searchTerm })
|
|
|
|
|
98 |
})
|
99 |
+
.then(response => response.json())
|
100 |
+
.then(data => {
|
101 |
+
if (data.error) {
|
102 |
+
addMessage('bot', `Sorry, I couldn’t find anything for "${searchTerm}": ${data.error}`);
|
103 |
+
} else if (data.suggestions.length > 0) {
|
104 |
+
addMessage('bot', `Here are some suggestions for "${searchTerm}":`);
|
105 |
+
displayItemsList(data.suggestions, 'suggestionsList');
|
106 |
+
console.log(`Suggestions fetched for ${searchTerm}:`, data.suggestions);
|
107 |
+
} else {
|
108 |
+
addMessage('bot', `No suggestions found for "${searchTerm}". Try something else!`);
|
109 |
+
}
|
110 |
+
})
|
111 |
+
.catch(error => {
|
112 |
+
addMessage('bot', `Error fetching suggestions: ${error.message}`);
|
113 |
+
});
|
114 |
+
}
|
115 |
+
|
116 |
+
// Fetch item details from Salesforce
|
117 |
+
function fetchItemDetails(itemName) {
|
118 |
+
fetch('/get_item_details', {
|
119 |
+
method: 'POST',
|
120 |
+
headers: { 'Content-Type': 'application/json' },
|
121 |
+
body: JSON.stringify({ item_name: itemName })
|
122 |
})
|
123 |
+
.then(response => response.json())
|
124 |
+
.then(data => {
|
125 |
+
if (data.error) {
|
126 |
+
addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}`);
|
127 |
+
} else {
|
128 |
+
const details = data.item_details;
|
129 |
+
selectedItems.push(details);
|
130 |
+
addMessage('bot', `Added "${itemName}" to your selection. Here are the details:`);
|
131 |
+
displaySelectedItems();
|
132 |
+
console.log(`Details fetched for ${itemName}:`, details);
|
133 |
+
}
|
134 |
+
})
|
135 |
+
.catch(error => {
|
136 |
+
addMessage('bot', `Error fetching details: ${error.message}`);
|
137 |
+
});
|
138 |
}
|
139 |
|
140 |
+
// Display items (menu items or suggestions)
|
141 |
+
function displayItemsList(items, containerId) {
|
142 |
+
const container = document.getElementById(containerId);
|
143 |
+
if (!container) {
|
144 |
+
console.error(`${containerId} container not found!`);
|
145 |
return;
|
146 |
}
|
147 |
+
container.innerHTML = '';
|
148 |
|
149 |
+
items.forEach(item => {
|
150 |
+
const itemDiv = document.createElement('div');
|
151 |
+
itemDiv.className = containerId === 'menuItemsList' ? 'ingredient-item' : 'suggestion-item';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
const img = document.createElement('img');
|
153 |
+
img.src = item.image_url || 'https://via.placeholder.com/60';
|
154 |
+
img.alt = item.name;
|
155 |
const name = document.createElement('div');
|
156 |
+
name.textContent = item.name;
|
157 |
name.style.textAlign = 'center';
|
158 |
name.style.marginTop = '5px';
|
159 |
name.style.fontSize = '12px';
|
160 |
const button = document.createElement('button');
|
161 |
button.textContent = 'Add';
|
162 |
+
button.onclick = () => fetchItemDetails(item.name);
|
163 |
+
itemDiv.appendChild(img);
|
164 |
+
itemDiv.appendChild(name);
|
165 |
+
itemDiv.appendChild(button);
|
166 |
+
container.appendChild(itemDiv);
|
|
|
|
|
|
|
|
|
|
|
167 |
});
|
168 |
}
|
169 |
|
170 |
+
// Display selected items with details
|
171 |
+
function displaySelectedItems() {
|
172 |
+
const selectedArea = document.getElementById('itemDetails');
|
|
|
|
|
|
|
|
|
|
|
173 |
if (!selectedArea) {
|
174 |
+
console.error('Item details container not found!');
|
175 |
+
return;
|
|
|
|
|
|
|
176 |
}
|
177 |
+
selectedArea.innerHTML = '';
|
178 |
|
179 |
+
selectedItems.forEach(item => {
|
180 |
const div = document.createElement('div');
|
181 |
+
div.innerHTML = `<strong>${item.name}</strong><br>Description: ${item.description}<br>Ingredients: ${item.ingredients}`;
|
182 |
selectedArea.appendChild(div);
|
183 |
});
|
184 |
}
|
185 |
|
186 |
+
// Display options (e.g., Vegetarian, Non-Vegetarian, Both)
|
187 |
function displayOptions(options) {
|
188 |
const chatMessages = document.getElementById('chatMessages');
|
189 |
if (!chatMessages) {
|
190 |
console.error('Chat messages container not found for options!');
|
191 |
return;
|
192 |
}
|
|
|
193 |
chatMessages.innerHTML = '';
|
194 |
+
|
195 |
options.forEach(opt => {
|
196 |
const button = document.createElement('button');
|
197 |
button.textContent = opt.text;
|
|
|
199 |
button.onclick = () => {
|
200 |
addMessage('user', opt.text);
|
201 |
conversation.push({ role: 'user', message: opt.text });
|
202 |
+
handleResponse(opt.text);
|
203 |
};
|
204 |
chatMessages.appendChild(button);
|
205 |
});
|
206 |
+
|
207 |
chatMessages.appendChild(document.createElement('br'));
|
208 |
const backButton = document.createElement('button');
|
209 |
backButton.textContent = 'Go Back';
|
210 |
backButton.className = 'option-button';
|
211 |
backButton.onclick = () => {
|
|
|
212 |
const userName = conversation.length > 1 ? conversation[1].message : 'User';
|
213 |
+
conversation = [
|
214 |
+
{ role: 'bot', message: `Hi there! I'm Chef Bot! May I know your name?` },
|
215 |
+
{ role: 'user', message: userName },
|
216 |
+
{ role: 'bot', message: `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?` }
|
217 |
+
];
|
218 |
+
selectedItems = [];
|
219 |
chatMessages.innerHTML = '';
|
220 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
221 |
displayOptions([
|
|
|
227 |
chatMessages.appendChild(backButton);
|
228 |
}
|
229 |
|
230 |
+
// Event listeners
|
231 |
+
document.getElementById('userInput').addEventListener('keypress', (e) => {
|
232 |
+
if (e.key === 'Enter') sendMessage();
|
|
|
|
|
233 |
});
|
234 |
|
235 |
// Initial load check
|