forked from yuvrajzingh/AI-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (86 loc) · 3.57 KB
/
script.js
File metadata and controls
105 lines (86 loc) · 3.57 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
document.addEventListener("DOMContentLoaded", () => {
const chatHistory = document.getElementById("chat-history");
const userInput = document.getElementById("user-input");
const sendButton = document.getElementById("send-button");
// Function to create a message bubble
function createMessageBubble(text, sender) {
const messageBubble = document.createElement("div");
messageBubble.classList.add("message", sender);
messageBubble.innerText = text;
chatHistory.appendChild(messageBubble);
chatHistory.scrollTop = chatHistory.scrollHeight; // Auto-scroll to latest message
}
// Function to handle user input
function handleUserInput() {
const message = userInput.value.trim();
if (message === "") return;
createMessageBubble(message, "user"); // Display user message
userInput.value = ""; // Clear input field
setTimeout(() => {
generateBotResponse(message);
}, 500); // Simulate bot typing delay
}
// Function to generate bot response
function generateBotResponse(userMessage) {
let botResponse = "I'm just a simple bot! Try saying 'hello' or 'help'.";
const responses = {
hello: "Hi there! How can I assist you today?",
help: "Sure! You can ask me about anything. Try 'What can you do?'",
bye: "Goodbye! Have a great day!",
"what is your name": "I'm Chatbot! What's yours?",
};
userMessage = userMessage.toLowerCase(); // Normalize input
for (const key in responses) {
if (userMessage.includes(key)) {
botResponse = responses[key];
break;
}
}
createMessageBubble(botResponse, "bot");
}
// Event Listeners
sendButton.addEventListener("click", handleUserInput);
userInput.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
handleUserInput();
}
});
});
// Mic-js
const micIcon = document.getElementById('micIcon');
const userInput = document.getElementById('user-input');
const transcriptionDisplay = document.getElementById('transcription');
let recognition;
if ('webkitSpeechRecognition' in window) {
recognition = new webkitSpeechRecognition();
recognition.lang = 'en-US';
recognition.continuous = true; // This allows continuous speech recognition
recognition.interimResults = true; // This will give real-time results
recognition.onstart = function() {
transcriptionDisplay.textContent = 'Listening...';
micIcon.classList.remove('fa-microphone'); // Change to active mic icon
micIcon.classList.add('fa-microphone-alt');
};
recognition.onresult = function(event) {
let transcript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
transcript += event.results[i][0].transcript;
}
// Display the result in the user input field
userInput.value = transcript; // Set the transcript to user input field
};
recognition.onerror = function(event) {
transcriptionDisplay.textContent = 'Error occurred: ' + event.error;
};
recognition.onend = function() {
transcriptionDisplay.textContent = 'Stopped listening.';
micIcon.classList.remove('fa-microphone-alt');
micIcon.classList.add('fa-microphone');
};
}
// Activate speech recognition when mic icon is clicked
micIcon.addEventListener('click', function() {
if (recognition && recognition.start) {
recognition.start();
}
});