-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (101 loc) · 3.72 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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"] */
const Alexa = require('alexa-sdk');
const https = require('https');
let alexa;
const APP_ID = 'amzn1.ask.skill.70498491-196e-40ad-8763-3f77d7a35318';
const languageStrings = {
'en': {
translation: {
SKILL_NAME: 'Share Price',
GET_SHARE_PRICE_MESSAGE_1: 'The current share price for <say-as interpret-as="spell-out">',
GET_SHARE_PRICE_MESSAGE_2: '</say-as> is: ',
GET_INVALID_SHARE_PRICE_MESSAGE: 'Sorry, I couldn\'t get the share price for ',
HELP_MESSAGE: 'You can say give me the share price for <say-as interpret-as="spell-out">SBRY</say-as>, or, you can say exit... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
UNHANDLED_MESSAGE: 'Sorry, I didn\'t understand your request.',
STOP_MESSAGE: 'Goodbye!',
},
},
};
function httpGet(query, callback) {
console.log(`Query: ${query}`);
const options = {
host: 'www.alphavantage.co',
path: `/query?function=TIME_SERIES_INTRADAY&symbol=LON:${query}&interval=1min&apikey=3E1HUGJQ89TUJZIH`,
method: 'GET',
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
callback(body);
});
});
req.end();
req.on('error', (error) => {
console.error(`ERROR: ${error}`);
});
}
const handlers = {
'LaunchRequest': function () {
this.emit('AMAZON.HelpIntent');
},
'GetSharePriceIntent': function () {
const stockSymbol = this.event.request.intent.slots.Symbol.value;
let sharePrice = '';
let speechOutput = '';
const sharePriceMessage1 = this.t('GET_SHARE_PRICE_MESSAGE_1');
const sharePriceMessage2 = this.t('GET_SHARE_PRICE_MESSAGE_2');
const invalidSharePriceMessage = this.t('GET_INVALID_SHARE_PRICE_MESSAGE');
const skillName = this.t('SKILL_NAME');
if (stockSymbol !== encodeURIComponent(stockSymbol)) {
speechOutput = invalidSharePriceMessage + stockSymbol;
console.log(`Speech Output: ${speechOutput}`);
alexa.emit(':tellWithCard', speechOutput, `${stockSymbol} ${skillName}`, sharePrice);
} else {
httpGet(stockSymbol, (response) => {
try {
const responseData = JSON.parse(response);
const lastRefreshed = responseData['Meta Data']['3. Last Refreshed'];
console.log(`Last Refreshed: ${lastRefreshed}`);
sharePrice = responseData['Time Series (1min)'][lastRefreshed]['1. open'];
console.log(`Share Price: ${sharePrice}`);
} catch (err) {
console.error('Error parsing json response, likely request did not succeed');
}
// Create speech output
if (sharePrice === '') {
speechOutput = invalidSharePriceMessage + stockSymbol;
} else {
speechOutput = sharePriceMessage1 + stockSymbol + sharePriceMessage2 + sharePrice;
}
console.log(`Speech Output: ${speechOutput}`);
alexa.emit(':tellWithCard', speechOutput, `${stockSymbol} ${skillName}`, sharePrice);
});
}
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_REPROMPT');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'Unhandled': function () {
this.emit(':tell', this.t('UNHANDLED_MESSAGE'));
},
};
exports.handler = function (event, context) {
alexa = Alexa.handler(event, context);
alexa.resources = languageStrings;
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};