-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
312 lines (297 loc) · 9.99 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
// Alexa Lambda Kegbot Integration
// Copyright 2016-2020 Pat Freeman <[email protected]>
// https://github.com/patfreeman/alexa-lambda-kegbot
"use strict";
const Alexa = require("ask-sdk-core");
const config = require("./config/default");
const kegbot = require("./lib/kegbot");
const helper = require("./lib/helper");
const i18n = require("i18next");
const sprintf = require("i18next-sprintf-postprocessor");
// Pull in time package
var javascript_time_ago = require("javascript-time-ago");
javascript_time_ago.locale(require("javascript-time-ago/locales/en"));
const time_ago_english = new javascript_time_ago("en-US");
const languageStrings = {
"en-US": {
translation: {
SKILL_NAME: "Kegbot",
HELP_MESSAGE:
"You can ask questions such as, what's on tap, or, how much is left...Now, what can I help you with?",
},
},
};
const OnTapHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return (
request.type === "IntentRequest" && request.intent.name === "OnTapIntent"
);
},
async handle(handlerInput) {
var TapNumber = undefined;
if (handlerInput.requestEnvelope.request.intent.slots) {
if (handlerInput.requestEnvelope.request.intent.slots.TapNumber) {
TapNumber =
handlerInput.requestEnvelope.request.intent.slots.TapNumber.value;
}
}
const kegs = await kegbot.getCurrentKegs();
console.log(kegs);
var speechOutput = config.applicationName;
if (TapNumber && (TapNumber > kegs.length || TapNumber < 1)) {
speechOutput += " only has " + kegs.length + " tap";
if (kegs.length > 1) {
speechOutput += "s";
}
} else if (TapNumber && TapNumber <= kegs.length) {
var keg = kegs[TapNumber - 1];
if (keg == undefined) {
speechOutput += " has nothing on tap number " + TapNumber;
} else {
speechOutput += " has " + keg.type.name + " on tap number " + TapNumber;
}
} else if (kegs.length) {
kegs.forEach(function (keg, index) {
if (keg == undefined) {
speechOutput += " has nothing on tap number " + (index + 1);
return;
}
if (kegs.length > 1 && index === kegs.length - 1) {
speechOutput += " and ";
}
speechOutput += " has " + keg.type.name + " on tap";
if (kegs.length > 1) {
speechOutput += " number " + (index + 1);
}
});
} else {
speechOutput += " has nothing on tap.";
}
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(speechOutput)
.getResponse();
},
};
const RecentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return (
request.type === "IntentRequest" && request.intent.name === "RecentIntent"
);
},
async handle(handlerInput) {
try {
await callDirectiveService(handlerInput, "Wow! That's a lot of drinks.");
} catch (err) {
console.log("Directive failed: " + err);
}
const drinks = await kegbot.getCurrentDrinks();
console.log(drinks);
var speechOutput = config.applicationName;
if (drinks) {
var keg = {};
drinks.forEach(function (drink, index) {
if (drink == undefined) {
return;
}
if (!keg[drink.keg.id]) {
keg[drink.keg.id] = {};
keg[drink.keg.id].user_ids = [];
keg[drink.keg.id].volume = 0;
}
keg[drink.keg.id].name = drink.keg.beverage.name;
keg[drink.keg.id].user_ids[drink.user_id] += 1;
keg[drink.keg.id].volume += drink.volume_ml;
keg[drink.keg.id].start = drink.session.start_time;
});
for (var key in keg) {
speechOutput += " poured " + helper.volume(keg[key].volume);
speechOutput += " of " + keg[key].name + " for user";
var plural = 0;
if (Object.keys(keg[key].user_ids).length > 1) {
speechOutput += "s";
plural = Object.keys(keg[key].user_ids).length - 1;
}
Object.keys(keg[key].user_ids).forEach(function (user, index) {
if (plural >= 1 && plural == index) {
speechOutput += " and";
}
speechOutput += " " + user;
});
// Convert the date format to epoch then realtive
var parts = keg[key].start.match(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/
);
var date = Date.UTC(
+parts[1],
parts[2] - 1,
+parts[3],
+parts[4],
+parts[5]
);
speechOutput += " " + time_ago_english.format(date) + ". ";
}
}
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(speechOutput)
.getResponse();
},
};
const VolumeHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return (
request.type === "IntentRequest" && request.intent.name === "VolumeIntent"
);
},
async handle(handlerInput) {
var TapNumber = undefined;
var VolumeUnits = undefined;
if (handlerInput.requestEnvelope.request.intent.slots) {
if (handlerInput.requestEnvelope.request.intent.slots.TapNumber) {
TapNumber =
handlerInput.requestEnvelope.request.intent.slots.TapNumber.value;
}
if (handlerInput.requestEnvelope.request.intent.slots.VolumeUnits) {
VolumeUnits =
handlerInput.requestEnvelope.request.intent.slots.VolumeUnits.value;
}
}
const kegs = await kegbot.getCurrentKegs();
console.log(kegs);
if (VolumeUnits == undefined && config.units == "imperial") {
VolumeUnits = "pints";
}
var speechOutput = config.applicationName;
if (TapNumber && (TapNumber > kegs.length || TapNumber < 1)) {
speechOutput += " only has " + kegs.length + " taps";
} else if (TapNumber && TapNumber <= kegs.length) {
var keg = kegs[TapNumber - 1];
if (keg == undefined) {
speechOutput += " has nothing on tap number " + TapNumber;
} else {
if (VolumeUnits === "pints") {
speechOutput +=
" has " +
Math.round(keg.volume_ml_remain / 473.176) +
" pints";
} else if (VolumeUnits === "percent" || VolumeUnits === "percentage") {
speechOutput +=
" has " + keg.percent_full.toPrecision(2) + " percent";
} else {
speechOutput +=
" has " + Math.round(keg.volume_ml_remain / 1000) + " liters";
}
speechOutput += " of " + keg.type.name + " on tap number " + TapNumber;
}
} else if (kegs.length) {
kegs.forEach(function (keg, index) {
if (keg == undefined) {
speechOutput += " has nothing remaining on tap number " + (index + 1);
return;
}
if (kegs.length > 1 && index === kegs.length - 1) {
speechOutput += " and ";
}
if (VolumeUnits === "pints") {
speechOutput +=
" has " +
Math.round(keg.volume_ml_remain / 473.176) +
" pints";
} else if (VolumeUnits === "percent" || VolumeUnits === "percentage") {
speechOutput +=
" has " + keg.percent_full.toPrecision(2) + " percent";
} else {
speechOutput +=
" has " + Math.round(keg.volume_ml_remain / 1000) + " liters";
}
speechOutput += " of " + keg.type.name + " on tap";
if (kegs.length > 1) {
speechOutput += " number " + (index + 1);
} else {
speechOutput += ".";
}
});
} else {
speechOutput += " has nothing on tap.";
}
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(speechOutput)
.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
console.log(`Error stack: ${error.stack}`);
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(requestAttributes.t("ERROR_MESSAGE"))
.reprompt(requestAttributes.t("ERROR_MESSAGE"))
.getResponse();
},
};
const LocalizationInterceptor = {
process(handlerInput) {
const localizationClient = i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
resources: languageStrings,
});
localizationClient.localize = function localize() {
const args = arguments;
const values = [];
for (let i = 1; i < args.length; i += 1) {
values.push(args[i]);
}
const value = i18n.t(args[0], {
returnObjects: true,
postProcess: "sprintf",
sprintf: values,
});
if (Array.isArray(value)) {
return value[Math.floor(Math.random() * value.length)];
}
return value;
};
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = function translate(...args) {
return localizationClient.localize(...args);
};
},
};
function callDirectiveService(handlerInput, message) {
const requestEnvelope = handlerInput.requestEnvelope;
const directiveServiceClient = handlerInput.serviceClientFactory.getDirectiveServiceClient();
const requestId = requestEnvelope.request.requestId;
const endpoint = requestEnvelope.context.System.apiEndpoint;
const token = requestEnvelope.context.System.apiAccessToken;
const directive = {
header: {
requestId,
},
directive: {
type: "VoicePlayer.Speak",
speech: message,
},
};
return directiveServiceClient.enqueue(directive, endpoint, token);
}
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(OnTapHandler)
.addRequestHandlers(RecentHandler)
.addRequestHandlers(VolumeHandler)
.addRequestInterceptors(LocalizationInterceptor)
.addErrorHandlers(ErrorHandler)
.withApiClient(new Alexa.DefaultApiClient())
.lambda();