forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmppbot.js
193 lines (158 loc) · 5.33 KB
/
xmppbot.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
var log = require('../core/log');
var moment = require('moment');
var _ = require('lodash');
var xmpp = require('node-xmpp-client');
var config = require('../core/util').getConfig();
var xmppbot = config.xmppbot;
var utc = moment.utc;
var Actor = function() {
_.bindAll(this);
this.bot = new xmpp.Client({ jid: xmppbot.client_id,
password: xmppbot.client_pwd,
host: xmppbot.client_host,
port: xmppbot.client_port
});
this.advice = 'Dont got one yet :(';
this.adviceTime = utc();
this.state = xmppbot.status_msg;
this.price = 'Dont know yet :(';
this.priceTime = utc();
this.commands = {
';;advice': 'emitAdvice',
';;price': 'emitPrice',
';;donate': 'emitDonation',
';;real advice': 'emitRealAdvice',
';;help': 'emitHelp'
};
this.rawCommands = _.keys(this.commands);
this.bot.addListener('online', this.setState);
this.bot.addListener('stanza', this.rawStanza);
this.bot.addListener("error", this.logError);
this.bot.connection.socket.setTimeout(0)
this.bot.connection.socket.setKeepAlive(true, 10000)
}
Actor.prototype.setState = function() {
var elem = new xmpp.Element('presence', { }).c('show').t('chat').up().c('status').t(this.state)
this.bot.send(elem);
};
Actor.prototype.rawStanza = function(stanza) {
if (stanza.is('presence') && (stanza.attrs.type == 'subscribe')) {
this.bot.send(new xmpp.Element('presence', { to: stanza.attrs.from, type: 'subscribed' }));
}
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {
// Swap addresses...
var from = stanza.attrs.from;
var body = stanza.getChild('body');
if (!body) {
return;
}
var message_recv = body.getText(); //Get Incoming Message
this.verifyQuestion(from, message_recv);
}
};
Actor.prototype.sendMessageTo = function(receiver, message){
this.bot.send(new xmpp.Element('message', { to: receiver, type: 'chat' }).
c('body').t(message)
);
};
Actor.prototype.sendMessage = function(message) {
this.sendMessageTo(this.from, message);
};
Actor.prototype.processCandle = function(candle) {
this.price = candle.close;
this.priceTime = candle.start;
};
Actor.prototype.processAdvice = function(advice) {
if (xmppbot.muteSoft && advice.recommendation === 'soft') return;
this.advice = advice.recommendation;
this.adviceTime = utc();
if(xmppbot.emitUpdates)
this.newAdvice(xmppbot.receiver);
};
Actor.prototype.verifyQuestion = function(receiver, text) {
if(text in this.commands)
this[this.commands[text]](receiver);
}
Actor.prototype.newAdvice = function(receiver) {
this.sendMessageTo(receiver, 'Important news!');
this.emitAdvice(receiver);
}
// sent advice
Actor.prototype.emitAdvice = function(receiver) {
var message = [
'Advice for ',
config.watch.exchange,
' ',
config.watch.currency,
'/',
config.watch.asset,
' using ',
config.tradingAdvisor.method,
' at ',
config.tradingAdvisor.candleSize,
' minute candles, is:\n',
this.advice,
' ',
config.watch.asset,
' (from ',
this.adviceTime.fromNow(),
')'
].join('');
this.sendMessageTo(receiver, message);
};
// sent price
Actor.prototype.emitPrice = function(receiver) {
var 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.sendMessageTo(receiver, message);
};
// sent donation info
Actor.prototype.emitDonation = function(receiver) {
var message = 'You want to donate? How nice of you! You can send your coins here:';
message += '\nBTC:\t19UGvmFPfFyFhPMHu61HTMGJqXRdVcHAj3';
this.sendMessageTo(receiver, message);
};
Actor.prototype.emitHelp = function(receiver) {
var message = _.reduce(
this.rawCommands,
function(message, command) {
return message + ' ' + command + ',';
},
'possible commands are:'
);
message = message.substr(0, _.size(message) - 1) + '.';
this.sendMessageTo(receiver, message);
}
Actor.prototype.emitRealAdvice = function(receiver) {
// http://www.examiner.com/article/uncaged-a-look-at-the-top-10-quotes-of-gordon-gekko
// http://elitedaily.com/money/memorable-gordon-gekko-quotes/
var realAdvice = [
'I don\'t throw darts at a board. I bet on sure things. Read Sun-tzu, The Art of War. Every battle is won before it is ever fought.',
'Ever wonder why fund managers can\'t beat the S&P 500? \'Cause they\'re sheep, and sheep get slaughtered.',
'If you\'re not inside, you\'re outside!',
'The most valuable commodity I know of is information.',
'It\'s not a question of enough, pal. It\'s a zero sum game, somebody wins, somebody loses. Money itself isn\'t lost or made, it\'s simply transferred from one perception to another.',
'What\'s worth doing is worth doing for money. (Wait, wasn\'t I a free and open source bot?)',
'When I get a hold of the son of a bitch who leaked this, I\'m gonna tear his eyeballs out and I\'m gonna suck his fucking skull.'
];
this.sendMessageTo(receiver, _.first(_.shuffle(realAdvice)));
}
Actor.prototype.logError = function(message) {
log.error('XMPP ERROR:', message);
};
module.exports = Actor;