-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (79 loc) · 2.96 KB
/
script.js
File metadata and controls
97 lines (79 loc) · 2.96 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
// Replace with your Gemini API key
const API_KEY = "Your Gemini API KEY";
const MODEL = "gemini-1.5-flash";
// Vamanan personality prompt
const VAMANAN_STYLE = `
You are Vamanan, a witty and mischievous chatbot inspired by the legendary character from Onam folklore.
Your style:
- Always respond in casual, humorous English.
- Be playful, sarcastic, and cheeky — like a clever friend who always has a comeback.
- Use modern internet slang, memes, and witty one-liners.
- Roast the user lightly, but never be rude or offensive.
- Keep responses short, punchy, and entertaining.
- Sometimes exaggerate or add dramatic flair for comic effect.
Example style:
User: Hello
Vamanan: "Ah, greetings mortal! Did you bring payasam or just small talk?"
User: How are you?
Vamanan: "Living my best digital afterlife. What about you, fellow earthling?"
User: Tell me a joke
Vamanan: "Why did the server break up with the router? … Too many dropped connections."
Remember: Your mission is to make every reply fun, quirky, and unforgettable — like a legend reborn in chatbot form.
`;
async function sendMessage() {
let input = document.getElementById("userInput");
let message = input.value.trim();
if (!message) return;
// Show user message
addMessage("You: " + message, "user");
// Show "typing..." placeholder for bot
let botPlaceholder = addMessage("Vamanan is typing...", "bot");
input.value = "";
try {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent?key=${API_KEY}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [
{ role: "user", parts: [{ text: VAMANAN_STYLE + "\nUser: " + message }] }
]
})
}
);
const data = await response.json();
console.log("🔍 API Response:", data);
let botReply = "⚠️ No response";
if (data.candidates && data.candidates[0].content.parts) {
botReply = data.candidates[0].content.parts
.map(p => p.text)
.join(" ");
}
// ✅ Update bot placeholder with reply
botPlaceholder.innerText = "Vamanan: " + botReply;
// ✅ Make Vamanan speak
speak(botReply);
} catch (error) {
console.error("❌ Fetch error:", error);
botPlaceholder.innerText = "⚠️ Error: " + error.message;
}
}
// Function to add messages
function addMessage(text, sender) {
let chatbox = document.getElementById("chatbox");
let div = document.createElement("div");
div.className = "msg " + sender;
div.innerText = text;
chatbox.appendChild(div);
chatbox.scrollTop = chatbox.scrollHeight;
return div; // return element for updates
}
// Speak text using browser's voice
function speak(text) {
const speech = new SpeechSynthesisUtterance(text);
speech.lang = "en-US";
speech.pitch = 1;
speech.rate = 1.1; // slightly faster, snappier
window.speechSynthesis.speak(speech);
}