forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegrambot.js
197 lines (173 loc) · 5.34 KB
/
telegrambot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const log = require('../core/log');
const moment = require('moment');
const _ = require('lodash');
const config = require('../core/util').getConfig();
const telegrambot = config.telegrambot;
const emitTrades = telegrambot.emitTrades;
const utc = moment.utc;
const telegram = require("node-telegram-bot-api");
const Actor = function() {
_.bindAll(this);
this.advice = null;
this.adviceTime = utc();
this.price = 'Dont know yet :(';
this.priceTime = utc();
this.commands = {
'/start': 'emitStart',
'/advice': 'emitAdvice',
'/subscribe': 'emitSubscribe',
'/unsubscribe': 'emitUnSubscribe',
'/price': 'emitPrice',
'/help': 'emitHelp'
};
if (telegrambot.donate) {
this.commands['/donate'] = 'emitDonate';
}
this.rawCommands = _.keys(this.commands);
this.chatId = null;
this.subscribers = [];
this.bot = new telegram(telegrambot.token, { polling: true });
this.bot.onText(/(.+)/, this.verifyQuestion);
};
Actor.prototype.processCandle = function(candle, done) {
this.price = candle.close;
this.priceTime = candle.start;
done();
};
Actor.prototype.processAdvice = function(advice) {
if (advice.recommendation === 'soft') return;
this.advice = advice.recommendation;
this.adviceTime = utc();
this.advicePrice = this.price;
this.subscribers.forEach(this.emitAdvice, this);
};
if(emitTrades) {
Actor.prototype.processTradeInitiated = function (tradeInitiated) {
var message = 'Trade initiated. ID: ' + tradeInitiated.id +
'\nAction: ' + tradeInitiated.action + '\nPortfolio: ' +
tradeInitiated.portfolio + '\nBalance: ' + tradeInitiated.balance;
this.bot.sendMessage(this.chatId, message);
}
Actor.prototype.processTradeCancelled = function (tradeCancelled) {
var message = 'Trade cancelled. ID: ' + tradeCancelled.id;
this.bot.sendMessage(this.chatId, message);
}
Actor.prototype.processTradeAborted = function (tradeAborted) {
var message = 'Trade aborted. ID: ' + tradeAborted.id +
'\nNot creating order! Reason: ' + tradeAborted.reason;
this.bot.sendMessage(this.chatId, message);
}
Actor.prototype.processTradeErrored = function (tradeErrored) {
var message = 'Trade errored. ID: ' + tradeErrored.id +
'\nReason: ' + tradeErrored.reason;
this.bot.sendMessage(this.chatId, message);
}
Actor.prototype.processTradeCompleted = function (tradeCompleted) {
var message = 'Trade completed. ID: ' + tradeCompleted.id +
'\nAction: ' + tradeCompleted.action +
'\nPrice: ' + tradeCompleted.price +
'\nAmount: ' + tradeCompleted.amount +
'\nCost: ' + tradeCompleted.cost +
'\nPortfolio: ' + tradeCompleted.portfolio +
'\nBalance: ' + tradeCompleted.balance +
'\nFee percent: ' + tradeCompleted.feePercent +
'\nEffective price: ' + tradeCompleted.effectivePrice;
this.bot.sendMessage(this.chatId, message);
}
}
Actor.prototype.verifyQuestion = function(msg, text) {
this.chatId = msg.chat.id;
if (text[1].toLowerCase() in this.commands) {
this[this.commands[text[1].toLowerCase()]]();
} else {
this.emitHelp();
}
};
Actor.prototype.emitStart = function() {
this.bot.sendMessage(this.chatId, 'Hello! How can I help you?');
};
Actor.prototype.emitSubscribe = function() {
if (this.subscribers.indexOf(this.chatId) === -1) {
this.subscribers.push(this.chatId);
this.bot.sendMessage(this.chatId, `Success! Got ${this.subscribers.length} subscribers.`);
} else {
this.bot.sendMessage(this.chatId, "You are already subscribed.");
}
};
Actor.prototype.emitUnSubscribe = function() {
if (this.subscribers.indexOf(this.chatId) > -1) {
this.subscribers.splice(this.subscribers.indexOf(this.chatId), 1);
this.bot.sendMessage(this.chatId, "Success!");
} else {
this.bot.sendMessage(this.chatId, "You are not subscribed.");
}
};
Actor.prototype.emitAdvice = function(chatId) {
let message = [
'Advice for ',
config.watch.exchange,
' ',
config.watch.currency,
'/',
config.watch.asset,
' using ',
config.tradingAdvisor.method,
' at ',
config.tradingAdvisor.candleSize,
' minute candles, is:\n',
].join('');
if (this.advice) {
message += this.advice +
' ' +
config.watch.asset +
' ' +
this.advicePrice +
' (' +
this.adviceTime.fromNow() +
')';
} else {
message += 'None'
}
if (chatId) {
this.bot.sendMessage(chatId, message);
} else {
this.bot.sendMessage(this.chatId, message);
}
};
// sent price over to the last chat
Actor.prototype.emitPrice = function() {
const message = [
'Current price at ',
config.watch.exchange,
' ',
config.watch.currency,
'/',
config.watch.asset,
' is ',
this.price,
' ',
config.watch.currency,
' (from ',
this.priceTime.fromNow(),
')'
].join('');
this.bot.sendMessage(this.chatId, message);
};
Actor.prototype.emitDonate = function() {
this.bot.sendMessage(this.chatId, telegrambot.donate);
};
Actor.prototype.emitHelp = function() {
let message = _.reduce(
this.rawCommands,
function(message, command) {
return message + ' ' + command + ',';
},
'Possible commands are:'
);
message = message.substr(0, _.size(message) - 1) + '.';
this.bot.sendMessage(this.chatId, message);
};
Actor.prototype.logError = function(message) {
log.error('Telegram ERROR:', message);
};
module.exports = Actor;