Skip to content

Commit e0f5413

Browse files
authored
Chabot- Part1
0 parents  commit e0f5413

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

app.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const chatBody = document.querySelector(".chat-body");
2+
const txtInput = document.querySelector("#txtInput");
3+
const send = document.querySelector(".send");
4+
5+
send.addEventListener("click", () => renderUserMessage());
6+
7+
txtInput.addEventListener("keyup", (event) => {
8+
if (event.keyCode === 13) {
9+
renderUserMessage();
10+
}
11+
});
12+
13+
const renderUserMessage = () => {
14+
const userInput = txtInput.value;
15+
renderMessageEle(userInput, "user");
16+
txtInput.value = "";
17+
setTimeout(() => {
18+
renderChatbotResponse(userInput);
19+
setScrollPosition();
20+
}, 600);
21+
};
22+
23+
const renderChatbotResponse = (userInput) => {
24+
const res = getChatbotResponse(userInput);
25+
renderMessageEle(res);
26+
};
27+
28+
const renderMessageEle = (txt, type) => {
29+
let className = "user-message";
30+
if (type !== "user") {
31+
className = "chatbot-message";
32+
}
33+
const messageEle = document.createElement("div");
34+
const txtNode = document.createTextNode(txt);
35+
messageEle.classList.add(className);
36+
messageEle.append(txtNode);
37+
chatBody.append(messageEle);
38+
};
39+
40+
const getChatbotResponse = (userInput) => {
41+
return responseObj[userInput] == undefined
42+
? "Please try something else"
43+
: responseObj[userInput];
44+
};
45+
46+
const setScrollPosition = () => {
47+
if (chatBody.scrollHeight > 0) {
48+
chatBody.scrollTop = chatBody.scrollHeight;
49+
}
50+
};

images/chatbot.png

3.4 KB
Loading

images/cwt.jpg

23.4 KB
Loading

images/send.svg

Lines changed: 3 additions & 0 deletions
Loading

index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Chatbot</title>
8+
<link rel="stylesheet" href="style.css" />
9+
<script src="response.js" defer></script>
10+
<script src="app.js" defer></script>
11+
</head>
12+
<body>
13+
<div class="container">
14+
<div class="chat-header">
15+
<div class="logo">
16+
<img src="images/cwt.jpg" alt="cwt" />
17+
</div>
18+
<div class="title">Let's Chat</div>
19+
</div>
20+
<div class="chat-body"></div>
21+
<div class="chat-input">
22+
<div class="input-sec">
23+
<input type="text" id="txtInput" placeholder="Typ here" autofocus />
24+
</div>
25+
<div class="send">
26+
<img src="images/send.svg" alt="send" />
27+
</div>
28+
</div>
29+
</div>
30+
</body>
31+
</html>

response.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const responseObj = {
2+
hello: "Hey ! How are you doing ?",
3+
hey: "Hey! What's Up",
4+
today: new Date().toDateString(),
5+
time: new Date().toLocaleTimeString(),
6+
};

style.css

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Poppins:wght@200;300&display=swap");
2+
* {
3+
margin: 0;
4+
padding: 0;
5+
box-sizing: border-box;
6+
font-family: "Poppins", sans-serif;
7+
}
8+
body {
9+
background: #4b5c66;
10+
}
11+
.container {
12+
--light-color: #fff;
13+
height: 420px;
14+
width: 350px;
15+
background: var(--light-color);
16+
position: fixed;
17+
bottom: 50px;
18+
right: 10px;
19+
box-shadow: 0px 0px 15px 0px black;
20+
}
21+
.chat-header {
22+
height: 60px;
23+
display: flex;
24+
align-items: center;
25+
padding: 0px 30px;
26+
background-color: #0652c0;
27+
color: var(--light-color);
28+
font-size: 1.5rem;
29+
}
30+
31+
.chat-header .logo {
32+
height: 35px;
33+
width: 35px;
34+
box-shadow: 0px 0px 10px 0px black;
35+
}
36+
.chat-header img {
37+
height: 100%;
38+
width: 100%;
39+
}
40+
.chat-header .title {
41+
padding-left: 10px;
42+
}
43+
.chat-body {
44+
height: 300px;
45+
display: flex;
46+
flex-direction: column;
47+
padding: 8px 10px;
48+
align-items: flex-end;
49+
overflow-y: auto;
50+
}
51+
.chat-input {
52+
height: 60px;
53+
display: flex;
54+
align-items: center;
55+
border-top: 1px solid #ccc;
56+
}
57+
.input-sec {
58+
flex: 9;
59+
}
60+
.send {
61+
flex: 1;
62+
padding-right: 4px;
63+
}
64+
#txtInput {
65+
line-height: 30px;
66+
padding: 8px 10px;
67+
border: none;
68+
outline: none;
69+
caret-color: black;
70+
font-size: 1rem;
71+
width: 100%;
72+
}
73+
74+
.chatbot-message,
75+
.user-message {
76+
padding: 8px;
77+
background: #ccc;
78+
margin: 5px;
79+
width: max-content;
80+
border-radius: 10px 3px 10px 10px;
81+
}
82+
.chatbot-message {
83+
background: #0652c0;
84+
color: var(--light-color);
85+
align-self: flex-start;
86+
border-radius: 10px 10px 3px 10px;
87+
}

0 commit comments

Comments
 (0)