Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
257 lines (212 loc) · 6.43 KB
/
Copy pathscript.js
File metadata and controls
257 lines (212 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const chatMessages = document.querySelector("#chatMessages");
const chatForm = document.querySelector("#chatForm");
const messageInput = document.querySelector("#messageInput");
const clearChat = document.querySelector("#clearChat");
const themeToggle = document.querySelector("#themeToggle");
const starterMessages = [
{
role: "bot",
text: "Hello! How can I help you today?",
},
// {
// role: "user",
// text: "Give me information of this platform?",
// },
// {
// role: "bot",
// text: "Aichatter is a polished chatbot landing interface with a responsive layout, animated replies, theme switching, auto-scrolling conversations, and a ready API integration point for your own bot service.",
// },
// {
// role: "user",
// text: "How does it work?",
// },
];
function initIcons() {
if (window.lucide) {
window.lucide.createIcons();
}
}
function setTheme(theme) {
document.documentElement.dataset.theme = theme;
localStorage.setItem("aichatter-theme", theme);
const icon = theme === "light" ? "moon" : "sun";
themeToggle.innerHTML = `<i data-lucide="${icon}"></i>`;
initIcons();
}
function getInitialTheme() {
const savedTheme = localStorage.getItem("aichatter-theme");
if (savedTheme) return savedTheme;
return window.matchMedia("(prefers-color-scheme: light)").matches
? "light"
: "dark";
}
function scrollToBottom() {
requestAnimationFrame(() => {
chatMessages.scrollTo({
top: chatMessages.scrollHeight,
behavior: "smooth",
});
});
}
function createAvatar(role) {
const avatar = document.createElement("div");
avatar.className = "avatar";
avatar.innerHTML = `<i data-lucide="${
role === "user" ? "user" : "bot"
}"></i>`;
return avatar;
}
function appendMessage(role, text) {
const row = document.createElement("div");
row.className = `message-row ${role}`;
const avatar = createAvatar(role);
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.textContent = text;
row.append(avatar, bubble);
chatMessages.appendChild(row);
initIcons();
scrollToBottom();
return row;
}
function appendTyping() {
const row = document.createElement("div");
row.className = "message-row bot";
row.dataset.typing = "true";
row.appendChild(createAvatar("bot"));
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.innerHTML =
'<span class="typing"><span></span><span></span><span></span></span>';
row.appendChild(bubble);
chatMessages.appendChild(row);
initIcons();
scrollToBottom();
return row;
}
function typeReply(text) {
const row = document.createElement("div");
row.className = "message-row bot";
row.appendChild(createAvatar("bot"));
const bubble = document.createElement("div");
bubble.className = "bubble";
row.appendChild(bubble);
chatMessages.appendChild(row);
initIcons();
let index = 0;
const speed = Math.max(8, Math.min(26, 900 / text.length));
const interval = window.setInterval(() => {
bubble.textContent = text.slice(0, index + 1);
index += 1;
scrollToBottom();
if (index >= text.length) {
window.clearInterval(interval);
}
}, speed);
}
// async function getBotReply(userMessage) {
// // Add your chatbot API here.
// Example:
// const response = await fetch("YOUR_CHATBOT_API_URL", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// "Authorization": "Bearer YOUR_API_KEY"
// },
// body: JSON.stringify({ message: userMessage })
// });
// const data = await response.json();
// return data.reply;
// await new Promise((resolve) => window.setTimeout(resolve, 900));
// return `I received: "${userMessage}". Connect your bot API inside getBotReply() and this area will show the live assistant response with the same animation.`;
// }
const GEMINI_API_KEY = "YOUR_GEMINI_API_KEY"; // Replace with your actual API key
const GEMINI_MODEL = "gemini-2.5-flash";
async function getBotReply(userMessage) {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${GEMINI_MODEL}:generateContent`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-goog-api-key": GEMINI_API_KEY
},
body: JSON.stringify({
contents: [
{
role: "user",
parts: [{ text: userMessage }]
}
]
})
}
);
const data = await response.json();
if (!response.ok) {
console.error(data);
return "Gemini API error. Please check your API key, model name, or billing/settings.";
}
return (
data?.candidates?.[0]?.content?.parts?.[0]?.text ||
"Gemini did not return a text reply."
);
}
async function handleSubmit(event) {
event.preventDefault();
const message = messageInput.value.trim();
if (!message) return;
appendMessage("user", message);
messageInput.value = "";
messageInput.focus();
const typingRow = appendTyping();
try {
const reply = await getBotReply(message);
typingRow.remove();
typeReply(reply || "I could not find a reply from the chatbot API.");
} catch (error) {
typingRow.remove();
typeReply(
"The chatbot API did not respond. Please check your endpoint, API key, and response format."
);
console.error(error);
}
}
function resetChat() {
chatMessages.innerHTML = "";
starterMessages.forEach((message) =>
appendMessage(message.role, message.text)
);
}
themeToggle.addEventListener("click", () => {
const currentTheme = document.documentElement.dataset.theme || "dark";
setTheme(currentTheme === "dark" ? "light" : "dark");
});
chatForm.addEventListener("submit", handleSubmit);
clearChat.addEventListener("click", resetChat);
setTheme(getInitialTheme());
resetChat();
initIcons();
// Footer
const backToTopBtn = document.querySelector("#backToTop");
const footerNewsletter = document.querySelector(".footer-newsletter");
if (backToTopBtn) {
backToTopBtn.addEventListener("click", () => {
window.scrollTo({
top: 0,
behavior: "smooth"
});
});
}
if (footerNewsletter) {
footerNewsletter.addEventListener("submit", (event) => {
event.preventDefault();
const input = footerNewsletter.querySelector("input");
const email = input.value.trim();
if (!email) return;
input.value = "";
input.placeholder = "Subscribed successfully";
});
}
if (window.lucide) {
window.lucide.createIcons();
}