Skip to content

Commit 7f6fedd

Browse files
Chat-application is pushed for the first time
1 parent f4e4a9b commit 7f6fedd

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Ai Chatbot</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<div class="chat-container">
11+
<div class="chat-header">
12+
<h1>AI Chatbot</h1>
13+
</div>
14+
<div class="chat-messages" id="chat-messages">
15+
<div class="message bot-message">
16+
<p>Hello! I'm your AI assistant. How can I help you today?</p>
17+
</div>
18+
</div>
19+
<div class="chat-input">
20+
<input type="text" id="user-input" placeholder="Type your message here...">
21+
<button id="send-button">Send</button>
22+
</div>
23+
</div>
24+
25+
<script src="script.js"></script>
26+
</body>
27+
</html>

script.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const chatMessages = document.getElementById("chat-messages");
3+
const userInput = document.getElementById("user-input");
4+
const sendButton = document.getElementById("send-button");
5+
6+
const botResponses = {
7+
hello: "Hello! How can I help you today?",
8+
hi: "Hi there! How can I assist you?",
9+
"how are you": "I'm doing well, thank you! How about you?",
10+
"what can you do": "I can answer simple questions and have basic conservations. Try asking something else. ",
11+
"i am fine": "Great! need any assistance?",
12+
bye: "Goodbye! Have a great day!",
13+
default: "I'm not sure I understand. Could you try asking something else?"
14+
};
15+
16+
//Function to add a message to the chat
17+
function addMessage(message, isUser = false) {
18+
const messageDiv = document.createElement("div");
19+
messageDiv.classList.add("message");
20+
messageDiv.classList.add(isUser ? "user-message" : "bot-message");
21+
22+
const messageText = document.createElement("p");
23+
messageText.textContent = message;
24+
messageDiv.appendChild(messageText);
25+
26+
chatMessages.appendChild(messageDiv)
27+
chatMessages.scrollTop = chatMessages.scrollHeight;
28+
}
29+
30+
//Function to get bot response
31+
function getBotResponse(userMessage) {
32+
const lowerMessage = userMessage.toLowerCase();
33+
34+
for (const [key, value] of Object.entries(botResponses)) {
35+
if (lowerMessage.includes(key)) {
36+
return value;
37+
}
38+
}
39+
return botResponses.default;
40+
}
41+
42+
//Function to handle sending messages
43+
function sendMessage() {
44+
const message = userInput.value.trim();
45+
if (message) {
46+
addMessage(message, true);
47+
userInput.value = "";
48+
49+
setTimeout(() => {
50+
const botResponse = getBotResponse(message);
51+
addMessage(botResponse);
52+
}, 500);
53+
}
54+
}
55+
56+
//Event listeners
57+
sendButton.addEventListener("click",sendMessage);
58+
59+
userInput.addEventListener("keypress", (e) => {
60+
if (e.key === "Enter") {
61+
sendMessage();
62+
}
63+
});
64+
});

style.css

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
box-sizing: border-box;
5+
font-family: Arial, Helvetica, sans-serif;
6+
}
7+
8+
body {
9+
display: flex;
10+
justify-content: center;
11+
align-items: center;
12+
min-height: 100vh;
13+
background-color: #f0f2f5;
14+
}
15+
16+
.chat-container {
17+
width: 400px;
18+
height: 600px;
19+
background-color: #ffffff;
20+
border-radius: 10px;
21+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
22+
display: flex;
23+
flex-direction: column;
24+
}
25+
26+
.chat-header {
27+
padding: 20px;
28+
background-color: #075e54;
29+
color: white;
30+
border-radius: 10px 10px 0 0;
31+
}
32+
33+
.chat-header h1 {
34+
font-size: 1.5rem;
35+
text-align: center;
36+
}
37+
38+
.chat-messages {
39+
flex: 1;
40+
padding: 20px;
41+
overflow-y: auto;
42+
}
43+
44+
.message {
45+
margin-bottom: 15px;
46+
max-width: 80%;
47+
padding: 10px 15px;
48+
border-radius: 15px;
49+
word-wrap: break-word;
50+
}
51+
52+
.bot-message {
53+
background-color: #e9ecef;
54+
align-self: flex-start;
55+
border-radius: 15px 15px 15px 0;
56+
}
57+
58+
.user-message {
59+
background-color: #075e54;
60+
color: white;
61+
margin-left: auto;
62+
border-radius: 15px 15px 0 15px;
63+
}
64+
65+
.chat-input {
66+
padding: 20;
67+
display: flex;
68+
gap: 10px;
69+
border-top: 1px solid #e9ecef;
70+
}
71+
72+
#user-input {
73+
flex: 1;
74+
padding: 10px;
75+
border: 1px solid #e9ecef;
76+
border-radius: 5px;
77+
outline: none;
78+
}
79+
80+
#send-button {
81+
padding: 10px 20px;
82+
background-color: #075e54;
83+
color: white;
84+
border: none;
85+
border-radius: 5px;
86+
cursor: pointer;
87+
transition: background-color 0.3s;
88+
}
89+
90+
#send-button:hover {
91+
background-color: #128c7e;
92+
}

0 commit comments

Comments
 (0)