-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (103 loc) · 2.98 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
const Alexa = require('alexa-sdk');
const APP_ID = undefined;
// App model data:
// Exercise steps
const exerciseStore = require('exerciseStore');
// Collections of exercises that make up a routine
const routinesStore = require('routineStore');
// methods
const conversationHandler = require('conversationHandler');
// such as LIGHT, MEDIUM
let exerciseType = undefined;
// current routine step index
let exerciseStep = 0;
// Returns the current exercise State e.g. 'HEAL_LIFT_INIT'
function getExerciseState() {
return routinesStore[exerciseType][exerciseStep];
}
function exerciseHandler() {
let currExerciseState = getExerciseState();
let responseData = exerciseStore[currExerciseState];
if (responseData.type === 'text'){
/*
returns:
{
responseType: (string),
text: (string)
}
*/
return responseData.config;
}
if (responseData.type === 'syncMethod'){
return exerciseMethods[responseData.config.method](responseData.config.params);
}
}
const handlers = {
'LaunchRequest': function () {
resetExerciseStep();
let initialResponse = conversationHandler('INIT');
this.emit(initialResponse.responseType, initialResponse.text);
},
'lightIntent': function () {
exerciseType = 'LIGHT';
let responseData = exerciseHandler();
this.emit(responseData.responseType, responseData.text);
},
'mediumIntent': function () {
exerciseType = 'MEDIUM';
this.emit(':ask', 'I am not able to train medium yet, please say light if you wish to do this exercise instead.');
},
'readyIntent': function () {
incrementExerciseStep();
let responseData = exerciseHandler();
this.emit(responseData.responseType, responseData.text);
},
'repeatIntent': function () {
// let incrementExerciseBy = this.event.request.intent.slots.number.value;
let responseData = exerciseHandler();
this.emit(responseData.responseType, responseData.text);
},
'repeatRoutineIntent': function () {
resetExerciseStep();
let responseData = exerciseHandler();
this.emit(responseData.responseType, responseData.text);
}
};
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
// TODO export.
const exerciseMethods = {
repeatedMiddleStep: function (params) {
let { repetitions,
initialInstruction,
instruction,
finalInstruction,
congratulate
} = params;
let output = initialInstruction;
for (var i = 1; i < repetitions; i++) {
if (i < repetitions - 1) {
output += instruction;
} else {
output += finalInstruction;
output += congratulate;
}
}
return {
responseType: params.responseType,
text: output
}
},
}
function resetExerciseStep () {
exerciseStep = 0;
}
function incrementExerciseStep () {
exerciseStep ++;
}
export default index;