-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.js
executable file
·79 lines (72 loc) · 2.54 KB
/
actions.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
/* eslint-disable no-console */
const qs = require('querystring');
const fetch = require('node-fetch');
const log = (event) => {
console.log('Event', JSON.stringify(event, null, 2));
return Promise.resolve(event);
};
const getCommand = text => /^<@[A-Z0-9]*>(.+)/.exec(text)[1].trim();
const parseConvertCommand = (command) => {
const pattern = /[a-z\s]*(\d+).*([a-z]{3}).*([a-z]{3})/i;
const matches = command.match(pattern);
if (matches) {
return {
amount: +matches[1],
source: matches[2],
target: matches[3],
};
}
return null;
};
// Make an API call to http://fixer.io/
const callFixer = (command) => {
const url = `https://api.fixer.io/latest?base=${command.source}&symbols=${command.target}`;
console.log(`Requesting ${url}`);
return fetch(url)
.then(response => response.json())
.then((json) => {
if (json.error === 'Invalid base') {
return `No rates found for currency "${command.source}"`;
}
if (!json.rates[command.target]) {
return `No rates found for currency "${command.target}"`;
}
const result = json.rates[command.target] * command.amount;
const displayResult = parseFloat(Math.round(result * 100) / 100).toFixed(2);
return `${command.amount}${command.source} is ${displayResult}${command.target}`;
});
};
// Generate a response to the command.
const doCommand = (event) => {
const rawCommand = event.slack.event.text;
const command = getCommand(rawCommand);
const convertCommand = parseConvertCommand(command);
if (convertCommand) {
return callFixer(convertCommand)
.then(reply => Object.assign(event, { reply }));
}
const defaultReply = `I'm sorry, I don't understand the command "${command}"
Please use a format like "convert 1AUD to USD"`;
return Object.assign(event, { reply: defaultReply });
};
// Send a response via Slack.
const sendResponse = (event) => {
const params = {
token: event.team.bot.bot_access_token,
channel: event.slack.event.channel,
text: event.reply,
};
const url = `https://slack.com/api/chat.postMessage?${qs.stringify(params)}`;
console.log(`Requesting ${url}`);
return fetch(url)
.then(response => response.json())
.then((response) => {
if (!response.ok) throw new Error('SlackAPIError');
return Object.assign(event, { response });
});
};
module.exports.handler = (event, context, callback) => log(event)
.then(doCommand) // Attempt the command
.then(sendResponse) // Update the channel
.then(() => callback(null)) // Sucess
.catch(callback); // Error