jithenderchoudary commited on
Commit
2e6c845
·
verified ·
1 Parent(s): df59914

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +148 -210
static/script.js CHANGED
@@ -1,12 +1,86 @@
 
 
1
  let conversation = [
2
  { role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }
3
  ];
4
  let userName = '';
5
  let isSubmitting = false;
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  function addMessage(role, message) {
8
  const chatMessages = document.getElementById('chatMessages');
9
- if (!chatMessages) return;
10
  const messageDiv = document.createElement('div');
11
  messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
12
  messageDiv.textContent = message;
@@ -19,24 +93,20 @@ function sendMessage() {
19
  const message = userInput.value.trim();
20
  if (message) {
21
  addMessage('user', message);
22
- conversation.push({ role: 'user', message: message });
23
  userInput.value = '';
24
  setTimeout(() => handleResponse(message), 500);
25
  }
26
  }
27
 
28
  document.addEventListener('DOMContentLoaded', () => {
29
- const userInput = document.getElementById('userInput');
30
- const clearButton = document.getElementById('clearButton');
31
- if (userInput) {
32
- userInput.addEventListener('keydown', (event) => {
33
- if (event.key === 'Enter' && !event.shiftKey) {
34
- event.preventDefault();
35
- sendMessage();
36
- }
37
- });
38
- }
39
- if (clearButton) clearButton.addEventListener('click', clearChat);
40
  });
41
 
42
  function clearChat() {
@@ -48,10 +118,60 @@ function clearChat() {
48
  isSubmitting = false;
49
  }
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  function displayForm() {
52
  const chatMessages = document.getElementById('chatMessages');
53
  if (chatMessages.querySelector('.registration-form')) return;
54
-
55
  const formDiv = document.createElement('div');
56
  formDiv.className = 'bot-message';
57
  formDiv.innerHTML = `
@@ -70,26 +190,17 @@ function displayForm() {
70
  chatMessages.scrollTop = chatMessages.scrollHeight;
71
  }
72
 
73
- function resetForm() {
74
- document.getElementById('mobileNumber').value = '';
75
- document.getElementById('email').value = '';
76
- document.getElementById('issueDescription').value = '';
77
- document.getElementById('submitButton').disabled = false;
78
- isSubmitting = false;
79
- }
80
-
81
  function submitForm() {
82
  if (isSubmitting) return;
83
-
84
  const submitButton = document.getElementById('submitButton');
85
  isSubmitting = true;
86
  submitButton.disabled = true;
87
 
88
- const mobileNumber = document.getElementById('mobileNumber').value.trim();
89
  const email = document.getElementById('email').value.trim();
90
- const issueDescription = document.getElementById('issueDescription').value.trim();
91
 
92
- if (!/^\d{10}$/.test(mobileNumber)) {
93
  addMessage('bot', 'Please enter a valid 10-digit mobile number.');
94
  resetForm();
95
  return;
@@ -99,18 +210,13 @@ function submitForm() {
99
  resetForm();
100
  return;
101
  }
102
- if (!issueDescription) {
103
  addMessage('bot', 'Please describe your issue.');
104
  resetForm();
105
  return;
106
  }
107
 
108
- const formData = {
109
- name: userName,
110
- mobileNumber,
111
- email,
112
- issueDescription
113
- };
114
 
115
  fetch('/submit-case', {
116
  method: 'POST',
@@ -120,191 +226,23 @@ function submitForm() {
120
  .then(res => res.json())
121
  .then(data => {
122
  if (data.success) {
123
- addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
124
  setTimeout(resetForm, 4000);
125
  } else {
126
- addMessage('bot', 'There was an error submitting your case. Please try again later.');
127
  resetForm();
128
  }
129
  })
130
  .catch(() => {
131
- addMessage('bot', 'There was an error submitting your case. Please try again later.');
132
  resetForm();
133
  });
134
  }
135
 
136
- function displayOptions(options) {
137
- const chatMessages = document.getElementById('chatMessages');
138
- options.forEach(opt => {
139
- const messageDiv = document.createElement('div');
140
- messageDiv.className = 'bot-message';
141
- const button = document.createElement('button');
142
- button.textContent = opt.text;
143
- button.className = `option-button ${opt.class}`;
144
- button.onclick = () => {
145
- addMessage('user', opt.text);
146
- conversation.push({ role: 'user', message: opt.text });
147
- setTimeout(() => handleResponse(opt.text), 500);
148
- };
149
- messageDiv.appendChild(button);
150
- chatMessages.appendChild(messageDiv);
151
- });
152
- chatMessages.scrollTop = chatMessages.scrollHeight;
153
- }
154
-
155
- function displayCategories() {
156
- const categories = [
157
- {
158
- text: 'About TioNat and Account Setup', class: 'blue', questions: [
159
- { text: 'What is TioNat?', class: 'blue' },
160
- { text: 'How do I create an account on TioNat?', class: 'blue' },
161
- { text: 'How can I track my order?', class: 'blue' },
162
- { text: 'Do you ship internationally?', class: 'blue' },
163
- { text: 'What payment methods do you accept?', class: 'blue' }
164
- ]
165
- },
166
- {
167
- text: 'Nutritional Supplements', class: 'blue', questions: [
168
- { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
169
- { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
170
- { text: 'How do I know which supplement is right for me?', class: 'blue' },
171
- { text: 'Are there any side effects of using your supplements?', class: 'blue' },
172
- { text: 'Do you offer products for specific health conditions?', class: 'blue' }
173
- ]
174
- },
175
- {
176
- text: 'Health Care Products', class: 'blue', questions: [
177
- { text: 'How do I use your health care products?', class: 'blue' },
178
- { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
179
- { text: 'Are your health care products FDA-approved?', class: 'blue' },
180
- { text: 'Are your personal care products cruelty-free?', class: 'blue' },
181
- { text: 'What ingredients do you use in your skincare products?', class: 'blue' }
182
- ]
183
- },
184
- {
185
- text: 'Personal Care and Skincare', class: 'blue', questions: [
186
- { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
187
- { text: 'How do I find the right skincare routine?', class: 'blue' },
188
- { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
189
- { text: 'How long does it take to receive my order?', class: 'blue' },
190
- { text: 'Do you offer free shipping?', class: 'blue' }
191
- ]
192
- },
193
- {
194
- text: 'Returns and Customer Support', class: 'blue', questions: [
195
- { text: 'How do I return a product?', class: 'blue' },
196
- { text: 'How can I contact customer service?', class: 'blue' },
197
- { text: 'Can I track my return?', class: 'blue' },
198
- { text: 'How can I get a product recommendation?', class: 'blue' },
199
- { text: 'How do I reset my password?', class: 'blue' }
200
- ]
201
- },
202
- {
203
- text: 'Security and Account Management', class: 'blue', questions: [
204
- { text: 'Is my payment information secure?', class: 'blue' },
205
- { text: 'How do I update my account information?', class: 'blue' },
206
- { text: 'Can I leave a review for a product?', class: 'blue' },
207
- { text: 'How do I report a product issue?', class: 'blue' }
208
- ]
209
- },
210
- { text: 'Other', class: 'green' }
211
- ];
212
-
213
- const chatMessages = document.getElementById('chatMessages');
214
- categories.forEach(category => {
215
- const messageDiv = document.createElement('div');
216
- messageDiv.className = 'bot-message';
217
- const button = document.createElement('button');
218
- button.textContent = category.text;
219
- button.className = `option-button ${category.class}`;
220
- button.onclick = () => {
221
- addMessage('user', category.text);
222
- conversation.push({ role: 'user', message: category.text });
223
- if (category.text === 'Other') {
224
- addMessage('bot', "Please provide more details about your query so our service agent can assist you.");
225
- displayForm();
226
- } else {
227
- displayOptions(category.questions);
228
- }
229
- };
230
- messageDiv.appendChild(button);
231
- chatMessages.appendChild(messageDiv);
232
- });
233
- chatMessages.scrollTop = chatMessages.scrollHeight;
234
- }
235
-
236
- function handleResponse(userInput) {
237
- if (conversation.length === 2 && conversation[0].message.includes("your name")) {
238
- userName = userInput;
239
- addMessage('bot', `Nice to meet you, ${userInput}! 😊 Please select a category to explore common questions or choose 'Other' to submit a custom query.`);
240
- displayCategories();
241
- return;
242
- }
243
-
244
- const lower = userInput.toLowerCase();
245
- let botResponse = "";
246
-
247
- if (lower.includes("what is tionat")) {
248
- botResponse = "TioNat is an e-commerce platform offering Nutritional, Health Care, and Personal Care products.";
249
- } else if (lower.includes("create an account")) {
250
- botResponse = "Click 'Sign Up' on the top-right of our homepage to register.";
251
- } else if (lower.includes("track my order")) {
252
- botResponse = "You can track your order under 'Order History' in your TioNat account.";
253
- } else if (lower.includes("ship internationally")) {
254
- botResponse = "Yes, we ship to select countries. Check at checkout.";
255
- } else if (lower.includes("payment methods")) {
256
- botResponse = "We accept cards, PayPal, net banking, and COD (where available).";
257
- } else if (lower.includes("boosting immunity")) {
258
- botResponse = "Try our Vitamin C, Zinc, and Echinacea supplements.";
259
- } else if (lower.includes("vegan-friendly")) {
260
- botResponse = "Yes, many of our supplements are vegan-friendly. Check product labels.";
261
- } else if (lower.includes("which supplement is right")) {
262
- botResponse = "Consult a doctor or view our product guides.";
263
- } else if (lower.includes("side effects")) {
264
- botResponse = "Please read the label or consult your doctor before use.";
265
- } else if (lower.includes("specific health conditions")) {
266
- botResponse = "Yes! We have products for joint pain, digestion, heart health, etc.";
267
- } else if (lower.includes("how do i use your health care products")) {
268
- botResponse = "Usage instructions are on the package and product page.";
269
- } else if (lower.includes("money-back guarantee")) {
270
- botResponse = "Yes, within 30 days. See our Return Policy.";
271
- } else if (lower.includes("fda-approved")) {
272
- botResponse = "Check each product’s details for FDA approval.";
273
- } else if (lower.includes("cruelty-free")) {
274
- botResponse = "Yes! All personal care items are cruelty-free.";
275
- } else if (lower.includes("ingredients")) {
276
- botResponse = "We use high-quality natural oils, vitamins, and extracts.";
277
- } else if (lower.includes("sensitive skin")) {
278
- botResponse = "Yes, we have products for sensitive skin.";
279
- } else if (lower.includes("skincare routine")) {
280
- botResponse = "Start with cleanse–tone–moisturize. Contact us for guidance.";
281
- } else if (lower.includes("cancel my order")) {
282
- botResponse = "Contact us within 24 hrs to modify/cancel.";
283
- } else if (lower.includes("receive my order")) {
284
- botResponse = "3–7 business days depending on location.";
285
- } else if (lower.includes("free shipping")) {
286
- botResponse = "Yes, on orders above a minimum amount.";
287
- } else if (lower.includes("return a product")) {
288
- botResponse = "Return within 30 days via our Return Center.";
289
- } else if (lower.includes("contact customer service")) {
290
- botResponse = "Email support@tionat.com or call +1-800-123-4567.";
291
- } else if (lower.includes("track my return")) {
292
- botResponse = "Yes. You’ll get a tracking ID after initiating return.";
293
- } else if (lower.includes("product recommendation")) {
294
- botResponse = "Use our guides or ask us directly!";
295
- } else if (lower.includes("reset my password")) {
296
- botResponse = "Click ‘Forgot Password’ on the login page.";
297
- } else if (lower.includes("payment information secure")) {
298
- botResponse = "Yes, we use SSL encryption and secure gateways.";
299
- } else if (lower.includes("update my account")) {
300
- botResponse = "Login > Account Settings to update your info.";
301
- } else if (lower.includes("leave a review")) {
302
- botResponse = "Yes! Go to the product page after purchase.";
303
- } else if (lower.includes("report a product issue")) {
304
- botResponse = "Contact support immediately with product details.";
305
- } else {
306
- botResponse = "I'm sorry, I don't understand. Please choose a category or click 'Other'.";
307
- }
308
-
309
- addMessage('bot', botResponse);
310
  }
 
1
+ // TioNat Chatbot Full JavaScript Code with Category-wise FAQ Display
2
+
3
  let conversation = [
4
  { role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }
5
  ];
6
  let userName = '';
7
  let isSubmitting = false;
8
 
9
+ const faqCategories = [
10
+ {
11
+ name: 'About TioNat and Account Setup',
12
+ color: 'blue',
13
+ questions: [
14
+ "What is TioNat?",
15
+ "How do I create an account on TioNat?",
16
+ "How can I track my order?",
17
+ "Do you ship internationally?",
18
+ "What payment methods do you accept?"
19
+ ]
20
+ },
21
+ {
22
+ name: 'Nutritional Supplements',
23
+ color: 'purple',
24
+ questions: [
25
+ "What is the best supplement for boosting immunity?",
26
+ "Are your nutritional supplements vegan-friendly?",
27
+ "How do I know which supplement is right for me?",
28
+ "Are there any side effects of using your supplements?",
29
+ "Do you offer products for specific health conditions?"
30
+ ]
31
+ },
32
+ {
33
+ name: 'Health Care Products',
34
+ color: 'teal',
35
+ questions: [
36
+ "How do I use your health care products?",
37
+ "Do your products come with a money-back guarantee?",
38
+ "Are your health care products FDA-approved?",
39
+ "Are your personal care products cruelty-free?",
40
+ "What ingredients do you use in your skincare products?"
41
+ ]
42
+ },
43
+ {
44
+ name: 'Personal Care and Skincare',
45
+ color: 'orange',
46
+ questions: [
47
+ "Can I use your personal care products for sensitive skin?",
48
+ "How do I find the right skincare routine?",
49
+ "Can I modify or cancel my order after placing it?",
50
+ "How long does it take to receive my order?",
51
+ "Do you offer free shipping?"
52
+ ]
53
+ },
54
+ {
55
+ name: 'Returns and Customer Support',
56
+ color: 'red',
57
+ questions: [
58
+ "How do I return a product?",
59
+ "How can I contact customer service?",
60
+ "Can I track my return?",
61
+ "How can I get a product recommendation?",
62
+ "How do I reset my password?"
63
+ ]
64
+ },
65
+ {
66
+ name: 'Security and Account Management',
67
+ color: 'green',
68
+ questions: [
69
+ "Is my payment information secure?",
70
+ "How do I update my account information?",
71
+ "Can I leave a review for a product?",
72
+ "How do I report a product issue?"
73
+ ]
74
+ },
75
+ {
76
+ name: 'Other',
77
+ color: 'gray',
78
+ questions: []
79
+ }
80
+ ];
81
+
82
  function addMessage(role, message) {
83
  const chatMessages = document.getElementById('chatMessages');
 
84
  const messageDiv = document.createElement('div');
85
  messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
86
  messageDiv.textContent = message;
 
93
  const message = userInput.value.trim();
94
  if (message) {
95
  addMessage('user', message);
96
+ conversation.push({ role: 'user', message });
97
  userInput.value = '';
98
  setTimeout(() => handleResponse(message), 500);
99
  }
100
  }
101
 
102
  document.addEventListener('DOMContentLoaded', () => {
103
+ document.getElementById('userInput').addEventListener('keydown', event => {
104
+ if (event.key === 'Enter' && !event.shiftKey) {
105
+ event.preventDefault();
106
+ sendMessage();
107
+ }
108
+ });
109
+ document.getElementById('clearButton').addEventListener('click', clearChat);
 
 
 
 
110
  });
111
 
112
  function clearChat() {
 
118
  isSubmitting = false;
119
  }
120
 
121
+ function displayCategories() {
122
+ const chatMessages = document.getElementById('chatMessages');
123
+ faqCategories.forEach(category => {
124
+ const div = document.createElement('div');
125
+ div.className = 'bot-message';
126
+ const btn = document.createElement('button');
127
+ btn.textContent = category.name;
128
+ btn.className = `option-button ${category.color}`;
129
+ btn.onclick = () => {
130
+ addMessage('user', category.name);
131
+ if (category.name === 'Other') {
132
+ addMessage('bot', "Please describe your query below:");
133
+ displayForm();
134
+ } else {
135
+ displayFAQQuestions(category);
136
+ }
137
+ };
138
+ div.appendChild(btn);
139
+ chatMessages.appendChild(div);
140
+ });
141
+ chatMessages.scrollTop = chatMessages.scrollHeight;
142
+ }
143
+
144
+ function displayFAQQuestions(category) {
145
+ const chatMessages = document.getElementById('chatMessages');
146
+ category.questions.forEach(q => {
147
+ const div = document.createElement('div');
148
+ div.className = 'bot-message';
149
+ const btn = document.createElement('button');
150
+ btn.textContent = q;
151
+ btn.className = `option-button ${category.color}`;
152
+ btn.onclick = () => {
153
+ addMessage('user', q);
154
+ setTimeout(() => handleResponse(q), 500);
155
+ };
156
+ div.appendChild(btn);
157
+ chatMessages.appendChild(div);
158
+ });
159
+ chatMessages.scrollTop = chatMessages.scrollHeight;
160
+ }
161
+
162
+ function handleResponse(userInput) {
163
+ if (conversation.length === 2 && conversation[0].message.includes("your name")) {
164
+ userName = userInput;
165
+ addMessage('bot', `Nice to meet you, ${userInput}! 😊 Please select a category below.`);
166
+ displayCategories();
167
+ return;
168
+ }
169
+ addMessage('bot', "Thank you for your question! Our support agent will get back to you shortly.");
170
+ }
171
+
172
  function displayForm() {
173
  const chatMessages = document.getElementById('chatMessages');
174
  if (chatMessages.querySelector('.registration-form')) return;
 
175
  const formDiv = document.createElement('div');
176
  formDiv.className = 'bot-message';
177
  formDiv.innerHTML = `
 
190
  chatMessages.scrollTop = chatMessages.scrollHeight;
191
  }
192
 
 
 
 
 
 
 
 
 
193
  function submitForm() {
194
  if (isSubmitting) return;
 
195
  const submitButton = document.getElementById('submitButton');
196
  isSubmitting = true;
197
  submitButton.disabled = true;
198
 
199
+ const mobile = document.getElementById('mobileNumber').value.trim();
200
  const email = document.getElementById('email').value.trim();
201
+ const issue = document.getElementById('issueDescription').value.trim();
202
 
203
+ if (!/^[0-9]{10}$/.test(mobile)) {
204
  addMessage('bot', 'Please enter a valid 10-digit mobile number.');
205
  resetForm();
206
  return;
 
210
  resetForm();
211
  return;
212
  }
213
+ if (!issue) {
214
  addMessage('bot', 'Please describe your issue.');
215
  resetForm();
216
  return;
217
  }
218
 
219
+ const formData = { name: userName, mobile, email, issue };
 
 
 
 
 
220
 
221
  fetch('/submit-case', {
222
  method: 'POST',
 
226
  .then(res => res.json())
227
  .then(data => {
228
  if (data.success) {
229
+ addMessage('bot', 'Your case has been submitted. Our agent will contact you soon.');
230
  setTimeout(resetForm, 4000);
231
  } else {
232
+ addMessage('bot', 'Error submitting form. Try again later.');
233
  resetForm();
234
  }
235
  })
236
  .catch(() => {
237
+ addMessage('bot', 'Error submitting form. Try again later.');
238
  resetForm();
239
  });
240
  }
241
 
242
+ function resetForm() {
243
+ document.getElementById('mobileNumber').value = '';
244
+ document.getElementById('email').value = '';
245
+ document.getElementById('issueDescription').value = '';
246
+ document.getElementById('submitButton').disabled = false;
247
+ isSubmitting = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  }