forked from gits-lit/caredaycare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (181 loc) · 5.95 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
// configure environment variables in .env
require('dotenv').config()
// import alexa software development kit
const Alexa = require('alexa-sdk')
// import aws software development kit for our database
const awsSDK = require('aws-sdk')
awsSDK.config.update({
region: 'us-east-1',
endpoint: 'https://dynamodb.us-east-1.amazonaws.com',
accessKeyId: process.env.KEY,
secretAccessKey: process.env.SECRET_KEY
})
// start an instance of our DynamoDB
const docClient = new awsSDK.DynamoDB.DocumentClient()
const table = 'PillsDB'
const pill_brand = 'generic'
const params = {
TableName: table,
Key: {
'pill_brand': pill_brand
}
}
exports.handler = function (event, context, callback) {
// create alexa object
const alexa = Alexa.handler(event, context, callback)
// our application id
alexa.appId = process.env.APP_ID
// register intents for our skill
alexa.registerHandlers(handlers)
// after configuration, run alexa
alexa.execute()
}
const handlers = {
'HelloWorldIntent': function () {
// emit response directly
this.emit(':tell', 'Hello World!')
},
'ResetPillCountIntent': function () {
const self = this
const count = 10;
let params = {
TableName: table,
Item: {
"pill_count": count,
"pill_brand": pill_brand
}
};
console.log("Adding new item...");
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(params, null, 2));
}
});
params = {
TableName: table,
Item: {
"pill_count": count,
"pill_brand": 'greenpill'
}
};
console.log("Adding new item...");
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(params, null, 2));
self.emit(':tell', 'Pill count reset to ' + count);
}
});
},
'PillCountIntent': function () {
const self = this
docClient.get(params, function (err, data) {
if (err) {
console.error('Unable to read item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('GetItem succeeded:', JSON.stringify(data, null, 2))
// emit response directly
self.emit(':tell', 'You currently have ' + data.Item.pill_count + ' red pills')
}
})
},
'GreenPillCountIntent': function () {
const self = this
const pill_brand = 'greenpill'
const params = {
TableName: table,
Key: {
'pill_brand': pill_brand
}
}
docClient.get(params, function (err, data) {
if (err) {
console.error('Unable to read item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('GetItem succeeded:', JSON.stringify(data, null, 2))
// emit response directly
self.emit(':tell', 'You currently have ' + data.Item.pill_count + ' green pills')
}
})
},
'PillDispenseIntent': function () {
const self = this
docClient.get(params, function (err, data) {
if (err) {
console.error('Unable to read item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('GetItem succeeded:', JSON.stringify(data, null, 2))
if (data.Item.pill_count === 0) {
self.emit(':tell', 'Out of pills! Please restock');
return;
}
// add update query
params.UpdateExpression = 'set pill_count = :new_count'
params.ExpressionAttributeValues = {
':new_count': data.Item.pill_count - 1
}
params.ReturnValues = 'UPDATED_NEW'
console.log('Updating the item...')
// update the database
docClient.update(params, function (err, data) {
if (err) {
console.error('Unable to update item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('UpdateItem succeeded:', JSON.stringify(data, null, 2))
// emit response directly
if (data.Attributes.pill_count === 1) {
self.emit(':tell', 'Pill dispensed. You now have ' + data.Attributes.pill_count + ' red pill');
return;
}
self.emit(':tell', 'Pill dispensed. You now have ' + data.Attributes.pill_count + ' red pills')
}
})
}
})
},
'GreenPillDispenseIntent': function () {
const self = this
const pill_brand = 'greenpill'
const params = {
TableName: table,
Key: {
'pill_brand': pill_brand
}
}
docClient.get(params, function (err, data) {
if (err) {
console.error('Unable to read item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('GetItem succeeded:', JSON.stringify(data, null, 2))
if (data.Item.pill_count === 0) {
self.emit(':tell', 'Out of pills! Please restock');
return;
}
// add update query
params.UpdateExpression = 'set pill_count = :new_count'
params.ExpressionAttributeValues = {
':new_count': data.Item.pill_count - 1
}
params.ReturnValues = 'UPDATED_NEW'
console.log('Updating the item...')
// update the database
docClient.update(params, function (err, data) {
if (err) {
console.error('Unable to update item. Error JSON:', JSON.stringify(err, null, 2))
} else {
console.log('UpdateItem succeeded:', JSON.stringify(data, null, 2))
// emit response directly
if (data.Attributes.pill_count === 1) {
self.emit(':tell', 'Pill dispensed. You now have ' + data.Attributes.pill_count + ' green pill');
return;
}
self.emit(':tell', 'Pill dispensed. You now have ' + data.Attributes.pill_count + ' green pills')
}
})
}
})
}
}