// CCET Chatbot Application JavaScript class CCETChatbot { constructor() { this.chatContainer = document.getElementById('chat-container'); this.chatInput = document.getElementById('chat-input'); this.sendBtn = document.getElementById('send-btn'); this.sidebar = document.getElementById('sidebar'); this.toggleSidebarBtn = document.getElementById('toggle-sidebar'); this.closeSidebarBtn = document.getElementById('close-sidebar'); this.clearChatBtn = document.getElementById('clear-chat'); this.helpBtn = document.getElementById('help-btn'); this.helpModal = document.getElementById('help-modal'); this.syllabusSelect = document.getElementById('syllabus-select'); this.downloadSyllabusBtn = document.getElementById('download-syllabus'); this.conversationHistory = []; this.isTyping = false; // Syllabus URLs this.syllabusUrls = { 'cse': 'https://ccet.ac.in/pdf/CSE_syllabus_2025.pdf', 'mechanical': 'https://ccet.ac.in/pdf/Mech_syll2024-28.pdf', 'ece': 'https://ccet.ac.in/pdf/ECE_SYLLABUS_2024-28.pdf', 'civil': 'https://ccet.ac.in/pdf/Civil_syllabus2024-28.pdf' }; this.init(); } init() { this.bindEvents(); this.showWelcomeMessage(); } bindEvents() { // Chat input events this.sendBtn.addEventListener('click', () => this.sendMessage()); this.chatInput.addEventListener('keypress', (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); this.sendMessage(); } }); // Sidebar events this.toggleSidebarBtn?.addEventListener('click', () => this.toggleSidebar()); this.closeSidebarBtn?.addEventListener('click', () => this.closeSidebar()); // Quick query buttons const queryButtons = document.querySelectorAll('.query-btn'); queryButtons.forEach(btn => { btn.addEventListener('click', () => { const query = btn.getAttribute('data-query'); this.chatInput.value = query; this.sendMessage(); }); }); // Syllabus download this.syllabusSelect.addEventListener('change', () => { this.downloadSyllabusBtn.disabled = !this.syllabusSelect.value; }); this.downloadSyllabusBtn.addEventListener('click', () => this.downloadSyllabus()); // Clear chat this.clearChatBtn.addEventListener('click', () => this.clearChat()); // Help modal this.helpBtn.addEventListener('click', () => { this.helpModal.classList.add('show'); }); // Modal close events const modalCloseButtons = document.querySelectorAll('.modal-close'); modalCloseButtons.forEach(btn => { btn.addEventListener('click', () => { this.helpModal.classList.remove('show'); }); }); // Close modal on backdrop click this.helpModal.addEventListener('click', (e) => { if (e.target === this.helpModal) { this.helpModal.classList.remove('show'); } }); // Close sidebar on outside click (mobile) document.addEventListener('click', (e) => { if (window.innerWidth <= 768 && this.sidebar.classList.contains('show') && !this.sidebar.contains(e.target) && !this.toggleSidebarBtn.contains(e.target)) { this.closeSidebar(); } }); } showWelcomeMessage() { const welcomeHtml = `
`; this.chatContainer.innerHTML = welcomeHtml; } async sendMessage() { const message = this.chatInput.value.trim(); if (!message || this.isTyping) return; this.addMessage(message, 'user'); this.chatInput.value = ''; this.conversationHistory.push({ role: 'user', content: message }); this.showTypingIndicator(); try { // Send message to Flask backend const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ message: message }) }); const data = await response.json(); this.hideTypingIndicator(); if (response.ok) { this.addMessage(data.answer, 'bot'); this.conversationHistory.push({ role: 'bot', content: data.answer }); } else { this.addMessage('Sorry, I encountered an error. Please try again.', 'bot'); console.error('Error:', data.error); } } catch (error) { this.hideTypingIndicator(); this.addMessage('Sorry, I\'m having trouble connecting. Please check your connection and try again.', 'bot'); console.error('Network error:', error); } } addMessage(content, sender) { const messageDiv = document.createElement('div'); messageDiv.className = `message ${sender}`; const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); messageDiv.innerHTML = ` `; this.chatContainer.appendChild(messageDiv); this.scrollToBottom(); } showTypingIndicator() { this.isTyping = true; const typingDiv = document.createElement('div'); typingDiv.className = 'typing-indicator'; typingDiv.id = 'typing-indicator'; typingDiv.innerHTML = `