-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (57 loc) · 2.17 KB
/
Copy pathscript.js
File metadata and controls
66 lines (57 loc) · 2.17 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
// Get chat popup and body elements
const chatPopup = document.getElementById('chatPopup');
const chatBody = document.getElementById('chatBody');
const chatButtonContainer = document.getElementById('chatButtonContainer');
// Open the chat popup
function openChat() {
chatPopup.style.display = 'block';
}
// Close the chat popup
function closeChat() {
chatPopup.style.display = 'none';
}
// Load the chat button dynamically
function loadChatButton() {
fetch('chat-button.html')
.then(response => response.text())
.then(data => {
chatButtonContainer.innerHTML = data;
})
.catch(error => console.error('Error loading chat button:', error));
}
// Start voice recognition
function startVoiceRecognition() {
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = 'en-US';
recognition.start();
recognition.onresult = (event) => {
const userText = event.results[0][0].transcript;
displayMessage(userText, 'user');
generateBotResponse(userText);
};
recognition.onerror = (event) => {
console.error('Voice recognition error:', event.error);
};
}
// Display a message in the chat
function displayMessage(message, sender) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('chat-message', sender === 'user' ? 'user-message' : 'bot-message');
messageDiv.textContent = message;
chatBody.appendChild(messageDiv);
chatBody.scrollTop = chatBody.scrollHeight; // Scroll to the latest message
}
// Generate a bot response
function generateBotResponse(userText) {
let botResponse = "I'm not sure how to respond to that.";
if (userText.toLowerCase().includes('hello')) {
botResponse = 'Hello! How can I assist you today?';
} else if (userText.toLowerCase().includes('weather')) {
botResponse = 'The weather is great today!';
} else if (userText.toLowerCase().includes('time')) {
botResponse = `The current time is ${new Date().toLocaleTimeString()}.`;
}
displayMessage(botResponse, 'bot');
}
// Load the chat button when the page loads
window.onload = loadChatButton;