Why isn't the callback working, the response voice I hear is what i just spoke to it but the program must have listened to what I have said and responded with the call back I assigned to it. The problem i'm guessing is on the postVoice function. The message is properly being recorded on terminal but the code is not interpreting the message to process a callback response. As a side note the "say($("#stuff").text())" line is the one that is working and giving the response but the "postVoice($("#stuff").text())" must have worked the same way as the "say($("#stuff").text())" but it is not.
function say(text) {
responsiveVoice.speak(text, "UK English Male")
}
function listen() {
var recognition = new webkitSpeechRecognition();
recognition.interimResults = true;
recognition.onresult = function(event) {
if (event.results.length > 0) {
$("#stuff").text(event.results[0][0].transcript)
}
}
recognition.onend = function(event) {
//say($("#stuff").text())
postVoice($("#stuff").text())
}
recognition.start();
}
function postVoice(message){
$.ajax({
type: "POST",
url: "http://localhost:8000/voice",
data: {
message: message
},
success: function(data) {
$("#stuff").text(data.message)
say(data.message)
}
})
}
server side
var express = require('express')
var app = express()
var path = require('path')
var bodyParser = require('body-parser')
app.use(express.static(path.join(__dirname,'../client')))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(bodyParser.text())
app.post('/voice', function(req, res) {
getResponse(req.body.message, function(response){
res.json({
message: response
})
})
})
function getResponse(message, callback){
if(message == "hello"){
callback("hey there")
} else if (message == "goodbye"){
callback("bye-bye")
} else {
callback(message)
}
}
app.listen(8000, function(){
console.log("Listening on Port 8000... " + path.join(__dirname, '../client'))
})