1
1
// This function is ran on AWS Lambda. It is the endpoint
2
2
// where all Alexa commands are sent. Then, it sends requests
3
3
// to the node app from server folder to process them.
4
+
4
5
const Alexa = require ( 'ask-sdk-core' ) ;
5
6
const request = require ( 'request' ) ;
6
- const host = "http://18.233.168.151:80" ;
7
+
8
+ const host = "http://internal-alexaAppELB-464974694.us-east-1.elb.amazonaws.com" ;
7
9
8
10
const sendQuery = function ( query , handlerInput ) {
9
- return new Promise ( ( resolve , reject ) => {
11
+ return new Promise ( function ( resolve , reject ) {
10
12
const username = handlerInput . requestEnvelope . session . user . userId ;
11
13
request . post ( {
12
14
headers : {
@@ -36,10 +38,11 @@ const sendQuery = function(query, handlerInput) {
36
38
} else if ( handlerInput . requestEnvelope . request . intent . name == "report" ) {
37
39
const days = handlerInput . requestEnvelope . request . intent . slots . days . value ;
38
40
var temp = `Here is the breakdown of your activities for the last ${ days } days:\n` ;
41
+ console . log ( output ) ;
39
42
output . forEach ( ( prj ) => {
40
- temp += `You spent total of ${ prj . totalTime } minutes on ${ prj . _id } ` ;
43
+ temp += `You spent total of ${ prj . totalTime } minutes on ${ prj . _id } . Among those: ` ;
41
44
prj . brief . forEach ( ( session ) => {
42
- temp += `${ session . timeSpent } minutes spent on day ${ session . day } `
45
+ temp += `${ session . timeSpent } minutes were spent on day ${ session . day } , `
43
46
} ) ;
44
47
} ) ;
45
48
output = temp ;
@@ -51,9 +54,9 @@ const sendQuery = function(query, handlerInput) {
51
54
. withSimpleCard ( 'Got it!!!' , output )
52
55
. getResponse ( ) ) ;
53
56
}
54
- } )
55
- } ) ;
56
- }
57
+ } ) ;
58
+ } ) ;
59
+ } ;
57
60
58
61
59
62
@@ -64,7 +67,7 @@ const LaunchRequestHandler = {
64
67
} ,
65
68
handle ( handlerInput ) {
66
69
const speechText = 'Describe time tracker' ;
67
-
70
+
68
71
return handlerInput . responseBuilder
69
72
. speak ( speechText )
70
73
. reprompt ( speechText )
@@ -161,15 +164,34 @@ const startProjectIntentHandler = {
161
164
} ,
162
165
handle ( handlerInput ) {
163
166
console . log ( "Start project" ) ;
167
+ const token = handlerInput . requestEnvelope . context . System . apiAccessToken ;
168
+ const deviceId = handlerInput . requestEnvelope . context . System . device . deviceId ;
164
169
const project = handlerInput . requestEnvelope . request . intent . slots . projectName . value ;
170
+ return new Promise ( ( resolve , reject ) => {
171
+ request . get ( {
172
+ headers : {
173
+ "Authorization" : `Bearer ${ token } `
174
+ } ,
175
+ url : `https://api.amazonalexa.com/v2/devices/${ deviceId } /settings/System.timeZone`
176
+ } , function ( err , res ) {
177
+ if ( err ) {
178
+ resolve ( handlerInput . responseBuilder
179
+ . speak ( "Something went wrong!" )
180
+ . withSimpleCard ( 'Oops...' , "Something broken on our part" )
181
+ . getResponse ( ) ) ;
182
+ } else {
165
183
var query = `{
166
- time(username: "username"){
184
+ time(username: "username", timeZone: ${ res . body } ){
167
185
start(projectName: "${ project } ")
168
186
}
169
- }` ;
170
- return sendQuery ( query , handlerInput ) ;
171
- }
172
- } ;
187
+ }` ;
188
+ resolve ( sendQuery ( query , handlerInput ) ) ;
189
+ }
190
+ } ) ;
191
+ } )
192
+
193
+ }
194
+ } ;
173
195
174
196
175
197
const finishProjectIntentHandler = {
@@ -214,7 +236,7 @@ const lsIntentHandler = {
214
236
} ,
215
237
handle ( handlerInput ) {
216
238
console . log ( "Ls" ) ;
217
- var query = `{
239
+ var query = `{
218
240
time(username: "username"){
219
241
ls
220
242
}
@@ -230,7 +252,7 @@ const currentIntentHandler = {
230
252
} ,
231
253
handle ( handlerInput ) {
232
254
console . log ( "Current project" ) ;
233
- var query = `{
255
+ var query = `{
234
256
time(username: "username"){
235
257
current
236
258
}
@@ -247,18 +269,18 @@ const reportIntentHandler = {
247
269
} ,
248
270
handle ( handlerInput ) {
249
271
var query = `{
250
- time(username: ${ username } ){
251
- report(days:${ days } ){
252
- _id
253
- totalTime
254
- brief{
255
- timeSpent
256
- day
257
- month
272
+ time(username: "username"){
273
+ report(days: 4){
274
+ _id
275
+ totalTime
276
+ brief{
277
+ timeSpent
278
+ day
279
+ month
280
+ }
258
281
}
259
- }
260
- }
261
- }` ;
282
+ }
283
+ }` ;
262
284
return sendQuery ( query , handlerInput ) ;
263
285
}
264
286
} ;
0 commit comments