forked from uzillion/crypto-whale-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.js
127 lines (116 loc) · 4.04 KB
/
message.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
const request = require('request-promise-native');
const config = require('../config');
let prev_msg_id = 0;
let prev_to_id = "";
let prev_mo_id = "";
let prev_quantity = 0;
// let test = false;
let CHAT_ID = config.CHAT_ID;
const sendMessage = (chatOptions) => {
return request(chatOptions)
.catch(function (err) {
console.log("Main:", err.message);
});
}
const build = (messageObj) => {
let event = messageObj.event;
let symbol = messageObj.symbol;
let quantity = messageObj.quantity;
let price = messageObj.price;
let exchange = messageObj.exchange;
let to_id = undefined;
let mo_id = undefined;
let isAggregate = messageObj.isAggregate;
let encoded_message = "";
let aggr_msg = "";
let order_ids = "";
let special_msg = "";
let base = "";
let currency = "";
if(symbol) {
let unformatted = symbol.replace("-","");
base = unformatted.substr((symbol.substr(-4) == "USDT"?-4:-3));
// base = (/^USD(T)?$/.test(base)?"$":base);
currency = unformatted.replace(base, "");
}
if(exchange == 'gdax') {
to_id = messageObj.taker_order_id;
mo_id = messageObj.maker_order_id;
let taker = "";
let maker = "";
if(quantity < 0) {
taker = "seller";
maker = "buyer";
} else {
taker = "buyer";
maker = "seller";
}
order_ids = `\n${taker}-orderId: ${to_id.substring(to_id.length - 4)}\n${maker}-orderId: ${mo_id.substring(mo_id.length - 4)}`;
aggr_msg = isAggregate?"\n**Aggregated**":"";
special_msg = order_ids + aggr_msg;
}
if(event == "VOLUME") {
let type = messageObj.type;
let side = messageObj.side;
let size = Math.round(messageObj.size);
if(typeof type == "object") {
special_msg = `( order of ${type[1]} ${currency} placed at ${type[0]} ${base})\n`;
}
encoded_message = encodeURIComponent(`*VOLUME:*\n${symbol} (${exchange})\n${special_msg}Total ${side} volume = ${quantity} ${currency}, which is around ${size} times bigger than counterpart`);
}
else if(event == "TRADE") {
if(quantity < 0)
encoded_message = encodeURIComponent(`*TRADE:*\n${symbol} (${exchange})\nSold ${quantity*-1} at ${price} ${base}${special_msg}`);
else
encoded_message = encodeURIComponent(`*TRADE:*\n${symbol} (${exchange})\nBought ${quantity} at ${price} ${base}${special_msg}`);
}
else if(event == "limit-change") {
encoded_message = encodeURIComponent('*Limit change requested.*');
}
else if(event == "WD") {
let side = messageObj.side;
encoded_message = encodeURIComponent(`*VOLUME:*\n${symbol} (${exchange})\n${side} volume is down compared to before`);
}
if(config.TESTING)
CHAT_ID = config.TEST_CHAT_ID;
var chatOptions = {
uri: `https://api.telegram.org/bot${config.BOT_TOKEN}/sendMessage?parse_mode=Markdown&chat_id=${CHAT_ID}&text=${encoded_message}`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true
};
if(to_id == prev_to_id || mo_id == prev_mo_id) {
if(quantity > prev_quantity) {
chatOptions.uri = `https://api.telegram.org/bot${config.BOT_TOKEN}/editMessageText?parse_mode=Markdown&chat_id=${CHAT_ID}&message_id=${prev_msg_id}&text=${encoded_message}`;
sendMessage(chatOptions)
.then((res) => {
// console.log("This is res:", res);
if(res.ok) {
prev_msg_id = res.result.message_id;
prev_quantity = quantity;
} else {
console.log("Message update failed");
}
// console.log(res.result.text+" updated");
});
}
} else {
// console.log(encoded_message);
sendMessage(chatOptions)
.then((res) => {
if(res.ok) {
prev_msg_id = res.result.message_id;
prev_quantity = quantity;
} else {
console.log("Message sending failed");
}
// console.log(res.result.text+" sent");
});
}
if(to_id != undefined && mo_id != undefined) {
prev_to_id = to_id;
prev_mo_id = mo_id;
}
}
module.exports = build;