-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (126 loc) · 3.76 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
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
'use strict';
const Alexa = require('alexa-sdk');
const https = require('https');
const baseUrl = process.env.BASE_URL;
const accountId = process.env.ACCOUNT_ID;
const accessToken = process.env.ACCESS_TOKEN;
const languageStrings = {
'en': {
translation: {
SKILL_NAME: 'Monzo',
HELP_MESSAGE: 'Check your Monzo balance',
HELP_REPROMPT: 'Ask for your balance',
STOP_MESSAGE: 'Goodbye!',
},
},
};
const handlers = {
'LaunchRequest': function () {
this.emit('GetBalance');
},
'GetBalanceIntent': function () {
this.emit('GetBalance');
},
'GetBalance': function () {
const alexa = this;
getBalance(accountId, function (err, balance) {
if (!err) {
console.log(`Balance is ${balance}`)
alexa.emit(':tell', `You have ${formatCurrency(balance)} in your Monzo account`);
}
});
},
'GetLastTransactionIntent': function () {
this.emit('GetLastTransaction');
},
'GetLastTransaction': function () {
const alexa = this;
getLastTransaction(accountId, function (err, transaction) {
if (!err) {
console.log(`Transaction: ${transaction}`)
alexa.emit(':tell', `Your last transaction was ${formatTransaction(transaction)}`);
}
});
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
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'));
},
};
const formatCurrency = function (pennies) {
if (pennies === 0) {
return "Nothing";
}
const pounds = Math.floor(Math.abs(pennies) / 100);
const littlePennies = Math.abs(pennies) % 100;
if (littlePennies === 0) {
return `${pounds} pounds`;
}
return `${pounds} pounds ${littlePennies}`;
}
const getBalance = function (accountId, callback) {
console.log('Getting the balance');
https.get({
host: baseUrl,
path: `/balance?account_id=${accountId}`,
headers: {
'Authorization': accessToken
}
}, (res) => {
if (res.statusCode !== 200) {
callback("Something went wrong", null);
}
let body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
const balance = JSON.parse(body).balance;
callback(null, balance);
});
});
};
const formatTransaction = function (transaction) {
const fromTo = transaction.local_amount < 0 ? 'to' : 'from';
return `${formatCurrency(transaction.local_amount)} ${fromTo} ${transaction.description}`
}
const getLastTransaction = function (accountId, callback) {
console.log('Getting the balance');
https.get({
host: baseUrl,
path: `/transactions?account_id=${accountId}`,
headers: {
'Authorization': accessToken
}
}, (res) => {
if (res.statusCode !== 200) {
callback("Something went wrong", null);
}
let body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
const transactions = JSON.parse(body).transactions;
const last = transactions[transactions.length - 1];
callback(null, last);
});
});
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = process.env.APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};