-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
35 lines (26 loc) · 1003 Bytes
/
routes.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
const router = require('express').Router();
const convertCurrency = require('./api');
const formatResponse = (text) => ({
fulfillmentText: text,
source: 'moneybot-webhook'
})
router.post('/', async (req, res) => {
const { queryResult } = req.body;
const { action, parameters } = queryResult;
console.log(req.body);
let fulfillmentText = '';
if (action === 'convert') {
const { outputCurrency, amountToConvert } = parameters;
const { amount, currency: inputCurrency } = amountToConvert;
if (outputCurrency === inputCurrency) {
fulfillmentText = `Well, ${amount} ${outputCurrency} is obviously equal to ${amount} ${outputCurrency}!`;
res.status(200).json(formatResponse(fulfillmentText));
} else {
const data = await convertCurrency(inputCurrency, outputCurrency, amount);
if (data) {
res.status(200).json(formatResponse(`${amount} ${inputCurrency} gets you ${data} ${outputCurrency}!`));
}
}
}
})
module.exports = router;