geethareddy commited on
Commit
b0cbd7d
·
verified ·
1 Parent(s): 3c0a0ce

Update templates/menu_page.html

Browse files
Files changed (1) hide show
  1. templates/menu_page.html +104 -33
templates/menu_page.html CHANGED
@@ -91,48 +91,119 @@
91
  </div>
92
 
93
  <script>
94
- const listenButton = document.getElementById("listen-btn");
95
- const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
96
-
97
- recognition.lang = "en-US";
98
- recognition.interimResults = false;
99
-
100
- function speak(text) {
101
- const msg = new SpeechSynthesisUtterance(text);
102
- window.speechSynthesis.speak(msg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  }
104
-
105
- listenButton.addEventListener("click", () => {
106
- speak("Please say the item you want to order.");
107
- recognition.start();
 
 
 
 
108
  });
109
-
 
110
  recognition.onresult = (event) => {
111
- const command = event.results[0][0].transcript.toLowerCase();
112
- console.log("User said:", command);
 
 
 
 
 
 
 
 
 
 
113
 
114
- // Add logic to recognize specific items or trigger actions based on the command
115
- if (command.includes("order")) {
116
- // For example, navigate or process an order (customize as needed)
117
- speak("Your order has been placed.");
 
 
 
 
 
 
118
  }
119
  };
 
120
 
121
- recognition.onerror = (event) => {
122
- console.error("Speech recognition error:", event.error);
123
- speak("Sorry, I couldn't understand that. Please try again.");
 
 
 
 
 
 
 
 
 
124
  };
 
125
 
126
- // Voice reading of menu items on page load
127
- window.onload = function() {
128
- const menuItems = document.querySelectorAll('.menu-item h3');
129
- let itemsText = "The menu includes: ";
130
- menuItems.forEach(item => {
131
- itemsText += item.textContent + ", ";
132
- });
133
- speak(itemsText); // Speak all the item names
134
- }
135
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
 
 
 
 
 
137
  </body>
138
  </html>
 
91
  </div>
92
 
93
  <script>
94
+ const listenButton = document.getElementById("listen-btn");
95
+ const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
96
+ recognition.lang = "en-US";
97
+ recognition.interimResults = false;
98
+
99
+ let cart = []; // To store the cart items
100
+
101
+ function speak(text) {
102
+ const msg = new SpeechSynthesisUtterance(text);
103
+ window.speechSynthesis.speak(msg);
104
+ }
105
+
106
+ listenButton.addEventListener("click", () => {
107
+ speak("Please say the category you want to explore, such as Appetizer or Main Course.");
108
+ recognition.start();
109
+ });
110
+
111
+ recognition.onresult = (event) => {
112
+ const command = event.results[0][0].transcript.toLowerCase();
113
+ console.log("User said:", command);
114
+
115
+ if (command.includes("appetizer") || command.includes("main course")) {
116
+ // Identify the category and filter items (for demo, we simply proceed with a generic response)
117
+ const category = command.includes("appetizer") ? "Appetizer" : "Main Course";
118
+ speak(`You selected ${category}. What would you like to order from the ${category} menu?`);
119
+ askForItem(category);
120
+ } else {
121
+ speak("Sorry, I couldn't recognize that. Please say 'Appetizer' or 'Main Course'.");
122
  }
123
+ };
124
+
125
+ function askForItem(category) {
126
+ // Get all items from the selected category
127
+ const items = document.querySelectorAll(`.menu-item[data-category="${category}"]`);
128
+ let itemsText = `The ${category} menu includes: `;
129
+ items.forEach(item => {
130
+ itemsText += item.querySelector("h3").textContent + ", ";
131
  });
132
+ speak(itemsText);
133
+ recognition.start();
134
  recognition.onresult = (event) => {
135
+ const itemOrdered = event.results[0][0].transcript.toLowerCase();
136
+ const itemElement = Array.from(items).find(item => item.querySelector("h3").textContent.toLowerCase().includes(itemOrdered));
137
+
138
+ if (itemElement) {
139
+ const itemName = itemElement.querySelector("h3").textContent;
140
+ speak(`You selected ${itemName}. How many would you like to order?`);
141
+ askForQuantity(itemName);
142
+ } else {
143
+ speak("Sorry, I couldn't find that item. Please say the item again.");
144
+ }
145
+ };
146
+ }
147
 
148
+ function askForQuantity(itemName) {
149
+ recognition.start();
150
+ recognition.onresult = (event) => {
151
+ const quantity = event.results[0][0].transcript.trim();
152
+ if (isNaN(quantity)) {
153
+ speak("Sorry, I didn't catch that. Please say a number.");
154
+ } else {
155
+ cart.push({ itemName, quantity });
156
+ speak(`${quantity} of ${itemName} added to your cart. Would you like to add more items?`);
157
+ askToContinue();
158
  }
159
  };
160
+ }
161
 
162
+ function askToContinue() {
163
+ recognition.start();
164
+ recognition.onresult = (event) => {
165
+ const command = event.results[0][0].transcript.toLowerCase();
166
+ if (command.includes("yes")) {
167
+ speak("What would you like to order next?");
168
+ recognition.stop();
169
+ } else if (command.includes("no")) {
170
+ announceCartSummary();
171
+ } else {
172
+ speak("Sorry, I didn't understand. Please say 'yes' to continue or 'no' to proceed to checkout.");
173
+ }
174
  };
175
+ }
176
 
177
+ function announceCartSummary() {
178
+ let cartSummary = "Your cart includes: ";
179
+ cart.forEach(item => {
180
+ cartSummary += `${item.quantity} of ${item.itemName}, `;
181
+ });
182
+ cartSummary += "Would you like to proceed with the order?";
183
+ speak(cartSummary);
184
+ confirmationForOrder();
185
+ }
186
+
187
+ function confirmationForOrder() {
188
+ recognition.start();
189
+ recognition.onresult = (event) => {
190
+ const confirmation = event.results[0][0].transcript.toLowerCase();
191
+ if (confirmation.includes("yes")) {
192
+ speak("Your order has been placed successfully. Thank you!");
193
+ // Implement the final order placing logic here
194
+ } else if (confirmation.includes("no")) {
195
+ speak("Your order has been cancelled. Thank you!");
196
+ // Cancel order logic
197
+ } else {
198
+ speak("Sorry, I didn't understand. Please confirm if you'd like to place the order.");
199
+ }
200
+ };
201
+ }
202
 
203
+ recognition.onerror = (event) => {
204
+ console.error("Speech recognition error:", event.error);
205
+ speak("Sorry, I couldn't understand that. Please try again.");
206
+ };
207
+ </script>
208
  </body>
209
  </html>