-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
393 lines (371 loc) · 15.1 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* The main file for the Food Buddy Alexa Skill. Hosted on AWS Lambda.
* @authors Christopher Jereza, Michelle Hamilton
*/
//****************************************** ATTRIBUTES **************************************************************
'use strict';
/* The Alexa SDK. */
var Alexa = require('alexa-sdk');
/* String literal used for formatting. */
const WHITESPACE = ' ';
/* The states of this skill, used for separating ambiguous Yes and No intents. */
const STATES = {
'HELP': '_HELPSTATE',
'MAIN': '_MAINSTATE',
};
/* The Application ID for this Alexa Skill. */
const APP_ID = 'amzn1.ask.skill.f7574c4a-fe13-47d7-b1a5-34a199aacf04';
/* Default Alexa speech output messages. */
const MESSAGES = {
'WELCOME': 'Welcome to Food Buddy! Add an ingredient or food item to your meal.',
'STOP': 'Goodbye!',
'HELP': 'You can add ingredients to your meal by saying, ADD, and the amount of your ingredient. ' +
'Say, “what if I add,” followed by your quantity and ingredient, to hear the nutrients and calories you ' +
'will be adding. Say, “Cancel,” or, “remove,” followed by an ingredient to remove it from your meal. Say “summary” ' +
'at any point to hear the total nutritional information. ' +
'Would you like to hear an example?',
'UNHANDLED': 'I\'m sorry, I didn\'t get that. Please specify a quantity, ' +
'a unit of measurement, and an ingredient. Or, say \'Summary.\'',
'START_UNHANDLED': 'Say, \'Open Food Buddy\', to track the nutritional value of your meal.',
'REPROMPT': 'You can add another item, or ask for your summary.',
'HELP_UNHANDLED': 'Sorry, I didn\'t get that. Would you like to hear an example? Please say yes or no.'
};
/* File containing functions for accessing the USDA Nutrient Data Laboratory API. */
const DATA = require('./data.js');
//****************************************** FUNCTIONS **************************************************************
/**
* Add a specified ingredient to the current meal.
* @param {number} quantity - numerical amount of ingredient added
* @param {String} unit - unit of measurement (e.g. ounce/ounces, pound/pounds, cup/cups, etc.
* @param {String} ingredient - food item to add (e.g. chicken, pasta, rice, tofu, etc.)
* @return emit()
*/
function add(quantity, unit, ingredient) {
if (this.attributes.meal === undefined) {
this.attributes.meal = {};
}
if (this.attributes.ingredients === undefined) {
this.attributes.ingredients = {};
}
if (this.attributes.addedItems === undefined) {
this.attributes.addedItems = [];
}
DATA.getMacros.call(this, quantity, unit, ingredient, 0);
}
/**
* Update the session attributes with new ingredient
* @param {number} quantity - numerical amount of ingredient added
* @param {String} unit - unit of measurement (e.g. ounce/ounces, pound/pounds, cup/cups, etc.
* @param {String} ingredient - food item to add (e.g. chicken, pasta, rice, tofu, etc.)
* @param {object} macros - dictionary object with Calories, Protein, Carbs, and Fat attributes.
*/
function update(quantity, unit, ingredient, macros) {
if (this.attributes.meal === undefined) {
this.attributes.meal = {};
}
if (this.attributes.ingredients === undefined) {
this.attributes.ingredients = {};
}
if (this.attributes.addedItems === undefined) {
this.attributes.addedItems = [];
}
this.attributes.meal[ingredient] = macros;
this.attributes.ingredients[ingredient] = [quantity, unit, macros.Calories];
this.attributes.addedItems.push(ingredient);
console.log(ingredient + macros.Calories);
this.emit(':ask', 'Adding ' + stringify.call(this, quantity, unit, ingredient, macros.Calories) + '.', MESSAGES.REPROMPT);
}
/**
* Return a string with the quantity, unit, and ingredient of the added item.
* @param {number} quantity - numerical amount of ingredient added
* @param {String} unit - unit of measurement (e.g. ounce/ounces, pound/pounds, cup/cups, etc.
* @param {String} ingredient - food item to add (e.g. chicken, pasta, rice, tofu, etc.)
* @param {number} calories - number of calories contained in the quantity of added ingredient.
* @return {String}
*/
function stringify(quantity, unit, ingredient, calories) {
return quantity + WHITESPACE + unit + ' of ' + ingredient + ' with ' + calories + ' calories';
}
/**
* List every ingredient added so far.
* @return emit()
*/
function report() {
const meal = this.attributes.meal;
if (meal === null || meal === undefined || meal.length <= 0) {
this.emit(':ask', 'No food items have been added.');
} else {
let totalCalories = 0;
let totalCarbs = 0;
let totalProtein = 0;
let totalFat = 0;
let macros;
for (let ingredient in meal) {
if (meal.hasOwnProperty(ingredient) && meal[ingredient] !== undefined) {
macros = meal[ingredient];
totalCalories += parseFloat(macros.Calories);
totalCarbs += parseFloat(macros.Carbs);
totalProtein += parseFloat(macros.Protein);
totalFat += parseFloat(macros.Fat);
}
}
let outputSpeech = 'You have a total of ' + totalCalories + ' Calories, ' + totalCarbs + ' grams of carbohydrates, ' +
totalProtein + ' grams of protein, and ' + totalFat + ' grams of fat.';
this.emit(':ask', outputSpeech, MESSAGES.REPROMPT);
}
}
/**
* Remove the ingredient that was most recently added.
* @return emit()
*/
function remove() {
let meal = this.attributes.meal;
let ingredients = this.attributes.ingredients;
let addedItems = this.attributes.addedItems;
if (meal === undefined || meal === null || meal.length === 0) {
this.emit(':ask', 'You have no ingredients left to remove.', MESSAGES.REPROMPT);
} else if (ingredients === undefined || ingredients === null || ingredients.length === 0) {
this.emit(':ask', 'You have no ingredients left to remove.', MESSAGES.REPROMPT);
} else if (addedItems === undefined || addedItems === null || addedItems.length === 0) {
this.emit(':ask', 'You have no ingredients left to remove.', MESSAGES.REPROMPT);
} else {
let itemToDelete = addedItems[addedItems.length - 1];
delete meal[itemToDelete];
delete ingredients[itemToDelete];
this.attributes.meal = meal;
this.attributes.ingredients = ingredients;
this.attributes.addedItems.pop();
let outputSpeech = 'Removing ' + itemToDelete;
this.emit(':ask', outputSpeech, MESSAGES.REPROMPT);
}
}
/**
* Report a list of all ingredients added so far, along with the Calorie count for each item.
* @return emit()
*/
function ingredients() {
let meal = this.attributes.ingredients;
if (meal === undefined || meal === null || meal.length === 0) {
this.emit(':ask', 'You have no ingredients left to remove.', MESSAGES.REPROMPT);
} else if (this.attributes.addedItems === undefined
|| this.attributes.addedItems === null
|| this.attributes.addedItems.length === 0) {
this.emit(':ask', 'You have no ingredients left to remove.', MESSAGES.REPROMPT);
} else {
let outputSpeech = 'You have added: ';
let ingredients = [];
for (let ingredient in meal) {
if (meal.hasOwnProperty(ingredient) && meal[ingredient] !== undefined) {
ingredients.push(ingredient);
}
}
const len = ingredients.length;
if (len > 0) {
let ingredient;
let quantity;
let unit;
let calories;
for (let i = 0; i < len - 1; i += 1) {
ingredient = ingredients[i];
quantity = meal[ingredient][0];
unit = meal[ingredient][1];
calories = meal[ingredient][2];
outputSpeech += stringify.call(this, quantity, unit, ingredient, calories);
if (i < len - 2 || len > 2) {
outputSpeech += ', ';
} else {
outputSpeech += WHITESPACE;
}
}
if (len > 1) {
outputSpeech += 'and ';
}
ingredient = ingredients[len - 1];
quantity = meal[ingredient][0];
unit = meal[ingredient][1];
calories = meal[ingredient][2];
outputSpeech += stringify.call(this, quantity, unit, ingredient, calories) + '.';
}
this.emit(':ask', outputSpeech, MESSAGES.REPROMPT);
}
}
/**
* Tell the user the Calories and macro-nutrient counts for INGREDIENT, without adding it to the meal.
* @param {number} quantity - numerical amount of ingredient added
* @param {String} unit - unit of measurement (e.g. ounce/ounces, pound/pounds, cup/cups, etc.
* @param {String} ingredient - food item to add (e.g. chicken, pasta, rice, tofu, etc.)
* @return call to DATA.getMacros()
*/
function whatIf(quantity, unit, ingredient) {
if (this.attributes.meal === undefined) {
this.attributes.meal = {};
}
if (this.attributes.ingredients === undefined) {
this.attributes.ingredients = {};
}
DATA.getMacros.call(this, quantity, unit, ingredient, 1);
}
/**
* Helper function for WhatIfIntent.
* @param {number} quantity - numerical amount of ingredient added
* @param {String} unit - unit of measurement (e.g. ounce/ounces, pound/pounds, cup/cups, etc.
* @param {String} ingredient - food item to add (e.g. chicken, pasta, rice, tofu, etc.)
* @param {Object} macros - dictionary object with Calories, Protein, Carbs, and Fat attributes.
* @return emit()
*/
function whatIfHelper(quantity, unit, ingredient, macros) {
this.emit(':ask', 'Adding ' + quantity + WHITESPACE + unit + ' of ' + ingredient + ' would add ' + macros.Calories +
' calories, ' + macros.Carbs + ' grams of carbohydrates, ' + macros.Protein + ' grams of protein, and ' +
macros.Fat + ' grams of fat to your meal.', MESSAGES.REPROMPT);
}
//****************************************** HANDLERS *************************************************************
/* The initial handler for a new session. */
var handlers = {
'LaunchRequest': function () {
this.handler.state = STATES.MAIN;
this.emitWithState('LaunchRequest');
},
'AddIntent': function () {
this.handler.state = STATES.MAIN;
this.emitWithState('AddIntent');
},
'WhatIfIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('WhatIfIntent');
},
'ReportIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('ReportIntent');
},
'RemoveIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('RemoveIntent');
},
'IngredientsIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('IngredientsIntent');
},
'Unhandled': function() {
this.emit(':tell', MESSAGES.START_UNHANDLED);
},
'AMAZON.StopIntent': function() {
this.emit(':tell', MESSAGES.STOP);
},
'AMAZON.CancelIntent': function() {
this.emit('AMAZON.StopIntent');
}
};
/* The Main State handler for the Skill during the conversation. */
const mainStateHandler = Alexa.CreateStateHandler(STATES.MAIN, {
'LaunchRequest': function () {
this.emit(':ask', MESSAGES.WELCOME);
},
'AddIntent': function () {
const quantity = this.event.request.intent.slots.quantity.value;
const unit = this.event.request.intent.slots.unit.value;
const ingredient = this.event.request.intent.slots.ingredient.value;
add.call(this, quantity, unit, ingredient);
},
'WhatIfIntent': function() {
const quantity = this.event.request.intent.slots.quantity.value;
const unit = this.event.request.intent.slots.unit.value;
const ingredient = this.event.request.intent.slots.ingredient.value;
whatIf.call(this, quantity, unit, ingredient);
},
'ReportIntent': function() {
report.call(this);
},
'RemoveIntent': function() {
remove.call(this);
},
'IngredientsIntent': function() {
ingredients.call(this)
},
'Unhandled': function() {
this.emit(':ask', MESSAGES.UNHANDLED);
},
'AMAZON.StopIntent': function() {
this.emit(':tell', MESSAGES.STOP);
},
'AMAZON.HelpIntent': function() {
this.handler.state = STATES.HELP;
this.emit(':ask', MESSAGES.HELP);
},
'AMAZON.NoIntent': function() {
this.emit('ReportIntent');
},
'AMAZON.YesIntent': function() {
this.emit(':ask', 'Please add another ingredient, or say \'Summary\'.');
},
'StartOverIntent': function() {
this.attributes.meal = {};
this.attributes.ingredients = {};
this.attributes.addedItems = [];
this.emit('LaunchRequest');
},
'AMAZON.CancelIntent': function() {
this.emit('RemoveIntent');
}
});
/* Handler for the Help State of the conversation. Used to separate Yes and No intents. */
const helpStateHandler = Alexa.CreateStateHandler(STATES.HELP, {
'AMAZON.YesIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState(':ask', 'For example, you could say: Add three ounces of chicken, or you could ask: How many calories ' +
'would five grams of cheese add?', MESSAGES.REPROMPT);
},
'AMAZON.NoIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState(':ask', MESSAGES.WELCOME);
},
'LaunchRequest': function () {
this.handler.state = STATES.MAIN;
this.emitWithState('LaunchRequest');
},
'AddIntent': function () {
this.handler.state = STATES.MAIN;
this.emitWithState('AddIntent');
},
'WhatIfIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('WhatIfIntent');
},
'ReportIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('ReportIntent');
},
'RemoveIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('RemoveIntent');
},
'IngredientsIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('IngredientsIntent');
},
'StartOverIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('StartOverIntent');
},
'AMAZON.StopIntent': function() {
this.emit(':tell', MESSAGES.STOP);
},
'AMAZON.HelpIntent': function() {
this.emit(':ask', MESSAGES.HELP);
},
'AMAZON.CancelIntent': function() {
this.handler.state = STATES.MAIN;
this.emitWithState('AMAZON.YesIntent');
},
'Unhandled': function() {
this.emit(':ask', MESSSAGES.HELP_UNHANDLED);
},
});
//****************************************** EXPORTS *************************************************************
exports.update = update;
exports.whatIfHelper = whatIfHelper;
exports.handler = function(event, context){
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers, mainStateHandler, helpStateHandler);
alexa.execute();
};