Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +2 -271
static/script.js
CHANGED
@@ -1,9 +1,6 @@
|
|
1 |
let conversation = [
|
2 |
-
{ role: 'bot', message: "Hi there! I'm
|
3 |
];
|
4 |
-
let selectedIngredients = [];
|
5 |
-
let selectedMenuItem = null;
|
6 |
-
let cart = [];
|
7 |
|
8 |
function addMessage(role, message) {
|
9 |
const chatMessages = document.getElementById('chatMessages');
|
@@ -39,7 +36,6 @@ function sendMessage() {
|
|
39 |
function handleResponse(userInput) {
|
40 |
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
|
41 |
let botResponse = '';
|
42 |
-
let options = [];
|
43 |
|
44 |
// FAQ handling logic
|
45 |
if (lastMessage.includes("how do i contact customer support")) {
|
@@ -52,277 +48,12 @@ function handleResponse(userInput) {
|
|
52 |
botResponse = "You can cancel your subscription by visiting the 'Account Settings' page and selecting 'Cancel Subscription'.";
|
53 |
} else if (lastMessage.includes("are there any discounts available")) {
|
54 |
botResponse = "We offer seasonal discounts and promotions. Keep an eye on our website or subscribe to our newsletter for updates.";
|
55 |
-
}
|
56 |
-
// Custom meal preference handling
|
57 |
-
else if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
|
58 |
-
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
59 |
-
options = [
|
60 |
-
{ text: 'Vegetarian', class: 'green' },
|
61 |
-
{ text: 'Non-Vegetarian', class: 'red' }
|
62 |
-
];
|
63 |
-
} else if (lastMessage.includes('non-vegetarian')) {
|
64 |
-
console.log("Food preference selected: Non-Vegetarian");
|
65 |
-
botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
|
66 |
-
fetchIngredients('non-vegetarian');
|
67 |
-
return;
|
68 |
-
} else if (lastMessage.includes('vegetarian')) {
|
69 |
-
console.log("Food preference selected: Vegetarian");
|
70 |
-
botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
|
71 |
-
fetchIngredients('vegetarian');
|
72 |
-
return;
|
73 |
-
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
|
74 |
-
console.log(`Non-veg option selected: ${lastMessage}`);
|
75 |
-
botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
|
76 |
-
fetchIngredients(lastMessage.toLowerCase());
|
77 |
-
return;
|
78 |
-
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
79 |
-
botResponse = 'Here are some ingredients to customize your dish:';
|
80 |
-
fetchIngredients('both');
|
81 |
-
return;
|
82 |
-
} else if (lastMessage.includes('no') && selectedMenuItem) {
|
83 |
-
addToCart(selectedMenuItem);
|
84 |
-
botResponse = 'Item added to cart! Would you like to order more?';
|
85 |
-
options = [
|
86 |
-
{ text: 'Yes', class: 'green' },
|
87 |
-
{ text: 'No', class: 'red' }
|
88 |
-
];
|
89 |
-
selectedMenuItem = null;
|
90 |
} else {
|
91 |
// Fallback for unrecognized input
|
92 |
-
botResponse = "Sorry, I didn't understand that. Could you please clarify or
|
93 |
-
if (conversation.length === 2) {
|
94 |
-
options = [
|
95 |
-
{ text: 'Vegetarian', class: 'green' },
|
96 |
-
{ text: 'Non-Vegetarian', class: 'red' }
|
97 |
-
];
|
98 |
-
}
|
99 |
}
|
100 |
|
101 |
addMessage('bot', botResponse);
|
102 |
-
if (options.length > 0) {
|
103 |
-
displayOptions(options);
|
104 |
-
}
|
105 |
-
}
|
106 |
-
|
107 |
-
function fetchIngredients(foodPreference) {
|
108 |
-
console.log(`Fetching ingredients for dietary preference: ${foodPreference}`);
|
109 |
-
fetch('/get_ingredients', {
|
110 |
-
method: 'POST',
|
111 |
-
headers: {
|
112 |
-
'Content-Type': 'application/json',
|
113 |
-
},
|
114 |
-
body: JSON.stringify({ dietary_preference: foodPreference })
|
115 |
-
})
|
116 |
-
.then(response => {
|
117 |
-
console.log(`Response status: ${response.status}`);
|
118 |
-
if (!response.ok) {
|
119 |
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
120 |
-
}
|
121 |
-
return response.json();
|
122 |
-
})
|
123 |
-
.then(data => {
|
124 |
-
console.log('Response data:', data);
|
125 |
-
if (data.error) {
|
126 |
-
addMessage('bot', `Error fetching ingredients: ${data.error}`);
|
127 |
-
} else if (!data.ingredients || data.ingredients.length === 0) {
|
128 |
-
addMessage('bot', 'No ingredients found for this selection. Please try another option or check backend data.');
|
129 |
-
} else {
|
130 |
-
const ingredients = data.ingredients;
|
131 |
-
addMessage('bot', 'Please select ingredients:');
|
132 |
-
displayIngredientsList(ingredients);
|
133 |
-
displaySelectedIngredients();
|
134 |
-
}
|
135 |
-
})
|
136 |
-
.catch(error => {
|
137 |
-
console.error('Fetch error:', error);
|
138 |
-
addMessage('bot', `Failed to load ingredients: ${error.message}. Check console for details.`);
|
139 |
-
});
|
140 |
-
}
|
141 |
-
|
142 |
-
function displayOptions(options) {
|
143 |
-
const chatMessages = document.getElementById('chatMessages');
|
144 |
-
if (!chatMessages) {
|
145 |
-
console.error('Chat messages container not found for options!');
|
146 |
-
return;
|
147 |
-
}
|
148 |
-
options.forEach(opt => {
|
149 |
-
const button = document.createElement('button');
|
150 |
-
button.textContent = opt.text;
|
151 |
-
button.className = `option-button ${opt.class}`;
|
152 |
-
button.onclick = () => {
|
153 |
-
addMessage('user', opt.text);
|
154 |
-
conversation.push({ role: 'user', message: opt.text });
|
155 |
-
setTimeout(() => handleResponse(opt.text), 500);
|
156 |
-
};
|
157 |
-
chatMessages.appendChild(button);
|
158 |
-
});
|
159 |
-
chatMessages.appendChild(document.createElement('br'));
|
160 |
-
}
|
161 |
-
|
162 |
-
function displayIngredientsList(ingredients) {
|
163 |
-
const chatMessages = document.getElementById('chatMessages');
|
164 |
-
if (!chatMessages) {
|
165 |
-
console.error('Chat messages container not found for ingredients!');
|
166 |
-
return;
|
167 |
-
}
|
168 |
-
|
169 |
-
let ingredientsList = document.querySelector('.ingredients-list');
|
170 |
-
if (!ingredientsList) {
|
171 |
-
ingredientsList = document.createElement('div');
|
172 |
-
ingredientsList.className = 'ingredients-list';
|
173 |
-
chatMessages.appendChild(ingredientsList);
|
174 |
-
} else {
|
175 |
-
ingredientsList.innerHTML = '';
|
176 |
-
}
|
177 |
-
|
178 |
-
ingredients.forEach(ingredient => {
|
179 |
-
const item = document.createElement('div');
|
180 |
-
item.className = 'ingredient-item';
|
181 |
-
const img = document.createElement('img');
|
182 |
-
img.src = ingredient.image_url || 'https://via.placeholder.com/80';
|
183 |
-
img.alt = ingredient.name;
|
184 |
-
const name = document.createElement('div');
|
185 |
-
name.textContent = ingredient.name;
|
186 |
-
name.style.textAlign = 'center';
|
187 |
-
name.style.marginTop = '5px';
|
188 |
-
name.style.fontSize = '12px';
|
189 |
-
const button = document.createElement('button');
|
190 |
-
button.textContent = 'Add';
|
191 |
-
button.onclick = () => {
|
192 |
-
if (!selectedIngredients.some(item => item.name === ingredient.name)) {
|
193 |
-
selectedIngredients.push(ingredient);
|
194 |
-
displaySelectedIngredients();
|
195 |
-
}
|
196 |
-
};
|
197 |
-
item.appendChild(img);
|
198 |
-
item.appendChild(name);
|
199 |
-
item.appendChild(button);
|
200 |
-
ingredientsList.appendChild(item);
|
201 |
-
});
|
202 |
-
}
|
203 |
-
|
204 |
-
function displaySelectedIngredients() {
|
205 |
-
const chatMessages = document.getElementById('chatMessages');
|
206 |
-
if (!chatMessages) {
|
207 |
-
console.error('Chat messages container not found for selected ingredients!');
|
208 |
-
return;
|
209 |
-
}
|
210 |
-
|
211 |
-
let selectedArea = document.querySelector('.selected-ingredients');
|
212 |
-
if (!selectedArea) {
|
213 |
-
selectedArea = document.createElement('div');
|
214 |
-
selectedArea.className = 'selected-ingredients';
|
215 |
-
chatMessages.appendChild(selectedArea);
|
216 |
-
} else {
|
217 |
-
selectedArea.innerHTML = '';
|
218 |
-
}
|
219 |
-
|
220 |
-
selectedIngredients.forEach(ingredient => {
|
221 |
-
const div = document.createElement('div');
|
222 |
-
div.textContent = ingredient.name;
|
223 |
-
selectedArea.appendChild(div);
|
224 |
-
});
|
225 |
-
|
226 |
-
if (selectedIngredients.length > 0) {
|
227 |
-
let submitButton = document.querySelector('.submit-button');
|
228 |
-
if (!submitButton) {
|
229 |
-
submitButton = document.createElement('button');
|
230 |
-
submitButton.className = 'submit-button';
|
231 |
-
submitButton.textContent = 'Submit Ingredients';
|
232 |
-
submitButton.onclick = submitIngredients;
|
233 |
-
chatMessages.appendChild(submitButton);
|
234 |
-
}
|
235 |
-
}
|
236 |
-
}
|
237 |
-
|
238 |
-
function submitIngredients() {
|
239 |
-
fetch('/submit_ingredients', {
|
240 |
-
method: 'POST',
|
241 |
-
headers: {
|
242 |
-
'Content-Type': 'application/json',
|
243 |
-
},
|
244 |
-
body: JSON.stringify({ ingredients: selectedIngredients })
|
245 |
-
})
|
246 |
-
.then(response => response.json())
|
247 |
-
.then(data => {
|
248 |
-
if (data.success) {
|
249 |
-
const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
|
250 |
-
fetchMenuItems(ingredientNames);
|
251 |
-
} else {
|
252 |
-
addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
|
253 |
-
}
|
254 |
-
})
|
255 |
-
.catch(error => {
|
256 |
-
addMessage('bot', `Error: ${error.message}`);
|
257 |
-
});
|
258 |
-
}
|
259 |
-
|
260 |
-
function fetchMenuItems(ingredientNames) {
|
261 |
-
fetch('/get_menu_items', {
|
262 |
-
method: 'POST',
|
263 |
-
headers: {
|
264 |
-
'Content-Type': 'application/json',
|
265 |
-
},
|
266 |
-
body: JSON.stringify({ ingredient_names: ingredientNames })
|
267 |
-
})
|
268 |
-
.then(response => response.json())
|
269 |
-
.then(data => {
|
270 |
-
if (data.error) {
|
271 |
-
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
272 |
-
} else {
|
273 |
-
const menuItems = data.menu_items || [];
|
274 |
-
addMessage('bot', 'Here are some dishes based on your selected ingredients:');
|
275 |
-
displayMenuItems(menuItems);
|
276 |
-
}
|
277 |
-
})
|
278 |
-
.catch(error => {
|
279 |
-
addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
|
280 |
-
});
|
281 |
-
}
|
282 |
-
|
283 |
-
function displayMenuItems(menuItems) {
|
284 |
-
const chatMessages = document.getElementById('chatMessages');
|
285 |
-
if (!chatMessages) {
|
286 |
-
console.error('Chat messages container not found for menu items!');
|
287 |
-
return;
|
288 |
-
}
|
289 |
-
|
290 |
-
let menuItemsList = document.querySelector('.menu-items-list');
|
291 |
-
if (!menuItemsList) {
|
292 |
-
menuItemsList = document.createElement('div');
|
293 |
-
menuItemsList.className = 'menu-items-list';
|
294 |
-
chatMessages.appendChild(menuItemsList);
|
295 |
-
} else {
|
296 |
-
menuItemsList.innerHTML = '';
|
297 |
-
}
|
298 |
-
|
299 |
-
menuItems.forEach(item => {
|
300 |
-
const menuItem = document.createElement('div');
|
301 |
-
menuItem.className = 'menu-item';
|
302 |
-
const img = document.createElement('img');
|
303 |
-
img.src = item.image_url || 'https://via.placeholder.com/80';
|
304 |
-
img.alt = item.name;
|
305 |
-
const name = document.createElement('div');
|
306 |
-
name.textContent = item.name;
|
307 |
-
name.style.textAlign = 'center';
|
308 |
-
name.style.marginTop = '5px';
|
309 |
-
name.style.fontSize = '12px';
|
310 |
-
const button = document.createElement('button');
|
311 |
-
button.textContent = 'Add to Cart';
|
312 |
-
button.onclick = () => {
|
313 |
-
selectedMenuItem = item;
|
314 |
-
addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
|
315 |
-
const options = [
|
316 |
-
{ text: 'Yes', class: 'green' },
|
317 |
-
{ text: 'No', class: 'red' }
|
318 |
-
];
|
319 |
-
displayOptions(options);
|
320 |
-
};
|
321 |
-
menuItem.appendChild(img);
|
322 |
-
menuItem.appendChild(name);
|
323 |
-
menuItem.appendChild(button);
|
324 |
-
menuItemsList.appendChild(menuItem);
|
325 |
-
});
|
326 |
}
|
327 |
|
328 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
|
|
1 |
let conversation = [
|
2 |
+
{ role: 'bot', message: "Hi there! I'm your assistant! How can I help you today?" }
|
3 |
];
|
|
|
|
|
|
|
4 |
|
5 |
function addMessage(role, message) {
|
6 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
36 |
function handleResponse(userInput) {
|
37 |
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
|
38 |
let botResponse = '';
|
|
|
39 |
|
40 |
// FAQ handling logic
|
41 |
if (lastMessage.includes("how do i contact customer support")) {
|
|
|
48 |
botResponse = "You can cancel your subscription by visiting the 'Account Settings' page and selecting 'Cancel Subscription'.";
|
49 |
} else if (lastMessage.includes("are there any discounts available")) {
|
50 |
botResponse = "We offer seasonal discounts and promotions. Keep an eye on our website or subscribe to our newsletter for updates.";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
} else {
|
52 |
// Fallback for unrecognized input
|
53 |
+
botResponse = "Sorry, I didn't understand that. Could you please clarify or ask a different question?";
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
|
56 |
addMessage('bot', botResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
|
59 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|