🎤 Voice Chat with Your Botpress Agent
Start Talking
https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
const botpressUrl = ‘https://bots.botpress.cloud/api/v1/bots/e386c5a7-ae1b-4f79-9d44-e2fb20423e12’;
const logDiv = document.getElementById(‘log’);
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = ‘fr-FR’;
speechSynthesis.speak(utterance);
}
function listenAndSend() {
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = ‘fr-FR’;
recognition.interimResults = false;
recognition.onresult = async function(event) {
const userText = event.results[0][0].transcript;
logDiv.innerHTML += `
Vous: ${userText}
`;
try {
const res = await axios.post(botpressUrl + ‘/users/user123/messages’, {
type: ‘text’,
text: userText
});
const reply = res.data.responses?.[0]?.payload?.text || ‘(Pas de réponse)’;
logDiv.innerHTML += `
Bot: ${reply}
`;
speak(reply);
} catch (err) {
logDiv.innerHTML += `
Erreur lors de la requête.
`;
console.error(err);
}
};
recognition.onerror = (e) => console.error(‘Recognition error:’, e);
recognition.start();
}
document.getElementById(‘start’).addEventListener(‘click’, listenAndSend);