Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +85 -60
static/script.js
CHANGED
@@ -30,40 +30,39 @@ function sendMessage() {
|
|
30 |
userInput.value = '';
|
31 |
setTimeout(() => handleResponse(message), 500);
|
32 |
} else {
|
33 |
-
|
34 |
}
|
35 |
}
|
36 |
|
37 |
function handleResponse(userInput) {
|
38 |
const lowerInput = userInput.toLowerCase();
|
39 |
let botResponse = '';
|
40 |
-
let options = [];
|
41 |
|
42 |
-
//
|
43 |
if (conversation.length === 2) {
|
44 |
-
botResponse = `Nice to meet you, ${userInput}! 😊
|
45 |
-
|
46 |
{ text: 'Vegetarian', class: 'green' },
|
47 |
{ text: 'Non-Vegetarian', class: 'red' },
|
48 |
{ text: 'Both', class: 'gray' }
|
49 |
-
];
|
50 |
}
|
51 |
-
// After dietary preference
|
52 |
else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
53 |
-
botResponse = `
|
54 |
-
|
|
|
55 |
return;
|
56 |
}
|
57 |
-
//
|
58 |
else {
|
59 |
-
|
|
|
|
|
60 |
return;
|
61 |
}
|
62 |
|
63 |
addMessage('bot', botResponse);
|
64 |
-
if (options.length > 0) {
|
65 |
-
displayOptions(options);
|
66 |
-
}
|
67 |
}
|
68 |
|
69 |
// Fetch menu items from Salesforce
|
@@ -72,24 +71,31 @@ function fetchMenuItems(dietaryPreference) {
|
|
72 |
.then(response => response.json())
|
73 |
.then(data => {
|
74 |
if (data.error) {
|
75 |
-
addMessage('bot', `
|
76 |
} else {
|
77 |
const filteredItems = data.menu_items.filter(item => {
|
78 |
if (dietaryPreference === 'both') return true;
|
79 |
-
//
|
80 |
-
return dietaryPreference === 'vegetarian'
|
|
|
|
|
81 |
});
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
}
|
86 |
})
|
87 |
.catch(error => {
|
88 |
-
addMessage('bot', `Error
|
|
|
89 |
});
|
90 |
}
|
91 |
|
92 |
-
// Fetch suggestions from Salesforce
|
93 |
function suggestItems(searchTerm) {
|
94 |
fetch('/suggest_items', {
|
95 |
method: 'POST',
|
@@ -99,21 +105,22 @@ function suggestItems(searchTerm) {
|
|
99 |
.then(response => response.json())
|
100 |
.then(data => {
|
101 |
if (data.error) {
|
102 |
-
addMessage('bot', `
|
103 |
} else if (data.suggestions.length > 0) {
|
104 |
-
addMessage('bot', `
|
105 |
displayItemsList(data.suggestions, 'suggestionsList');
|
106 |
-
console.log(`Suggestions fetched for ${searchTerm}:`, data.suggestions);
|
107 |
} else {
|
108 |
-
addMessage('bot', `No
|
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',
|
@@ -123,17 +130,18 @@ function fetchItemDetails(itemName) {
|
|
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', `
|
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 |
|
@@ -142,6 +150,7 @@ 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 = '';
|
@@ -172,25 +181,31 @@ 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.
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
184 |
}
|
185 |
|
186 |
-
// Display options (
|
187 |
function displayOptions(options) {
|
188 |
-
const chatMessages = document.
|
189 |
if (!chatMessages) {
|
190 |
console.error('Chat messages container not found for options!');
|
191 |
return;
|
192 |
}
|
193 |
-
|
|
|
194 |
|
195 |
options.forEach(opt => {
|
196 |
const button = document.createElement('button');
|
@@ -201,30 +216,40 @@ function displayOptions(options) {
|
|
201 |
conversation.push({ role: 'user', message: opt.text });
|
202 |
handleResponse(opt.text);
|
203 |
};
|
204 |
-
|
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.
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
}
|
229 |
|
230 |
// Event listeners
|
@@ -232,5 +257,5 @@ document.getElementById('userInput').addEventListener('keypress', (e) => {
|
|
232 |
if (e.key === 'Enter') sendMessage();
|
233 |
});
|
234 |
|
235 |
-
// Initial load
|
236 |
-
console.log('
|
|
|
30 |
userInput.value = '';
|
31 |
setTimeout(() => handleResponse(message), 500);
|
32 |
} else {
|
33 |
+
addMessage('bot', 'Please type something to continue! 😊');
|
34 |
}
|
35 |
}
|
36 |
|
37 |
function handleResponse(userInput) {
|
38 |
const lowerInput = userInput.toLowerCase();
|
39 |
let botResponse = '';
|
|
|
40 |
|
41 |
+
// Step 1: After name input
|
42 |
if (conversation.length === 2) {
|
43 |
+
botResponse = `Nice to meet you, ${userInput}! 😊 What type of food would you like to explore today?`;
|
44 |
+
displayOptions([
|
45 |
{ text: 'Vegetarian', class: 'green' },
|
46 |
{ text: 'Non-Vegetarian', class: 'red' },
|
47 |
{ text: 'Both', class: 'gray' }
|
48 |
+
]);
|
49 |
}
|
50 |
+
// Step 2: After dietary preference
|
51 |
else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
52 |
+
botResponse = `Awesome! Let me fetch some ${lowerInput} menu items for you...`;
|
53 |
+
addMessage('bot', botResponse);
|
54 |
+
fetchMenuItems(lowerInput);
|
55 |
return;
|
56 |
}
|
57 |
+
// Step 3: Search for specific items
|
58 |
else {
|
59 |
+
botResponse = `Looking for something with "${userInput}"...`;
|
60 |
+
addMessage('bot', botResponse);
|
61 |
+
suggestItems(userInput);
|
62 |
return;
|
63 |
}
|
64 |
|
65 |
addMessage('bot', botResponse);
|
|
|
|
|
|
|
66 |
}
|
67 |
|
68 |
// Fetch menu items from Salesforce
|
|
|
71 |
.then(response => response.json())
|
72 |
.then(data => {
|
73 |
if (data.error) {
|
74 |
+
addMessage('bot', `Oops! Couldn’t fetch menu items: ${data.error}. Try again?`);
|
75 |
} else {
|
76 |
const filteredItems = data.menu_items.filter(item => {
|
77 |
if (dietaryPreference === 'both') return true;
|
78 |
+
// Client-side filtering (update if Salesforce supports this)
|
79 |
+
return dietaryPreference === 'vegetarian'
|
80 |
+
? item.name.toLowerCase().includes('veg')
|
81 |
+
: !item.name.toLowerCase().includes('veg');
|
82 |
});
|
83 |
+
if (filteredItems.length > 0) {
|
84 |
+
addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
|
85 |
+
displayItemsList(filteredItems, 'menuItemsList');
|
86 |
+
} else {
|
87 |
+
addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`);
|
88 |
+
}
|
89 |
+
console.log(`Fetched menu items for ${dietaryPreference}:`, filteredItems);
|
90 |
}
|
91 |
})
|
92 |
.catch(error => {
|
93 |
+
addMessage('bot', `Error connecting to the database: ${error.message}. I’ll try again soon!`);
|
94 |
+
setTimeout(() => fetchMenuItems(dietaryPreference), 2000); // Retry after 2 seconds
|
95 |
});
|
96 |
}
|
97 |
|
98 |
+
// Fetch suggestions from Salesforce
|
99 |
function suggestItems(searchTerm) {
|
100 |
fetch('/suggest_items', {
|
101 |
method: 'POST',
|
|
|
105 |
.then(response => response.json())
|
106 |
.then(data => {
|
107 |
if (data.error) {
|
108 |
+
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Try another term?`);
|
109 |
} else if (data.suggestions.length > 0) {
|
110 |
+
addMessage('bot', `Found these suggestions for "${searchTerm}":`);
|
111 |
displayItemsList(data.suggestions, 'suggestionsList');
|
|
|
112 |
} else {
|
113 |
+
addMessage('bot', `No matches for "${searchTerm}". Maybe try "chicken" or "rice"?`);
|
114 |
}
|
115 |
+
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
116 |
})
|
117 |
.catch(error => {
|
118 |
+
addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
|
119 |
+
setTimeout(() => suggestItems(searchTerm), 2000); // Retry after 2 seconds
|
120 |
});
|
121 |
}
|
122 |
|
123 |
+
// Fetch item details from Salesforce with improved error handling
|
124 |
function fetchItemDetails(itemName) {
|
125 |
fetch('/get_item_details', {
|
126 |
method: 'POST',
|
|
|
130 |
.then(response => response.json())
|
131 |
.then(data => {
|
132 |
if (data.error) {
|
133 |
+
addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Does it exist in our menu?`);
|
134 |
} else {
|
135 |
const details = data.item_details;
|
136 |
selectedItems.push(details);
|
137 |
+
addMessage('bot', `Here’s what I found for "${itemName}":`);
|
138 |
displaySelectedItems();
|
139 |
console.log(`Details fetched for ${itemName}:`, details);
|
140 |
}
|
141 |
})
|
142 |
.catch(error => {
|
143 |
+
addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. I’ll retry...`);
|
144 |
+
setTimeout(() => fetchItemDetails(itemName), 2000); // Retry after 2 seconds
|
145 |
});
|
146 |
}
|
147 |
|
|
|
150 |
const container = document.getElementById(containerId);
|
151 |
if (!container) {
|
152 |
console.error(`${containerId} container not found!`);
|
153 |
+
addMessage('bot', 'Something went wrong with the display. Please try again!');
|
154 |
return;
|
155 |
}
|
156 |
container.innerHTML = '';
|
|
|
181 |
const selectedArea = document.getElementById('itemDetails');
|
182 |
if (!selectedArea) {
|
183 |
console.error('Item details container not found!');
|
184 |
+
addMessage('bot', 'Can’t show details right now. Try again later!');
|
185 |
return;
|
186 |
}
|
187 |
selectedArea.innerHTML = '';
|
188 |
|
189 |
+
if (selectedItems.length === 0) {
|
190 |
+
selectedArea.innerHTML = '<div>No items selected yet!</div>';
|
191 |
+
} else {
|
192 |
+
selectedItems.forEach(item => {
|
193 |
+
const div = document.createElement('div');
|
194 |
+
div.innerHTML = `<strong>${item.name}</strong><br>Description: ${item.description}<br>Ingredients: ${item.ingredients}`;
|
195 |
+
selectedArea.appendChild(div);
|
196 |
+
});
|
197 |
+
}
|
198 |
}
|
199 |
|
200 |
+
// Display options (Vegetarian, Non-Vegetarian, Both)
|
201 |
function displayOptions(options) {
|
202 |
+
const chatMessages = document.getElementId('chatMessages');
|
203 |
if (!chatMessages) {
|
204 |
console.error('Chat messages container not found for options!');
|
205 |
return;
|
206 |
}
|
207 |
+
const optionsDiv = document.createElement('div');
|
208 |
+
optionsDiv.style.marginTop = '10px';
|
209 |
|
210 |
options.forEach(opt => {
|
211 |
const button = document.createElement('button');
|
|
|
216 |
conversation.push({ role: 'user', message: opt.text });
|
217 |
handleResponse(opt.text);
|
218 |
};
|
219 |
+
optionsDiv.appendChild(button);
|
220 |
});
|
221 |
|
|
|
222 |
const backButton = document.createElement('button');
|
223 |
backButton.textContent = 'Go Back';
|
224 |
backButton.className = 'option-button';
|
225 |
+
backButton.style.marginTop = '10px';
|
226 |
+
backButton.onclick = () => resetConversation();
|
227 |
+
optionsDiv.appendChild(document.createElement('br'));
|
228 |
+
optionsDiv.appendChild(backButton);
|
229 |
+
|
230 |
+
chatMessages.appendChild(optionsDiv);
|
231 |
+
}
|
232 |
+
|
233 |
+
// Reset conversation
|
234 |
+
function resetConversation() {
|
235 |
+
const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
|
236 |
+
conversation = [
|
237 |
+
{ role: 'bot', message: `Hi there! I'm Chef Bot! May I know your name?` },
|
238 |
+
{ role: 'user', message: userName },
|
239 |
+
{ role: 'bot', message: `Nice to meet you, ${userName}! 😊 What type of food would you like to explore today?` }
|
240 |
+
];
|
241 |
+
selectedItems = [];
|
242 |
+
const chatMessages = document.getElementById('chatMessages');
|
243 |
+
chatMessages.innerHTML = '';
|
244 |
+
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
245 |
+
displayOptions([
|
246 |
+
{ text: 'Vegetarian', class: 'green' },
|
247 |
+
{ text: 'Non-Vegetarian', class: 'red' },
|
248 |
+
{ text: 'Both', class: 'gray' }
|
249 |
+
]);
|
250 |
+
document.getElementById('suggestionsList').innerHTML = '';
|
251 |
+
document.getElementById('menuItemsList').innerHTML = '';
|
252 |
+
displaySelectedItems();
|
253 |
}
|
254 |
|
255 |
// Event listeners
|
|
|
257 |
if (e.key === 'Enter') sendMessage();
|
258 |
});
|
259 |
|
260 |
+
// Initial load
|
261 |
+
console.log('Chef Bot script loaded successfully!');
|