-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (49 loc) · 1.31 KB
/
app.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
const SlackBot = require("slackbots");
const request = require("request");
const config = require('./config');
// initialize bot
const bot = new SlackBot({
token: config.slack.token,
name: "Currency Bot"
});
bot.on("start", () => {
console.log("Bot is ready.");
});
// event raised when we type something on bot
// on each recieved message, filter it out
// the type 'message' is classified as user generated message
bot.on("message", (data) => {
if(data.type==='message') {
filterMessage(data)
}
})
async function filterMessage(message) {
// when user types report
if(message.text === 'report') {
let data = await getAllPair();
let formattedData = "";
Object.keys(data.quotes).forEach((singlePair) => {
formattedData += singlePair;
formattedData += ' : ' + data.quotes[singlePair] + '\n';
})
postMessage(formattedData)
} else {
// further processing can be done
// such as custom commands
return;
}
}
function getAllPair() {
return new Promise((resolve, reject) => {
let apiEndPoint = config.currencyLayer.apiEndPoint.replace('$ACCESS_KEY$',config.currencyLayer.apiKey);
request(apiEndPoint,(error,response,body) => {
if(error) {
return reject(error);
}
resolve(JSON.parse(body));
})
})
}
function postMessage(message) {
bot.postMessageToChannel(config.slack.channelName, message);
}