1- // Daniel Shiffman
2- // http://codingtra.in
3- // http://patreon.com/codingtrain
1+ // Chat Bot
2+ // The Coding Train / Daniel Shiffman
3+ // https://thecodingtrain.com/challenges/80-voice-chatbot-with-p5speech
4+ // https://youtu.be/iFTgphKCP9U
45
5- // Voice Chatbot with p5.Speech
6- // Edited Video: https://youtu.be/iFTgphKCP9U
6+ // Code from Challenge: https://editor.p5js.org/codingtrain/sketches/QcY2Z36mJ
77
8+ // This code has been adapted to use promises instead of callbacks since callbacks are deprecated in RiveScript.
89function setup ( ) {
910 noCanvas ( ) ;
1011 let speech = new p5 . Speech ( ) ;
11- let speechRec = new p5 . SpeechRec ( ' en-US' , gotSpeech ) ;
12+ let speechRec = new p5 . SpeechRec ( " en-US" , gotSpeech ) ;
1213 let continuous = true ;
1314 let interim = false ;
1415 speechRec . start ( continuous , interim ) ;
1516
1617 let bot = new RiveScript ( ) ;
17- bot . loadFile ( 'brain.rive' , brainReady , brainError ) ;
18+
19+ // changed callbacks to .then and .catch
20+ bot . loadFile ( "brain.rive" ) . then ( brainReady ) . catch ( brainError ) ;
1821
1922 function brainReady ( ) {
20- console . log ( ' Chatbot ready!' ) ;
23+ console . log ( " Chatbot ready!" ) ;
2124 bot . sortReplies ( ) ;
2225 }
2326
2427 function brainError ( ) {
25- console . log ( ' Chatbot error!' ) ;
28+ console . log ( " Chatbot error!" ) ;
2629 }
2730
2831 // let button = select('#submit');
@@ -31,13 +34,14 @@ function setup() {
3134
3235 // button.mousePressed(chat);
3336
34- function gotSpeech ( ) {
37+ // Using async and await
38+ async function gotSpeech ( ) {
3539 if ( speechRec . resultValue ) {
3640 let input = speechRec . resultString ;
37- //user_input.value(input);
38- let reply = bot . reply ( ' local-user' , input ) ;
41+ // user_input.value(input);
42+ let reply = await bot . reply ( " local-user" , input ) ;
3943 speech . speak ( reply ) ;
40- //output.html(reply);
44+ // output.html(reply);
4145 }
4246 }
4347
0 commit comments