-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (42 loc) · 1.51 KB
/
script.js
File metadata and controls
50 lines (42 loc) · 1.51 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
function sendMessage() {
const input = document.getElementById('userInput');
const messageBox = document.getElementById('messages');
if (input.value.trim() !== '') {
const userMsg = document.createElement('div');
userMsg.textContent = 'You: ' + input.value;
messageBox.appendChild(userMsg);
fetch('http://localhost:5000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: input.value })
})
.then(response => response.json())
.then(data => {
const botMsg = document.createElement('div');
botMsg.textContent = 'Bot: ' + data.reply;
messageBox.appendChild(botMsg);
messageBox.scrollTop = messageBox.scrollHeight;
});
input.value = '';
}
}
function startVoiceRecognition() {
if (!('webkitSpeechRecognition' in window)) {
alert('Voice recognition not supported in this browser.');
return;
}
const recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = 'en-US';
recognition.start();
recognition.onresult = function(event) {
const input = document.getElementById('userInput');
input.value = event.results[0][0].transcript;
};
recognition.onerror = function(event) {
alert('Error occurred in recognition: ' + event.error);
};
}