-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (156 loc) · 5.17 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
'use strict';
// Create specific endpoint
const ENDPOINT_RATES = 'http://www.apilayer.net/api/live?access_key=6e36ba1b01cf1b5c6ce5867f5a02dc8b&source=USD¤cies=MXN&format=1';
const Alexa = require('ask-sdk');
const rp = require('request-promise');
let skill;
let lastTimestamp = null;
let lastRate = null;
const cardName = 'MXN Exchange Rate';
const intentName = 'ExchangeIntent';
let requestOptions = {
method: 'GET',
uri: ENDPOINT_RATES,
headers: {
'content-type': 'application/json'
},
json: true,
resolveWithFullResponse: false
};
async function getData() {
return rp(requestOptions)
.then(function(response) {
return response;
})
.catch(function(error) {
console.log(`Error while retrieving exchange rate: ${error}`);
return err;
});
}
async function getCurrentExchangeRate() {
// if data is fresh dont request
let fresh = isFresh();
console.log(`Is data fresh ${fresh}`);
if (fresh) {
return `The current MXN to USD exchange rate is: ${lastRate}`;
}
let data = await getData();
if (data && data.success === true) {
let rate = data.quotes.USDMXN;
rate = Number.parseFloat(rate).toFixed(2);
lastRate = rate;
lastTimestamp = data.timestamp;
let speechText = `The current MXN to USD exchange rate is: ${rate}`;
return speechText;
} else {
lastRate = null;
lastTimestamp = null;
return 'Sorry, I\'m unable to get the current exchange rate. Try again later!';
}
}
function isFresh() {
if (lastTimestamp !== null) {
var last = new Date(lastTimestamp * 1000);
var now = new Date();
if (
last.getDate() === now.getDate() &&
last.getMonth() === now.getMonth() &&
(now.getHours() - last.getHours) <= 1 &&
(now.getMinutes() - last.getMinutes()) < 10
) {
return true;
}
}
return false;
}
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Welcome to MXN Exchange Rate, you can ask: what is today\'s peso exchange rate?';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard(cardName, speechText)
.getResponse();
}
};
const ExchangeIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === intentName;
},
async handle(handlerInput) {
let speechText = await getCurrentExchangeRate();
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard(cardName, speechText)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'You can ask me: what is today\'s peso exchange rate?';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard(cardName, speechText)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speechText = 'Adios!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard(cardName, speechText)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. You can ask me: what is today\'s peso exchange rate?')
.getResponse();
},
};
exports.handler = async function (event, context) {
console.log(`REQUEST++++${JSON.stringify(event)}`);
if (!skill) {
skill = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
ExchangeIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(ErrorHandler)
.create();
}
return skill.invoke(event,context);
}