forked from Technigo/project-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
201 lines (163 loc) Β· 6.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// DOM selectors (variables that point to selected DOM elements) goes here π
const chat = document.getElementById('chat')
const locationForm = document.getElementById('location-form')
const locationInput = document.getElementById('location-input')
// Functions goes here π
// A function that will add a chat bubble in the correct place based on who the sender is
const showMessage = (message, sender) => {
// The if statement checks if the sender is the user and if that's the case it inserts
// an HTML section inside the chat with the posted message from the user
if (sender === 'user') {
chat.innerHTML += `
<section class="user-msg">
<div class="bubble user-bubble">
<p>${message}</p>
</div>
<img src="user.png" alt="User" />
</section>
`
// The else if statement checks if the sender is the bot and if that's the case it inserts
// an HTML section inside the chat with the posted message from the bot
} else if (sender === 'bot') {
// add a console.log here to see when it's being logged and not
console.log("Message is:", message);
console.log("Sender is:", sender);
chat.innerHTML += `
<section class="bot-msg">
<img src="bot.png" alt="Bot" />
<div class="bubble bot-bubble">
<p>${message}</p>
</div>
</section>
`
}
// This little thing makes the chat scroll to the last message when there are too many to
// be shown in the chat box
chat.scrollTop = chat.scrollHeight
// Clear the input field after submission
locationInput.value = ''
}
// A function to start the conversation
const greetUser = () => {
showMessage("Hello, I can help you choose the perfect outfit based on the weather. What's your location?π", 'bot')
}
// A function for the location input
const handleLocationInput = (event) => {
event.preventDefault() // Prevent from refreshing the page
const userLocation = locationInput.value
if (userLocation) {
showMessage(userLocation, 'user')
// Remove the event listener for location input after being hanlded
locationForm.removeEventListener('submit', handleLocationInput);
// Checking weather...
setTimeout(() => {
showMessage(`Checking the weather for ${userLocation}...`, 'bot')
setTimeout(() => {
const weather = "sunnyβοΈ and between 18-20 degrees"
showMessage(`It looks like it's going to be ${weather} today in ${userLocation}.`, 'bot')
setTimeout(showClothingOptions, 1000)
}, 2000)
}, 1000)
}
}
// Function to create buttons for choosing casual or formal clothing
const showClothingOptions = () => {
locationForm.removeEventListener('submit', handleInvalidInput);
chat.innerHTML += `
<section class="bot-msg clothing-options">
<img src="assets/bot.png" alt="Bot" />
<div class="bubble bot-bubble">
<p>Would you prefer casual or formal clothing?</p>
<button class="clothing-choice" data-choice="casual">Casual</button>
<button class="clothing-choice" data-choice="formal">Formal</button>
</div>
</section>
`
// Function to handle clothing choice
const handleClothingChoice = (event) => {
const choice = event.target.getAttribute('data-choice');
if (choice) {
showMessage(`I prefer ${choice} clothing.`, 'user');
// Remove event listeners
const buttons = document.querySelectorAll('.clothing-choice');
buttons.forEach(button => {
button.removeEventListener('click', handleClothingChoice);
});
// Remove event listener for invalid input after a valid choice
locationForm.removeEventListener('submit', handleInvalidInput);
// Ask about the occasion
setTimeout(() => {
askForOccasion(choice);
}, 1000);
}
}
// Function to ask about the occasion
const askForOccasion = (clothingChoice) => {
showMessage("Are you dressing for a specific occasion or activity (e.g., work, party, exercise)?", 'bot');
const occasionInputHandler = (event) => handleOccasionInput(event, clothingChoice, occasionInputHandler);
locationForm.addEventListener('submit', occasionInputHandler);
}
// Eevent listeners to the buttons to handle the user's choice
const buttons = document.querySelectorAll('.clothing-choice');
buttons.forEach(button => {
button.addEventListener('click', handleClothingChoice);
});
// Event listener to handle invalid input
locationForm.addEventListener('submit', handleInvalidInput);
}
// Function to handle invalid input (user typing text instead of clicking buttons)
const handleInvalidInput = (event) => {
event.preventDefault();
const userInput = locationInput.value;
if (userInput) {
showMessage(userInput, 'user');
showMessage("That's not a valid answer. Please refresh the chat and try again.", 'bot');
}
};
// Function to handle the occasion input
const handleOccasionInput = (event, clothingChoice, occasionInputHandler) => {
event.preventDefault();
const occasion = locationInput.value;
if (occasion) {
showMessage(occasion, 'user');
locationInput.value = '';
// Remove the event listener to prevent duplicates
locationForm.removeEventListener('submit', occasionInputHandler);
setTimeout(() => {
OutfitSuggestion(clothingChoice, occasion);
}, 1000);
}
}
// Function for outfit suggestion
const OutfitSuggestion = (clothingChoice, occasion) => {
let suggestion = '';
if (clothingChoice === 'casual') {
if (occasion.toLowerCase().includes('work')) {
suggestion = "A casual shirt and chinos might be perfect for work. π";
} else if (occasion.toLowerCase().includes('party')) {
suggestion = "How about jeans and a stylish t-shirt for the party? π";
} else if (occasion.toLowerCase().includes('exercise')) {
suggestion = "Athletic wear would be great for exercising! π";
} else {
suggestion = "A t-shirt and jeans would be great! ππ";
}
} else if (clothingChoice === 'formal') {
if (occasion.toLowerCase().includes('work')) {
suggestion = "A blazer with dress pants would suit you well for work. πΌ";
} else if (occasion.toLowerCase().includes('party')) {
suggestion = "A cocktail dress or a sharp suit would be ideal for the party. π₯";
} else if (occasion.toLowerCase().includes('exercise')) {
suggestion = "For exercise, comfort is key! Maybe opt for formal sportswear. π";
} else {
suggestion = "A suit and tie might be a great option! π";
}
}
showMessage(suggestion, 'bot');
// End the conversation
setTimeout(() => {
showMessage("Let me know if you need anything else!", 'bot');
}, 2000);
}
setTimeout(greetUser, 1000)
// Listen for the form submit event to capture user's location input
locationForm.addEventListener('submit', handleLocationInput)