Academic Chatbot

Academic Chatbot

body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
text-align: center;
padding: 20px;
}
.credit {
font-size: 16px;
margin-bottom: 10px;
font-weight: bold;
}
.chat-container {
max-width: 400px;
width: 100%;
background: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
overflow: hidden;
display: flex;
flex-direction: column;
}
.chat-header {
background: #007bff;
color: white;
padding: 10px;
text-align: center;
font-size: 18px;
}
.chat-body {
height: 300px;
overflow-y: auto;
padding: 10px;
}
.chat-message {
padding: 8px;
border-radius: 5px;
margin-bottom: 5px;
}
.user-message {
background: #d1e7dd;
align-self: flex-end;
}
.bot-message {
background: #e9ecef;
}
.chat-footer {
display: flex;
padding: 10px;
border-top: 1px solid #ccc;
}
.chat-footer input {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px;
}
.chat-footer button {
background: #007bff;
color: white;
border: none;
padding: 8px 12px;
margin-left: 5px;
border-radius: 5px;
cursor: pointer;
}

This chatbot is developed by Dr. Muhammad Asim Rafiq

Academic Chatbot
Hello! Ask me any academic question.

const API_KEY = “YOUR_API_KEY”; // Replace with your actual API key
const chatBody = document.getElementById(“chatBody”);

function sendMessage() {
let userInput = document.getElementById(“userInput”).value.trim();
if (!userInput) return;

// Display user message
let userMessage = document.createElement(“div”);
userMessage.className = “chat-message user-message”;
userMessage.innerText = userInput;
chatBody.appendChild(userMessage);
chatBody.scrollTop = chatBody.scrollHeight;

// Call Gemini API for response
getBotResponse(userInput);

// Clear input field
document.getElementById(“userInput”).value = “”;
}

async function getBotResponse(question) {
let botMessage = document.createElement(“div”);
botMessage.className = “chat-message bot-message”;
botMessage.innerText = “Thinking…”;
chatBody.appendChild(botMessage);
chatBody.scrollTop = chatBody.scrollHeight;

try {
let response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}`, {
method: “POST”,
headers: { “Content-Type”: “application/json” },
body: JSON.stringify({
contents: [{ parts: [{ text: question }] }]
})
});

let data = await response.json();
if (data && data.candidates && data.candidates.length > 0) {
botMessage.innerText = data.candidates[0].content.parts[0].text;
} else {
botMessage.innerText = “I couldn’t find an answer, please try again.”;
}
} catch (error) {
botMessage.innerText = “Error fetching response.”;
}

chatBody.scrollTop = chatBody.scrollHeight;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *