TextToSpeechJs Callback has confusing behavior

I’m trying to use the dynamic text to speech (TextToSpeechJS)

I have a while loop with a listener and a dynamic text to speech that loops until a valid response is heard.

Pseudo code…
while( !validRes ){ if( input = a valid response ) validRes = true textToSpeech() }

Text to speech code…
(callback) => { if( !validRes ) callback("Invalid response please try again"); }

If I don’t first try a invalid response it gets stuck on the callback. If I enter an invalid response and then a valid response the code continues as expected.

This fixed the problem…
(callback) => { if( !validRes ) callback("Invalid response please try again"); else callback(""); }

This seems a bit wonky, can someone give me a reason for this or is it a bug?

Hey @duncan.smith-freedma!

I am not immediately certain what may be going on based off the code you posted. It seems like the first text to speech callback will only work if you pass in some response, then it becomes defined and will only evaluate properly on any second response.

Can you post the files you are working with, or post a test version of your skill to github so I can take a closer look at in context? What is happening in the console logs and the text simulator when you try to run the skill? Try adding console.log(); to your code to help give a better idea what is happening within your skill.

Be sure to check out this section of our API docs for more detailed information TTS

And the TextToSpeech behavior

Thanks!

Text to Speech:
(callback) => {
console.log(“callback”);
if(!notepad.validRes){
callback(“Sorry I didn’t understand that, can you try again?”);
}
else{
callback("");
}
console.log(“callback end”);
}

sequence:

() => {
console.log(“into loop”);
notepad.validRes = false;
}

() => {
return !notepad.validRes;
}

Listen:

(listener) => {
listener.on(‘cloud’, function(asrResult, speakerIds) {
console.log(“it’s not the if statement”);
if(asrResult.NLParse.status === “GOT-PARSE”) {
if(asrResult.NLParse.music === “yes” | asrResult.NLParse.music === “no”){
console.log(“yes or no”);
notepad.music = (asrResult.NLParse.music === “yes”);
notepad.validRes = true;
}
}
notepad.continue = true;
});
}

() => {
notepad.continue = false;
}

() => {
return !notepad.continue;
}

Hi @duncan.smith-freedma,

It seems like part of the problem may be related to how the logic within and between your behaviors are playing out, and how this affects what states your behaviors are returning. If you can upload your .bt file here, take a screenshot of your behavior tree, and/or post your console logs, I can take a closer look at it for you.

TextToSpeechJS behaviors will return a ‘FAILED’ state if the ‘callback()’ function is not called. By having the ‘if’ statement containing the only ‘callback()’ function, it seems like the behavior will only succeed in this state by first running through with an invalid response. Then when it runs through the second time, it retains the ‘SUCCESS’ state instead or flipping back to ‘IN_PROGRESS’ after looping and is evaluated a second time.

I’m looking into this further to see if this working as designed or if this is indeed a bug.

Try setting up your code to console.log() your notepad variables as they are being defined and evaluated. Place execute scripts beteeen behaviors with ‘console.log()’ to track where behaviors fail and get stuck. This will give you an idea of where the code and behaviors may be breaking and if this is the same thing you are seeing on your end.

I recommend moving your if statements out of the ‘Listen’ and TextToSpeechJS behaviors. Have your Listen behavior parse the speech and have TextToSpeechJS create a string dynamically using variable strings (i.e callback("Cool, I'll play " + notepad.songTitle);. This will allow you to control branching logic with ‘Switchs’ and ‘Case’ decorators and understand how your behavior trees are executing you code. This is a preference, it helps me keep isolate and track problems.