voicemenu143 / templates /index.html
lokesh341's picture
Update templates/index.html
b17db49 verified
raw
history blame
6.16 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Biryani Hub - Voice Login & Register</title>
<script>
let recognition;
let nameCaptured = "";
let emailCaptured = "";
let mobileCaptured = "";
if ('webkitSpeechRecognition' in window) {
recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = 'en-US';
} else {
alert("Speech Recognition API not supported in this browser.");
}
function speak(text, callback) {
const speech = new SpeechSynthesisUtterance(text);
speech.onend = callback;
window.speechSynthesis.speak(speech);
}
function askLoginOrRegister() {
speak("Are you a new customer or an existing customer?", function() {
recognition.start();
recognition.onresult = function(event) {
let response = event.results[0][0].transcript.trim().toLowerCase();
recognition.stop();
if (response.includes("new")) {
checkIfUserExists();
} else if (response.includes("existing")) {
showLoginForm();
} else {
speak("Please say 'new' for registration or 'existing' for login.", askLoginOrRegister);
}
};
});
}
function checkIfUserExists() {
var email = document.getElementById('email').value;
var phone = document.getElementById('mobile').value;
fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: email, phone_number: phone })
})
.then(response => response.json())
.then(data => {
if (data.success) {
speak("You are already registered. Logging you in now.");
autoLoginSubmit();
} else {
speak("Please provide your details for registration.");
showRegistrationForm();
}
});
}
function showRegistrationForm() {
speak("Please tell me your name.", startListeningForName);
}
function startListeningForName() {
recognition.start();
recognition.onresult = function(event) {
nameCaptured = event.results[0][0].transcript.trim();
document.getElementById('name').value = nameCaptured;
recognition.stop();
speak("You said " + nameCaptured + ". Is it correct?", confirmName);
};
}
function confirmName() {
recognition.start();
recognition.onresult = function(event) {
let confirmation = event.results[0][0].transcript.trim().toLowerCase();
recognition.stop();
if (confirmation.includes("ok")) {
speak("Now, tell me your email.", startListeningForEmail);
} else {
speak("Let's try again. Tell me your name.", startListeningForName);
}
};
}
function startListeningForEmail() {
recognition.start();
recognition.onresult = function(event) {
emailCaptured = event.results[0][0].transcript.trim().replace(/\bat\b/g, '@').replace(/\s+/g, '');
document.getElementById('email').value = emailCaptured;
recognition.stop();
speak("You said " + emailCaptured + ". Is it correct?", confirmEmail);
};
}
function confirmEmail() {
recognition.start();
recognition.onresult = function(event) {
let confirmation = event.results[0][0].transcript.trim().toLowerCase();
recognition.stop();
if (confirmation.includes("ok")) {
speak("Now, tell me your mobile number.", startListeningForMobile);
} else {
speak("Let's try again. Tell me your email.", startListeningForEmail);
}
};
}
function startListeningForMobile() {
recognition.start();
recognition.onresult = function(event) {
mobileCaptured = event.results[0][0].transcript.trim().replace(/\s+/g, '');
document.getElementById('mobile').value = mobileCaptured;
recognition.stop();
speak("You said " + mobileCaptured + ". Is it correct?", confirmMobile);
};
}
function confirmMobile() {
recognition.start();
recognition.onresult = function(event) {
let confirmation = event.results[0][0].transcript.trim().toLowerCase();
recognition.stop();
if (confirmation.includes("ok")) {
autoSubmit();
} else {
speak("Let's try again. Tell me your mobile number.", startListeningForMobile);
}
};
}
function autoSubmit() {
fetch('/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: nameCaptured, email: emailCaptured, phone: mobileCaptured })
})
.then(response => response.json())
.then(data => {
if (data.success) {
speak("Registration complete. Thank you!");
} else {
speak(data.message);
}
});
}
window.onload = function () {
askLoginOrRegister();
};
</script>
</head>
<body>
</body>
</html>