forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.js
91 lines (74 loc) · 2.5 KB
/
slack.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
const WebClient = require('@slack/client').WebClient;
const _ = require('lodash');
const log = require('../core/log.js');
const util = require('../core/util.js');
const config = util.getConfig();
const slackConfig = config.slack;
const Slack = function(done) {
_.bindAll(this);
this.slack;
this.price = 'N/A';
this.done = done;
this.setup();
};
Slack.prototype.setup = function(done) {
this.slack = new WebClient(slackConfig.token);
const setupSlack = function(error, result) {
if(slackConfig.sendMessageOnStart){
const body = this.createResponse("#439FE0","Gekko started!") ;
this.send(body);
}else{
log.debug('Skipping Send message on startup')
}
};
setupSlack.call(this)
};
Slack.prototype.processCandle = function(candle, done) {
this.price = candle.close;
done();
};
Slack.prototype.processAdvice = function(advice) {
if (advice.recommendation == 'soft' && slackConfig.muteSoft) return;
const color = advice.recommendation === "long" ? "good" : (advice.recommendation === "short" ? "danger" : "warning");
const body = this.createResponse(color, "There is a new trend! The advice is to go `" + advice.recommendation + "`! Current price is `" + this.price + "`");
this.send(body);
};
Slack.prototype.send = function(content, done) {
this.slack.chat.postMessage(slackConfig.channel, "", content, (error, response) => {
if (error || !response) {
log.error('Slack ERROR:', error);
} else {
log.info('Slack Message Sent');
}
});
};
Slack.prototype.checkResults = function(error) {
if (error) {
log.warn('error sending slack', error);
} else {
log.info('Send advice via slack.');
}
};
Slack.prototype.createResponse = function(color, message) {
const template = {
"username": this.createUserName(),
"icon_url": this.createIconUrl(),
"attachments": [
{
"fallback": "",
"color": color,
"text": message,
"mrkdwn_in": ["text"]
}
]
};
return template;
};
Slack.prototype.createUserName = function() {
return config.watch.exchange[0].toUpperCase() + config.watch.exchange.slice(1) + " - " + config.watch.currency + "/" + config.watch.asset;
};
Slack.prototype.createIconUrl = function() {
const asset = config.watch.asset === "XBT" ? "btc" :config.watch.asset.toLowerCase();
return "https://github.com/cjdowner/cryptocurrency-icons/raw/master/128/icon/" + asset + ".png";
};
module.exports = Slack;