-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbasicCalculator.js
89 lines (85 loc) · 2.71 KB
/
basicCalculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Calculator Script - Basic Voice Script using all key parts of Violet
*/
var violet = require('violet').script();
// the voice script takes sometimes two values from the user
violet.addInputTypes({
"NumOne": "number",
"NumTwo": "number",
});
// define a controller to build out user request processing
var app = {
add: (a, b)=>{return parseInt(a)+parseInt(b); },
subtract: (a, b)=>{return parseInt(a)-parseInt(b); },
multiply: (a, b)=>{return parseInt(a)*parseInt(b); },
divide: (a, b)=>{return parseInt(a)/parseInt(b); }
}
// define the conversational flow and connect to the controller (last parameter)
violet.addFlowScript(`
<app>
<choice>
<expecting>What can you do</expecting>
<say>I can add, subtract, multiply, or divide two numbers</say>
</choice>
<choice id="add">
<expecting>I want to add</expecting>
<say>Sure</say>
<decision>
<prompt>What two numbers would you like me to add</prompt>
<prompt>What would you like me to add</prompt>
<choice>
<expecting>[[NumOne]] and [[NumTwo]]</expecting>
<say>The sum of [[NumOne]] and [[NumTwo]] is [[app.add(NumOne, NumTwo)]]</say>
</choice>
<choice>
<expecting>Cancel</expecting>
<say>Canceling Addition</say>
</choice>
</decision>
</choice>
<choice>
<expecting>I want to subtract</expecting>
<say>Sure</say>
<decision>
<prompt>What two numbers would you like me to subtract</prompt>
<choice>
<expecting>[[NumOne]] and [[NumTwo]]</expecting>
<say>Subtracting [[NumTwo]] from [[NumOne]] gives [[app.subtract(NumOne, NumTwo)]]</say>
</choice>
<choice>
<expecting>Cancel</expecting>
<say>Canceling Subtraction</say>
</choice>
</decision>
</choice>
<choice>
<expecting>I want to multiply</expecting>
<say>Sure</say>
<decision>
<prompt>What two numbers would you like me to multiply</prompt>
<choice>
<expecting>[[NumOne]] and [[NumTwo]]</expecting>
<say>Multiplying [[NumOne]] and [[NumTwo]] gives [[app.multiply(NumOne, NumTwo)]]</say>
</choice>
<choice>
<expecting>Cancel</expecting>
<say>Canceling Multiplication</say>
</choice>
</decision>
</choice>
<choice>
<expecting>I want to divide</expecting>
<say>Sure</say>
<decision>
<prompt>What two numbers would you like me to divide</prompt>
<choice>
<expecting>[[NumOne]] and [[NumTwo]]</expecting>
<say>Dividing [[NumOne]] by [[NumTwo]] gives [[app.divide(NumOne, NumTwo)]]</say>
</choice>
<choice>
<expecting>Cancel</expecting>
<say>Canceling Division</say>
</choice>
</decision>
</choice>
</app>`, {app});