-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalexaskill.js
54 lines (52 loc) · 1.62 KB
/
alexaskill.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
var https = require('https');
var APP_ID = 'amzn1.echo-sdk-ams.app.31f81f3c-4b1b-44c7-8126-cd4e468b72c2';
exports.handler = function (event, context, callback) {
if (event.session.application.applicationId !== APP_ID) {
callback(new Error('Invalid Application ID'), null);
return;
}
console.log(event.request);
if (!event.session.user.accessToken) {
callback(null, {
version: '1.0',
response: {
outputSpeech: {type: 'PlainText', text: 'Please go to your Alexa app and link your account.'},
card: {type: 'LinkAccount'}
}
});
return;
}
var json = JSON.stringify(event);
var options = {
method: 'POST',
hostname: 'api.rogertalk.com',
path: '/ask/v1/request',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Content-Length': Buffer.byteLength(json)
}
};
var req = https.request(options, function (res) {
var body = '', calledBack = false;
res.on('error', function (e) {
if (calledBack) return;
calledBack = true;
callback(e, null);
});
res.on('data', function (chunk) {
if (calledBack) return;
body += chunk;
});
res.on('end', function () {
if (calledBack) return;
calledBack = true;
callback(null, JSON.parse(body));
body = null;
});
});
req.on('error', function (e) {
context.fail('Error: ' + e.message);
});
req.write(json);
req.end();
};