-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
468 lines (410 loc) · 12.4 KB
/
Copy pathindex.html
File metadata and controls
468 lines (410 loc) · 12.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Login with Gemini Chatbot</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: Arial, sans-serif;
background: #f3f3f3;
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.login-box {
background: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
text-align: center;
}
input, button {
width: 100%;
margin: 10px 0;
padding: 10px;
font-size: 1rem;
box-sizing: border-box;
}
button {
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
}
#message {
color: red;
font-size: 0.9rem;
min-height: 1.2rem;
margin-top: 10px;
}
#message.success {
color: green;
}
#message.error {
color: red;
}
/* --- New Gemini Chatbot styles --- */
#chatToggleBtn {
position: fixed;
bottom: 30px;
right: 30px;
width: 60px;
height: 60px;
background-color: #007bff;
border-radius: 50%;
box-shadow: 0 4px 10px rgba(0,0,0,0.3);
color: white;
font-size: 30px;
cursor: pointer;
z-index: 1001;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.3s;
user-select: none;
}
#chatToggleBtn:hover {
background-color: #0056b3;
}
#chatToggleIcon {
transition: transform 0.3s ease, opacity 0.3s ease;
}
#chatWindow {
position: fixed;
bottom: 100px;
right: 30px;
width: 400px;
max-height: 500px;
background: white;
border-radius: 15px;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
display: flex;
flex-direction: column;
overflow: hidden;
z-index: 1000;
opacity: 0;
transform: translateY(20px);
pointer-events: none;
transition: opacity 0.4s ease, transform 0.4s ease;
}
#chatWindow.open {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
}
#chatHeader {
background-color: #007bff;
color: white;
padding: 15px;
font-weight: bold;
font-size: 18px;
border-radius: 15px 15px 0 0;
user-select: none;
}
.chat-container {
padding: 20px;
flex: 1;
overflow-y: auto;
display: flex;
flex-direction: column;
background: #fafafa;
}
.input-container {
display: flex;
border-top: 1px solid #ddd;
align-items: center;
}
.input-container input[type="text"] {
flex: 1;
min-width: 0;
padding: 12px 15px;
font-size: 16px;
border: none;
outline: none;
border-radius: 0 0 0 15px;
}
.input-container button.sendBtn {
flex: none;
width: auto;
padding: 12px 16px;
border: none;
background-color: #007bff;
color: white;
font-weight: bold;
cursor: pointer;
border-radius: 0 0 15px 0;
transition: background-color 0.3s;
}
button.sendBtn:hover {
background-color: #0056b3;
}
.message {
margin: 8px 0;
padding: 10px 15px;
border-radius: 20px;
max-width: 75%;
word-wrap: break-word;
animation: fadeIn 0.3s ease-in-out;
}
.user {
background: #d1e7dd;
align-self: flex-end;
}
.bot {
background: #f8d7da;
align-self: flex-start;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 500px) {
#chatWindow {
width: 90%;
right: 5%;
bottom: 90px;
}
}
</style>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<!-- Login Form -->
<div class="login-box">
<h2>SREC Wall</h2>
<input type="email" id="email" placeholder="Email" autocomplete="username" />
<input type="password" id="password" placeholder="Password" autocomplete="current-password" />
<button onclick="login()">Login</button>
<button onclick="signup()">Sign Up</button>
<div id="message"></div>
</div>
<!-- Gemini Chatbot -->
<div id="chatToggleBtn" title="Open Chat" aria-label="Toggle chat">
<span id="chatToggleIcon">💬</span>
</div>
<div id="chatWindow" role="dialog" aria-modal="true" aria-labelledby="chatHeader">
<div id="chatHeader">SREC FAQ Chatbot</div>
<div class="chat-container" id="chatBox"></div>
<div class="input-container">
<input type="text" id="userInput" placeholder="Ask about your college..." autocomplete="off" />
<button class="sendBtn" id="sendBtn">Send</button>
</div>
</div>
<!-- Firebase Auth + Login Script -->
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.0/firebase-app.js";
import { getAuth, signInWithEmailAndPassword, createUserWithEmailAndPassword, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.0/firebase-auth.js";
const firebaseConfig = {
apiKey: "AIzaSyD03vMpBpU5TOYwLy-hpImkku-MvwzANLw",
authDomain: "srecsocial-98c6f.firebaseapp.com",
projectId: "srecsocial-98c6f",
storageBucket: "srecsocial-98c6f.appspot.com",
messagingSenderId: "92453012487",
appId: "1:92453012487:web:1ee9723799a929c64356a2",
measurementId: "G-NYDLMEV2TE"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const chatToggleBtn = document.getElementById('chatToggleBtn');
const chatWindow = document.getElementById('chatWindow');
onAuthStateChanged(auth, (user) => {
const loginBox = document.querySelector('.login-box');
chatToggleBtn.style.display = "flex";
if (user) {
loginBox.style.display = "none";
window.location.href = "home.html";
} else {
loginBox.style.display = "block";
chatWindow.classList.remove('open');
}
});
window.login = async function() {
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const msg = document.getElementById("message");
msg.textContent = "";
msg.className = "";
if (!email || !password) {
msg.textContent = "❌ Please enter email and password.";
msg.className = "error";
return;
}
try {
await signInWithEmailAndPassword(auth, email, password);
} catch (err) {
msg.textContent = "❌ " + err.message;
msg.className = "error";
}
};
window.signup = async function() {
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const msg = document.getElementById("message");
msg.textContent = "";
msg.className = "";
if (!email || !password) {
msg.textContent = "❌ Please enter email and password.";
msg.className = "error";
return;
}
try {
await createUserWithEmailAndPassword(auth, email, password);
msg.textContent = "✅ Account created! Please login.";
msg.className = "success";
} catch (err) {
msg.textContent = "❌ " + err.message;
msg.className = "error";
}
};
</script>
<!-- Gemini Chatbot Logic (same as your original code) -->
<!-- <script type="module">
// ...Paste your Gemini Chatbot JavaScript here...
</script> -->
<script type="module">
// === YOUR CONFIGURATION ===
const GEMINI_API_KEY = "AIzaSyAhnZ9oCOlvsw-Dn8AaoFPFWO6H1POWN4U";
const FAQ_JSON_URL = "https://script.google.com/macros/s/AKfycbzsBf-ALL0bCb91a1_VgtfZIooVxAl_oMRT5BKHgpkEPkggf8REbhGL0WGycbzSGtHJ/exec";
let faqs = [];
async function loadFAQs() {
try {
const response = await fetch(FAQ_JSON_URL);
if (!response.ok) throw new Error("Failed to fetch FAQs");
faqs = await response.json();
console.log("Loaded FAQs:", faqs.length);
} catch (err) {
console.error("Error loading FAQs:", err);
faqs = [];
}
}
function getKeywords(text) {
return text.toLowerCase().replace(/[.,!?]/g, "").split(/\s+/).filter(w => w.length > 2);
}
function findRelevantFaqs(question, faqs, maxResults = 3) {
const questionKeywords = getKeywords(question);
if (questionKeywords.length === 0) return [];
const scoredFaqs = faqs.map(faq => {
const faqKeywords = getKeywords(faq.question);
const overlapCount = faqKeywords.filter(k => questionKeywords.includes(k)).length;
return { faq, score: overlapCount };
});
return scoredFaqs
.filter(f => f.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, maxResults)
.map(f => f.faq);
}
async function callGeminiFlashAPI(prompt) {
const url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent";
const body = { contents: [{ parts: [{ text: prompt }] }] };
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-goog-api-key": GEMINI_API_KEY,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const errorData = await response.json().catch(() => null);
throw new Error(errorData?.error?.message || "Gemini API error");
}
return await response.json();
}
function typewriterMarkdown(element, markdown, speed = 10) {
const html = marked.parse(markdown);
element.innerHTML = "";
let i = 0;
const tempDiv = document.createElement("div");
tempDiv.innerHTML = html;
const finalHTML = tempDiv.innerHTML;
function type() {
if (i <= finalHTML.length) {
element.innerHTML = finalHTML.slice(0, i);
i++;
setTimeout(type, speed);
}
}
type();
}
// const chatToggleBtn = document.getElementById('chatToggleBtn');
const chatToggleIcon = document.getElementById('chatToggleIcon');
// const chatWindow = document.getElementById('chatWindow');
const chatBox = document.getElementById('chatBox');
const userInput = document.getElementById('userInput');
const sendBtn = document.getElementById('sendBtn');
function changeIcon(newIcon, newTitle) {
chatToggleIcon.style.opacity = 0;
chatToggleIcon.style.transform = "scale(0.3)";
setTimeout(() => {
chatToggleIcon.textContent = newIcon;
chatToggleBtn.title = newTitle;
chatToggleIcon.style.opacity = 1;
chatToggleIcon.style.transform = "scale(1)";
}, 300);
}
chatToggleBtn.addEventListener('click', () => {
chatWindow.classList.toggle('open');
if (chatWindow.classList.contains('open')) {
changeIcon("❌", "Close Chat");
userInput.focus();
} else {
changeIcon("💬", "Open Chat");
}
});
function appendMessage(sender, message = "") {
const msg = document.createElement("div");
msg.classList.add("message", sender);
msg.textContent = message;
chatBox.appendChild(msg);
chatBox.scrollTop = chatBox.scrollHeight;
return msg;
}
async function handleUserInput() {
const input = userInput.value.trim();
if (!input) return;
appendMessage("user", input);
userInput.value = "";
const relevantFaqs = findRelevantFaqs(input, faqs, 3);
let prompt = `You are a helpful assistant answering questions about Sri Ramakrishna Engineering College.\n\nHere are some FAQs:\n\n`;
if (relevantFaqs.length === 0) {
prompt += "(No directly relevant FAQs found.)\n\n";
} else {
relevantFaqs.forEach(faq => {
prompt += `Q: ${faq.question}\nA: ${faq.answer}\n\n`;
});
}
prompt += `User's question:\n"${input}"\n\nPlease answer concisely and professionally using the information above if possible.`;
const botMsg = document.createElement("div");
botMsg.classList.add("message", "bot");
botMsg.textContent = "⏳ Thinking...";
chatBox.appendChild(botMsg);
chatBox.scrollTop = chatBox.scrollHeight;
try {
const result = await callGeminiFlashAPI(prompt);
const content = result?.candidates?.[0]?.content?.parts?.[0]?.text;
if (content) {
typewriterMarkdown(botMsg, content);
} else {
botMsg.textContent = "❌ Sorry, no response.";
}
} catch (err) {
botMsg.textContent = "❌ Error: " + err.message;
}
}
sendBtn.addEventListener('click', handleUserInput);
userInput.addEventListener('keydown', e => {
if (e.key === 'Enter') handleUserInput();
});
loadFAQs();
</script>
</body>
</html>