Swathi6 commited on
Commit
b82b19e
·
verified ·
1 Parent(s): c962a98

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +71 -0
script.js ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Example restaurant data for menu suggestions
2
+ const menu = {
3
+ vegan: ['Vegan Burger', 'Tofu Stir Fry', 'Veggie Pizza'],
4
+ vegetarian: ['Cheese Pizza', 'Pasta Alfredo', 'Caprese Salad'],
5
+ non_vegetarian: ['Chicken Curry', 'Grilled Salmon', 'Steak'],
6
+ spicy: ['Spicy Tacos', 'Chili Chicken', 'Spicy Wings'],
7
+ };
8
+
9
+ document.getElementById("chat-form").addEventListener("submit", function(e) {
10
+ e.preventDefault();
11
+
12
+ const userInput = document.getElementById("user-input").value;
13
+ document.getElementById("user-input").value = ''; // Clear input box
14
+ addMessage(userInput, 'user-message');
15
+
16
+ // Get bot response based on user input
17
+ getBotResponse(userInput);
18
+ });
19
+
20
+ function addMessage(message, className) {
21
+ const messageDiv = document.createElement('div');
22
+ messageDiv.classList.add('message', className);
23
+ messageDiv.innerText = message;
24
+ document.getElementById("chat-box").appendChild(messageDiv);
25
+ scrollToBottom();
26
+ }
27
+
28
+ function scrollToBottom() {
29
+ const chatBox = document.getElementById("chat-box");
30
+ chatBox.scrollTop = chatBox.scrollHeight;
31
+ }
32
+
33
+ function getBotResponse(userInput) {
34
+ const botResponse = getChatbotReply(userInput);
35
+ addMessage(botResponse, 'bot-response');
36
+ suggestMenu(userInput);
37
+ }
38
+
39
+ function getChatbotReply(userInput) {
40
+ // Simulating a basic conversation
41
+ if (userInput.toLowerCase().includes('hello') || userInput.toLowerCase().includes('hi')) {
42
+ return "Hello! How can I help you today?";
43
+ } else if (userInput.toLowerCase().includes('menu')) {
44
+ return "Sure! What type of food are you looking for? Vegan, Vegetarian, Non-Vegetarian, or Spicy?";
45
+ } else {
46
+ return "Sorry, I didn't quite catch that. Can you rephrase?";
47
+ }
48
+ }
49
+
50
+ function suggestMenu(userInput) {
51
+ let userPreference = '';
52
+
53
+ if (userInput.toLowerCase().includes('vegan')) {
54
+ userPreference = 'vegan';
55
+ } else if (userInput.toLowerCase().includes('vegetarian')) {
56
+ userPreference = 'vegetarian';
57
+ } else if (userInput.toLowerCase().includes('non-vegetarian')) {
58
+ userPreference = 'non_vegetarian';
59
+ } else if (userInput.toLowerCase().includes('spicy')) {
60
+ userPreference = 'spicy';
61
+ }
62
+
63
+ const suggestedItems = menu[userPreference] || [];
64
+
65
+ const menuDiv = document.getElementById('menu');
66
+ if (suggestedItems.length > 0) {
67
+ menuDiv.innerHTML = `<strong>Recommended Dishes:</strong><br>${suggestedItems.join(', ')}`;
68
+ } else {
69
+ menuDiv.innerHTML = "Sorry, I couldn't find any suggestions for that. Please specify your preference.";
70
+ }
71
+ }