Skip to content

Commit 4ab7a15

Browse files
Add files via upload
1 parent cbf0943 commit 4ab7a15

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>Word Search Chatbot</title>
7+
<link rel="stylesheet" href="styles.css">
8+
</head>
9+
<body>
10+
<div id="chatbot">
11+
<div id="header">
12+
<span class="chat-title"><b>PICTOPEDIA </b><BR><P>Word Search Chatbot Using Wikipedia API</P></span>
13+
</div>
14+
<div id="chat-output"></div>
15+
<div id="input-container">
16+
<input type="text" id="user-input" placeholder="Ask about word ">
17+
<button onclick="handleUserInput()">Search</button>
18+
</div>
19+
</div>
20+
<script src="script.js"></script>
21+
</body>
22+
</html>

script.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function handleUserInput() {
2+
const userInput = document.getElementById('user-input').value.trim();
3+
const chatOutput = document.getElementById('chat-output');
4+
5+
if (userInput === "") {
6+
chatOutput.innerHTML += `<div class="bot-message">Please enter a word to search.</div>`;
7+
return;
8+
}
9+
10+
chatOutput.innerHTML += `<div class="user-message">${userInput}</div>`;
11+
fetchWikipediaData(userInput);
12+
document.getElementById('user-input').value = '';
13+
chatOutput.scrollTop = chatOutput.scrollHeight;
14+
}
15+
16+
function fetchWikipediaData(query) {
17+
const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(query)}`;
18+
19+
fetch(url)
20+
.then(response => response.json())
21+
.then(data => {
22+
const chatOutput = document.getElementById('chat-output');
23+
if (data.extract) {
24+
let imageHTML = data.thumbnail ? `<img src="${data.thumbnail.source}" alt="${query}" style="max-width: 100%; height: auto; border-radius: 5px;">` : '';
25+
chatOutput.innerHTML += `<div class="bot-message">${imageHTML}<p>${data.extract}</p></div>`;
26+
} else {
27+
chatOutput.innerHTML += `<div class="bot-message">Sorry, I couldn't find on that word "${query}".</div>`;
28+
}
29+
chatOutput.scrollTop = chatOutput.scrollHeight;
30+
})
31+
.catch(error => {
32+
console.error('Error:', error);
33+
const chatOutput = document.getElementById('chat-output');
34+
chatOutput.innerHTML += `<div class="bot-message">There was an error retrieving the information.</div>`;
35+
chatOutput.scrollTop = chatOutput.scrollHeight;
36+
});
37+
}

styles.css

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
body {
2+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
3+
background-color: #ece5dd;
4+
display: flex;
5+
justify-content: center;
6+
align-items: center;
7+
height: 100vh;
8+
margin: 0px;
9+
}
10+
11+
#chatbot {
12+
width: 350px;
13+
max-width: 100%;
14+
height: 500px;
15+
display: flex;
16+
flex-direction: column;
17+
background-color: #fff;
18+
border-radius: 10px;
19+
box-shadow: 10px 5px 10px rgba(0, 0, 0, 0.2);
20+
overflow: hidden;
21+
}
22+
23+
#header {
24+
background-color: #075e54;
25+
padding: 10px;
26+
color: #fff;
27+
text-align: center;
28+
}
29+
30+
#chat-output {
31+
flex: 1;
32+
background-color: #e5ddd5;
33+
padding: 10px 20px;
34+
overflow-y: auto;
35+
display: flex;
36+
flex-direction: column;
37+
}
38+
39+
#chat-output div {
40+
margin: 5px 0;
41+
padding: 10px;
42+
border-radius: 7.5px;
43+
max-width: 70%;
44+
}
45+
46+
#chat-output div.user-message {
47+
background-color: #dcf8c6;
48+
align-self: flex-end; /* User messages to the right */
49+
}
50+
51+
#chat-output div.bot-message {
52+
background-color: #fff;
53+
align-self: flex-start; /* Bot messages to the left */
54+
}
55+
56+
#input-container {
57+
display: flex;
58+
align-items: center;
59+
padding: 10px;
60+
border-top: 1px solid #ccc;
61+
background-color: #f7f7f7;
62+
}
63+
64+
#user-input {
65+
flex: 1;
66+
padding: 10px;
67+
border: 1px solid #ccc;
68+
border-radius: 20px;
69+
outline: none;
70+
margin-right: 10px;
71+
}
72+
73+
button {
74+
padding: 10px;
75+
border: none;
76+
background-color: #114825;
77+
color: white;
78+
border-radius: 10%;
79+
cursor: pointer;
80+
transition: background-color 0.3s;
81+
}
82+
83+
button:hover {
84+
background-color: #3a6b4a;
85+
}
86+
87+
button:focus {
88+
outline: none;
89+
}

0 commit comments

Comments
 (0)