|
| 1 | +const superagent = require('superagent'); |
| 2 | +const _ = require('lodash'); |
| 3 | +const log = require('../core/log.js'); |
| 4 | +const util = require('../core/util.js'); |
| 5 | +const config = util.getConfig(); |
| 6 | +const iftttConfig = config.ifttt; |
| 7 | + |
| 8 | +const IFTTT = function(done) { |
| 9 | + _.bindAll(this); |
| 10 | + this.ifttt; |
| 11 | + this.price = 'N/A'; |
| 12 | + this.done = done; |
| 13 | + this.setup(); |
| 14 | +}; |
| 15 | + |
| 16 | +IFTTT.prototype.setup = function(done) { |
| 17 | + var setupIFTTT = function () { |
| 18 | + if(iftttConfig.sendMessageOnStart){ |
| 19 | + var exchange = config.watch.exchange; |
| 20 | + var currency = config.watch.currency; |
| 21 | + var asset = config.watch.asset; |
| 22 | + var body = "Gekko has started, Ive started watching " |
| 23 | + +exchange |
| 24 | + +" " |
| 25 | + +currency |
| 26 | + +" " |
| 27 | + +asset |
| 28 | + +" I'll let you know when I got some advice"; |
| 29 | + this.send(body); |
| 30 | + }else{ |
| 31 | + log.debug('Skipping Send message on startup') |
| 32 | + } |
| 33 | + }; |
| 34 | + setupIFTTT.call(this) |
| 35 | +}; |
| 36 | + |
| 37 | +IFTTT.prototype.processCandle = function(candle, done) { |
| 38 | + this.price = candle.close; |
| 39 | + |
| 40 | + done(); |
| 41 | +}; |
| 42 | + |
| 43 | +IFTTT.prototype.portfolioUpdate = function(portfolio) { |
| 44 | + var message = "Gekko has detected a portfolio update. " + |
| 45 | + "Your current " + config.watch.currency + " balance is " + portfolio.currency + '.' + |
| 46 | + "Your current " + config.watch.exchange + " balance is " + portfolio.assert + '.'; |
| 47 | + this.send(message); |
| 48 | +} |
| 49 | + |
| 50 | +IFTTT.prototype.processAdvice = function(advice) { |
| 51 | + if (advice.recommendation == 'soft' && iftttConfig.muteSoft) return; |
| 52 | + |
| 53 | + const text = [ |
| 54 | + 'Gekko is watching ', |
| 55 | + config.watch.exchange, |
| 56 | + ' and has detected a new trend, advice is to go ', |
| 57 | + advice.recommendation, |
| 58 | + '.\n\nThe current ', |
| 59 | + config.watch.asset, |
| 60 | + ' price is ', |
| 61 | + this.price |
| 62 | + ].join(''); |
| 63 | + |
| 64 | + this.send(text); |
| 65 | +}; |
| 66 | + |
| 67 | +IFTTT.prototype.send = function(content) { |
| 68 | + superagent. |
| 69 | + post('https://maker.ifttt.com/trigger/' + iftttConfig.eventName + '/with/key/' + iftttConfig.makerKey) |
| 70 | + .send({value1: content}) |
| 71 | + .end(function(err, res){ |
| 72 | + if(err || !res){ |
| 73 | + log.error('IFTTT ERROR:', error) |
| 74 | + }else{ |
| 75 | + log.info('IFTTT Message Sent') |
| 76 | + } |
| 77 | + }); |
| 78 | +}; |
| 79 | + |
| 80 | +IFTTT.prototype.checkResults = function(error) { |
| 81 | + if (error) { |
| 82 | + log.warn('error sending IFTTT', error); |
| 83 | + } else { |
| 84 | + log.info('Send advice via IFTTT.'); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +module.exports = IFTTT; |
0 commit comments