-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
354 lines (322 loc) · 10.3 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
/**
* Alexa Skill Lambda Function for accessing Particle devices.
**/
'use strict';
const Alexa = require('alexa-sdk');
const bst = require('bespoken-tools');
const utils = require('./utils/skillUtils').utils;
const particleApiUtils = require('./utils/particleApiUtils').utils;
const emitResponse = (alexaSdk, response) => {
alexaSdk.response.speak(response);
alexaSdk.emit(':responseReady');
};
const emitAccountLinkResponse = alexaSdk => {
alexaSdk.emit(
':tellWithLinkAccountCard',
'To start using this skill, please use the Alexa app to authenticate with Particle.'
);
};
const emitSetActiveDeviceResponse = alexaSdk => {
alexaSdk.response.speak(
`You need to set an active device to use this command. Say Alexa, set active device with the name of the device you want to work with.`
);
alexaSdk.emit('responseReady');
};
const handlers = {
LaunchRequest: function() {
if (this.event.session.user.accessToken === undefined) {
return emitAccountLinkResponse(this);
}
this.response
.speak(
'Welcome to the Particle cloud. You can ask me about online devices, list functions and variables, set variables and call cloud functions.'
)
.listen(
'Give me a command or ask "what can Particle do" to get started.'
);
this.emit(':responseReady');
},
NumberOfDevicesIntent: function() {
let response;
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
}
particleApiUtils
.getOnlineDevices(token)
.then(devices => {
const onlineDevicesCount = devices.length;
response = `You currently have ${
onlineDevicesCount > 1
? `${onlineDevicesCount} devices`
: `${onlineDevicesCount} device`
} online.`;
})
.catch(err => {
response = `This request has failed. Please try again. ${err}`;
})
.finally(() => {
emitResponse(this, response);
});
},
ListDevicesIntent: function() {
let response;
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
}
particleApiUtils
.getOnlineDevices(token)
.then(devices => {
const onlineDevicesCount = devices.length;
if (onlineDevicesCount === 1) {
const device = devices[0];
response = `You have one device online, named ${utils.normalizeDeviceName(
device.name
)}.`;
} else {
response = `You have ${onlineDevicesCount} devices online. Their names are ${utils.sayArray(
devices.map(device => utils.normalizeDeviceName(device.name))
)}.`;
}
})
.catch(err => {
response = `This request has failed. Please try again. ${err}`;
})
.finally(() => {
emitResponse(this, response);
});
},
SetActiveDeviceIntent: function() {
const deviceName = this.event.request.intent.slots.deviceName.value;
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
}
if (deviceName) {
particleApiUtils
.getDeviceByName(token, utils.normalizeDeviceName(deviceName))
.then(device => {
this.attributes['currentDevice'] = device.body.name;
this.attributes['currentDeviceId'] = device.body.id;
emitResponse(
this,
`Ok, I've set your current device to ${deviceName}`
);
})
.catch(err => {
if (err === 'DEVICE_NOT_FOUND') {
emitResponse(
this,
`Sorry, I couldn't find a device with the name ${deviceName} in your account.`
);
} else {
emitResponse(
this,
`Sorry, I couldn't get what you wanted. Please try again`
);
}
});
} else {
emitResponse(
this,
`Sorry, I didn't understand which device you wanted me to set as active. Please try again.`
);
}
},
GetActiveDeviceIntent: function() {
if (!this.attributes['currentDevice']) {
emitResponse(this, `You don't currently have an active device set.`);
} else {
emitResponse(
this,
`Your current active device is ${utils.normalizeDeviceName(
this.attributes['currentDevice']
)}`
);
}
},
ListFunctionsIntent: function() {
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
}
const deviceName = utils.normalizeDeviceName(
this.event.request.intent.slots.deviceName.value ||
this.attributes['currentDevice']
);
if (!deviceName) {
return emitResponse(
this,
`Please provide a device name or set an active device to use this command.`
);
}
particleApiUtils
.getDeviceFunctions(token, deviceName)
.then(functions => {
if (functions.length === 0) {
emitResponse(
this,
`There are no functions available on the device named ${deviceName}`
);
} else {
emitResponse(
this,
`Here are the functions for the device named ${deviceName}: ${utils.sayArray(
functions
)}`
);
}
})
.catch(err => {
if (err === 'DEVICE_NOT_FOUND') {
emitResponse(this, `Sorry, I couldn't find a device by that name`);
} else {
emitResponse(
this,
`Sorry, I couldn't get what you wanted. Please try again`
);
}
});
},
CallFunctionIntent: function() {
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
} else if (this.attributes['currentDevice'] === undefined) {
return emitSetActiveDeviceResponse(this);
}
const slots = this.event.request.intent.slots;
const deviceName = utils.normalizeDeviceName(
this.attributes['currentDevice']
);
const functionName = utils.normalizeFunctionName(slots.functionName.value);
particleApiUtils
.callDeviceFunction(token, deviceName, functionName)
.then(functions => {
emitResponse(
this,
`Ok, I called the function named ${functionName} on ${deviceName}`
);
})
.catch(err => {
if (err === 'FUNCTION_NOT_FOUND') {
emitResponse(this, `Sorry, I couldn't find a function by that name`);
} else if (err === 'DEVICE_NOT_FOUND') {
emitResponse(this, `Sorry, I couldn't find a device by that name`);
} else {
emitResponse(
this,
`Sorry, I couldn't get what you wanted. Please try again`
);
}
});
},
ListVariablesIntent: function() {
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
}
const deviceName = utils.normalizeDeviceName(
this.event.request.intent.slots.deviceName.value ||
this.attributes['currentDevice']
);
if (!deviceName) {
return emitResponse(
this,
`Please provide a device name or set an active device to use this command.`
);
}
particleApiUtils
.getDeviceVariables(token, deviceName)
.then(variables => {
if (utils.objectIsEmpty(variables)) {
emitResponse(
this,
`There are no variables available on the device named ${deviceName}`
);
} else {
emitResponse(
this,
`Here are the variables for the device named ${deviceName}: ${utils.sayObject(
variables
)}`
);
}
})
.catch(err => {
if (err === 'DEVICE_NOT_FOUND') {
emitResponse(this, `Sorry, I couldn't find a device by that name`);
} else {
emitResponse(
this,
`Sorry, I couldn't get what you wanted. Please try again`
);
}
});
},
GetVariableIntent: function() {
const token = this.event.session.user.accessToken;
if (token === undefined) {
return emitAccountLinkResponse(this);
} else if (this.attributes['currentDevice'] === undefined) {
return emitSetActiveDeviceResponse(this);
}
const slots = this.event.request.intent.slots;
const deviceName = utils.normalizeDeviceName(
this.attributes['currentDevice']
);
const variable = utils.normalizeFunctionName(slots.variable.value);
particleApiUtils
.getVariable(token, deviceName, variable)
.then(value => {
emitResponse(
this,
`The value of the variable named ${variable} on device ${deviceName} is ${
value.body.result
}`
);
})
.catch(err => {
if (err.statusCode === 404) {
emitResponse(
this,
`Sorry, I couldn't find a variable by that name. Please try again.`
);
} else {
emitResponse(
this,
`Sorry, I couldn't get what you wanted. Please try again`
);
}
});
},
'AMAZON.HelpIntent': function() {
const speechOutput = `You can ask me to set an active device, get names of online devices, get variables and call functions.`;
const reprompt = `What would you like to do?`;
this.emit(':ask', speechOutput + ' ' + reprompt, reprompt);
},
'AMAZON.CancelIntent': function() {
emitResponse(this, `Goodbye`);
},
'AMAZON.StopIntent': function() {
emitResponse(this, `Goodbye`);
} /*,
Unhandled: function() {
var speechOutput = 'Sorry, something went wrong.';
var reprompt = 'Would you like to try again?';
this.response.speak(speechOutput + ' ' + reprompt).listen(reprompt);
this.emit(':responseReady');
}*/
};
exports.handler = bst.Logless.capture(
'ccc51960-fc8e-4cf3-b117-67bf2b90f105',
function(event, context) {
const APP_ID = 'amzn1.ask.skill.925e2d0a-2bd9-434a-83ae-c6bb8fd16085';
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.dynamoDBTableName = 'particleDeviceState';
alexa.registerHandlers(handlers);
alexa.execute();
}
);