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
4
5
5
- // Voice Chatbot with p5.Speech
6
- // Edited Video: https://youtu.be/iFTgphKCP9U
6
+ // Code from Challenge: https://editor.p5js.org/codingtrain/sketches/QcY2Z36mJ
7
7
8
+ // This code has been adapted to use promises instead of callbacks since callbacks are deprecated in RiveScript.
8
9
function setup ( ) {
9
10
noCanvas ( ) ;
10
11
let speech = new p5 . Speech ( ) ;
11
- let speechRec = new p5 . SpeechRec ( ' en-US' , gotSpeech ) ;
12
+ let speechRec = new p5 . SpeechRec ( " en-US" , gotSpeech ) ;
12
13
let continuous = true ;
13
14
let interim = false ;
14
15
speechRec . start ( continuous , interim ) ;
15
16
16
17
let bot = new RiveScript ( ) ;
17
- bot . loadFile ( 'brain.rive' , brainReady , brainError ) ;
18
+
19
+ // changed callbacks to .then and .catch
20
+ bot . loadFile ( "brain.txt" ) . then ( brainReady ) . catch ( brainError ) ;
18
21
19
22
function brainReady ( ) {
20
- console . log ( ' Chatbot ready!' ) ;
23
+ console . log ( " Chatbot ready!" ) ;
21
24
bot . sortReplies ( ) ;
22
25
}
23
26
24
27
function brainError ( ) {
25
- console . log ( ' Chatbot error!' ) ;
28
+ console . log ( " Chatbot error!" ) ;
26
29
}
27
30
28
31
// let button = select('#submit');
@@ -31,13 +34,14 @@ function setup() {
31
34
32
35
// button.mousePressed(chat);
33
36
34
- function gotSpeech ( ) {
37
+ // Using async and await
38
+ async function gotSpeech ( ) {
35
39
if ( speechRec . resultValue ) {
36
40
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 ) ;
39
43
speech . speak ( reply ) ;
40
- //output.html(reply);
44
+ // output.html(reply);
41
45
}
42
46
}
43
47
0 commit comments