-
Notifications
You must be signed in to change notification settings - Fork 6
/
telegrambot.js
103 lines (87 loc) · 2.74 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
var log = require('../core/log');
var moment = require('moment');
var _ = require('lodash');
var util = require('../core/util');
var config = util.getConfig();
var utc = moment.utc;
var telegramfancy = require("tgfancy");
var Actor = function () {
_.bindAll(this);
this.chatId = null;
this.bot = new telegramfancy(config.telegrambot.token, { polling: true });
this.bot.onText(/(.+)/, this.verifyQuestion);
log.addRemoteLogger(this);
}
util.makeEventEmitter(Actor);
Actor.prototype.processAdvice = function (advice) {
log.info('advice =', advice.recommendation); // advice is an array carrying long/short and candle array info
if (this.chatId) {
var message;
if (advice.recommendation == 'short') {
message = "SELL order received, sending to exchange";
}
if (advice.recommendation == 'long') {
message = "BUY order received, sending to exchange";
}
this.bot.sendMessage(this.chatId, message);
}
}
Actor.prototype.processTrade = function (trade) {
//{
// action: [either "buy" or "sell"],
// price: [number, price that was sold at],
// date: [moment object, exchange time trade completed at],
// portfolio: [object containing amount in currency and asset],
// balance: [number, total worth of portfolio]
//}
if (this.chatId) {
this.bot.sendMessage(this.chatId, "Trade completed!");
this.bot.sendMessage(this.chatId, trade.date.toDate() + ": " + trade.action + " at " + trade.price.toFixed(2));
// emit portfolio command to get results of trade
this.emit('command', {
command: 'portfolio',
arguments: [null],
handled: false,
response: null
});
}
}
Actor.prototype.verifyQuestion = function (msg, text) {
this.chatId = msg.chat.id;
// simple parsing that supports a command and single argument
var tokens = text[1].split(" ");
if (tokens.length == 1 || tokens.length == 2) {
var command = tokens[0].toLowerCase();
var arg = tokens.length == 2 ? tokens[1].toLowerCase() : null;
this.emitCommand(command, arg);
}
else {
this.bot.sendMessage(this.chatId, "'" + text[1] + "' - syntax error");
}
}
Actor.prototype.emitCommand = function(command, arg) {
var cmd = {
command: command.replace('/',''),
arguments: [arg],
handled: false,
response: null
};
this.emit('command', cmd);
if (cmd.handled) {
if (cmd.response) {
this.bot.sendMessage(this.chatId, cmd.response);
}
}
else {
this.bot.sendMessage(this.chatId, "'" + cmd.command + "' - unrecognised command");
}
}
Actor.prototype.logError = function (message) {
log.error('Telegram ERROR:', message);
};
Actor.prototype.logRemote = function (message) {
if (this.chatId) {
this.bot.sendMessage(this.chatId, message);
}
}
module.exports = Actor;