-
Notifications
You must be signed in to change notification settings - Fork 995
/
Copy pathindex.js
74 lines (48 loc) · 2.18 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
// alexa-cookbook sample code
// There are three sections, Text Strings, Skill Code, and Helper Function(s).
// You can copy and paste the entire file contents as the code for a new Lambda function,
// or copy & paste section #3, the helper function, to the bottom of your existing Lambda code.
// 1. Text strings =====================================================================================================
// Modify these strings and messages to change the behavior of your Lambda function
var myBucket = 'alexabucket12'; // replace with your own bucket name!
var myObject = 'hello.txt'; // replace with your own file name!
// 2. Skill Code =======================================================================================================
var Alexa = require('alexa-sdk');
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
// alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
// alexa.dynamoDBTableName = 'YourTableName'; // creates new table for session.attributes
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest': function () {
this.emit('MyIntent');
},
'MyIntent': function () {
var myParams = {
Bucket: myBucket,
Key: myObject
};
S3read(myParams, myResult => {
console.log("sent : " + JSON.stringify(myParams));
console.log("received : " + myResult);
this.emit(':tell', 'The S 3 file says, ' + myResult );
}
);
}
};
// END of Intent Handlers {} ========================================================================================
// 3. Helper Function =================================================================================================
function S3read(params, callback) {
// call AWS S3
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
s3.getObject(params, function(err, data) {
if(err) { console.log(err, err.stack); }
else {
var fileText = data.Body.toString(); // this is the complete file contents
callback(fileText);
}
});
}