add total amount
This commit is contained in:
parent
f9c718a8d1
commit
22ea8a52da
128
index.js
128
index.js
@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const Request = require('request-promise');
|
const Request = require('request-promise');
|
||||||
const dotenv = require('dotenv');
|
const dotenv = require('dotenv');
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const token = process.env['TELEGRAM_TOKEN'];
|
const token = process.env['TELEGRAM_TOKEN'];
|
||||||
const chat_id = process.env['TELEGRAM_CHAT_ID'];
|
const chat_id = process.env['TELEGRAM_CHAT_ID'];
|
||||||
const api_key = process.env['BINANCE_API_KEY'];
|
const api_key = process.env['BINANCE_API_KEY'];
|
||||||
@ -10,28 +10,24 @@ const secret_key = process.env['BINANCE_SECRET_KEY'];
|
|||||||
const NODE_ENV = process.env.NODE_ENV || "development";
|
const NODE_ENV = process.env.NODE_ENV || "development";
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
const timeZone = process.env.TIME_ZONE_STRING || 'Asia/Kolkata';
|
const timeZone = process.env.TIME_ZONE_STRING || 'Asia/Kolkata';
|
||||||
|
if(NODE_ENV === "development") {
|
||||||
if (process.env.NODE_ENV === "development") {
|
|
||||||
console.log("NODE_ENV development")
|
console.log("NODE_ENV development")
|
||||||
} else {
|
} else {
|
||||||
console.log("NODE_ENV production")
|
console.log("NODE_ENV production")
|
||||||
}
|
}
|
||||||
//Indian time string
|
//Indian time string
|
||||||
const event = new Date().toLocaleString('en-IN', { timeZone: timeZone, timeZoneName: 'short' });
|
const event = new Date().toLocaleString('en-IN', {
|
||||||
|
timeZone: timeZone,
|
||||||
//sending telegram message
|
timeZoneName: 'short'
|
||||||
const Slim_bot = require('slimbot');
|
});
|
||||||
const bot = new Slim_bot(token);
|
|
||||||
|
|
||||||
//only for heroku port error
|
//only for heroku port error
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
app.all('/', (req, res) => res.send('Bot is Running'));
|
app.all('/', (req, res) => res.send('Bot is Running'));
|
||||||
app.listen(port, () => console.log(`${event} - Server started on ${port} port`));
|
app.listen(port, () => console.log(`${event} - Server started on ${port} port`));
|
||||||
|
|
||||||
//BinanceWS
|
//BinanceWS
|
||||||
const binanceApi = require('binance');
|
const binanceApi = require('binance');
|
||||||
const binanceWS = new binanceApi.BinanceWS(true);
|
const binanceWS = new binanceApi.BinanceWS(false);
|
||||||
try {
|
try {
|
||||||
binanceRest = new binanceApi.BinanceRest({
|
binanceRest = new binanceApi.BinanceRest({
|
||||||
key: api_key,
|
key: api_key,
|
||||||
@ -41,12 +37,11 @@ try {
|
|||||||
binanceWS.onUserData(binanceRest, data => {
|
binanceWS.onUserData(binanceRest, data => {
|
||||||
console.log(`${event} - Session Message: `, data);
|
console.log(`${event} - Session Message: `, data);
|
||||||
process_data(data);
|
process_data(data);
|
||||||
|
}, 60000).then(() => {
|
||||||
}, 60000).then(ws => {
|
|
||||||
console.log(`${event} - Monitoring Spot User Order Data for binance.com`);
|
console.log(`${event} - Monitoring Spot User Order Data for binance.com`);
|
||||||
sendMessage(`<b>Binance Spot Order Monitor Started</b>\nthis message shows that you or heroku(if your are using) restart the bot.`)
|
sendMessage(`<b>Binance Spot Order Monitor Started</b>\nthis message shows that you or heroku(if your are using) restart the bot.`)
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch(err) {
|
||||||
console.error(`${event} - ${err}`)
|
console.error(`${event} - ${err}`)
|
||||||
sendMessage(err.toString())
|
sendMessage(err.toString())
|
||||||
}
|
}
|
||||||
@ -54,39 +49,68 @@ try {
|
|||||||
function fixFloat(floatNum, Precision = 8) {
|
function fixFloat(floatNum, Precision = 8) {
|
||||||
const num = Number.parseFloat(floatNum).toFixed(Precision);
|
const num = Number.parseFloat(floatNum).toFixed(Precision);
|
||||||
const str = num.toString();
|
const str = num.toString();
|
||||||
return str.replace(/(\.\d+?)0+\b/g, "$1") //fix 20.000 to 20.0 or 0.0000000120 to 0.000000012
|
return str.replace(/(\.\d+?)0+\b/g, "$1") //fix 20.000 to 20.0 or 0.0000000120 to 0.000000012
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function process_data(data) {
|
function process_data(data) {
|
||||||
|
let {
|
||||||
|
e: eventType,
|
||||||
|
x: executionType,
|
||||||
|
s: symbol,
|
||||||
|
p: price,
|
||||||
|
q: quantity,
|
||||||
|
S: side,
|
||||||
|
o: orderType,
|
||||||
|
i: orderId,
|
||||||
|
X: orderStatus,
|
||||||
|
l: lastTradeQuantity,
|
||||||
|
z: Cumulative_filled_quantity
|
||||||
|
} = data;
|
||||||
|
let str4 = symbol.slice(symbol.length - 4);
|
||||||
|
let str3 = symbol.slice(symbol.length - 3);
|
||||||
|
let sy;
|
||||||
|
if(["USDT", "BUSD", "TUSD", "USDC", "BIDR", "IDRT", "BVND"].includes(str4)) {
|
||||||
|
sy = str4
|
||||||
|
} else if(["BNB", "BTC", "XRP", "TRX", "ETH", "AUD", "BRL", "EUR", "GBP", "RUB", "TRY", "PAX", "DAI", "UAH", "NGN", "VAI"].includes(str3)) {
|
||||||
|
sy = str3
|
||||||
|
}
|
||||||
|
let total = `${fixFloat(Number(price) * Number(quantity))} ${sy}`
|
||||||
|
if(orderType !== "LIMIT") {
|
||||||
|
let {
|
||||||
|
L: Lprice
|
||||||
|
} = data;
|
||||||
|
price = Lprice
|
||||||
|
}
|
||||||
let txt;
|
let txt;
|
||||||
if (data.eventType === 'executionReport') {
|
if(eventType === 'executionReport') {
|
||||||
if (data.executionType === 'NEW') {
|
if(executionType === 'NEW') {
|
||||||
if (data.orderStatus === 'NEW') {
|
if(orderStatus === 'NEW') {
|
||||||
txt = `💸 💸 💸\n<b>Spot Order CREATED\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Quantity:</b> ${fixFloat(data.quantity)}\n<b>Order ID:</b> #ID${data.orderId}`
|
txt = `💸 💸 💸\n<b>Spot Order CREATED\nSide:</b> ${side}\n<b>Symbol:</b> #${symbol}\n<b>Price:</b> ${price}\n<b>Quantity:</b> ${fixFloat(quantity)}\n<b>Total:</b> ${total}\n<b>Order ID:</b> #ID${orderId}`
|
||||||
|
} else if(orderStatus === 'REJECTED') {
|
||||||
|
txt = `🚫 🚫 🚫\n<b>Spot Order REJECTED\nSide:</b> ${side}\n<b>Symbol:</b> #${symbol}\n<b>Price:</b> ${price}\n<b>Quantity:</b> ${fixFloat(quantity)}\n<b>Total:</b> ${total}\n<b>Order ID:</b> #ID${orderId}`
|
||||||
}
|
}
|
||||||
else if (data.orderStatus === 'REJECTED') {
|
} else if(executionType === 'CANCELED') {
|
||||||
txt = `🚫 🚫 🚫\n<b>Spot Order REJECTED\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Quantity:</b> ${fixFloat(data.quantity)}\n<b>Order ID:</b> #ID${data.orderId}`
|
if(orderStatus === 'CANCELED') {
|
||||||
|
txt = `🛑 🛑 🛑\n<b>Spot Order CANCELED\nSide:</b> ${side}\n<b>Symbol:</b> #${symbol}\n<b>Price:</b> ${price}\n<b>Quantity:</b> ${fixFloat(quantity)}\n<b>Total:</b> ${total}\n<b>Order ID:</b> #ID${orderId}`
|
||||||
}
|
}
|
||||||
} else if (data.executionType === 'CANCELED') {
|
} else if(executionType === 'TRADE') {
|
||||||
if (data.orderStatus === 'CANCELED') {
|
if(orderStatus === 'PARTIALLY_FILLED') {
|
||||||
txt = `🛑 🛑 🛑\n<b>Spot Order CANCELED\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Quantity:</b> ${fixFloat(data.quantity)}\n<b>Order ID:</b> #ID${data.orderId}`
|
txt = `💰 💰 💰 <b>Spot Order PARTIALLY FILLED Side:</b> ${side} <b>Symbol:</b> #${symbol} <b>Price:</b> ${price}
|
||||||
|
<b>Last Filled:</b> ${fixFloat(lastTradeQuantity)}
|
||||||
|
<b>Remaining:</b> ${fixFloat(Number(quantity) - Number(Cumulative_filled_quantity))}
|
||||||
|
<b>Total:</b> ${total} <b>Order ID:</b> #ID${orderId}`
|
||||||
|
} else if(orderStatus === 'FILLED') {
|
||||||
|
txt = `✅ ✅ ✅\n<b>Spot Order FULLY FILLED\nSide:</b> ${side}\n<b>Symbol:</b> #${symbol}\n<b>Price:</b> ${price}\n<b>Filled:</b> ${fixFloat(Cumulative_filled_quantity)}\n<b>Total:</b> ${total}\n<b>Order ID:</b> #ID${orderId}`
|
||||||
}
|
}
|
||||||
} else if (data.executionType === 'TRADE') {
|
} else if(['REPLACED', 'EXPIRED', 'PENDING_CANCEL'].includes(orderStatus)) {
|
||||||
if (data.orderStatus === 'PARTIALLY_FILLED') {
|
txt = `🔴 🟡 🔵\n<b>Spot Order ${orderStatus}\nSide:</b> ${side}\n<b>Symbol:</b> #${symbol}\n<b>Price:</b> ${price}\n<b>Quantity:</b> ${fixFloat(quantity)}\n<b>Total:</b> ${total}\n<b>Order ID:</b> #ID${orderId}`
|
||||||
txt = `💰 💰 💰\n<b>Spot Order PARTIALLY FILLED\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Last Filled:</b> ${fixFloat(data.lastTradeQuantity)}\n<b>Remaining:</b> ${fixFloat(Number(data.quantity) - Number(data.accumulatedQuantity))}\n<b>Order ID:</b> #ID${data.orderId}`
|
|
||||||
} else if (data.orderStatus === 'FILLED') {
|
|
||||||
txt = `✅ ✅ ✅\n<b>Spot Order FULLY FILLED\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Filled:</b> ${fixFloat(data.accumulatedQuantity)}\n<b>Order ID:</b> #ID${data.orderId}`
|
|
||||||
}
|
|
||||||
} else if (['REPLACED', 'EXPIRED', 'PENDING_CANCEL'].includes(data.orderStatus)) {
|
|
||||||
txt = `🔴 🟡 🔵\n<b>Spot Order ${data.orderStatus}\nSide:</b> ${data.side}\n<b>Symbol:</b> #${data.symbol}\n<b>Price:</b> ${data.price}\n<b>Quantity:</b> ${fixFloat(data.quantity)}\n<b>Order ID:</b> #ID${data.orderId}`
|
|
||||||
} else {
|
} else {
|
||||||
txt = `⚠️ ⚠️ ⚠️\n<b>Undefined</b>\nExecution Type: ${data.executionType}\nOrder Status ${data.orderStatus}\nFull Details:\n${msg}`
|
txt = `⚠️ ⚠️ ⚠️\n<b>Undefined</b>\nExecution Type: ${executionType}\nOrder Status ${orderStatus}\nFull Details:\n${data}`
|
||||||
}
|
}
|
||||||
sendMessage(txt)
|
sendMessage(txt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//sending telegram message
|
||||||
function sendMessage(text) {
|
function sendMessage(text) {
|
||||||
let params = {
|
let params = {
|
||||||
chat_id: chat_id,
|
chat_id: chat_id,
|
||||||
@ -100,22 +124,20 @@ function sendMessage(text) {
|
|||||||
resolveWithFullResponse: true,
|
resolveWithFullResponse: true,
|
||||||
forever: true
|
forever: true
|
||||||
};
|
};
|
||||||
return Request(options)
|
return Request(options).then(resp => {
|
||||||
.then(resp => {
|
if(resp.statusCode !== 200) {
|
||||||
if (resp.statusCode !== 200) {
|
throw new Error(resp.statusCode + ':\n' + resp.body);
|
||||||
throw new Error(resp.statusCode + ':\n' + resp.body);
|
}
|
||||||
}
|
let updates = JSON.parse(resp.body);
|
||||||
let updates = JSON.parse(resp.body);
|
if(updates.ok) {
|
||||||
if (updates.ok) {
|
console.log("Message send via Telegram")
|
||||||
console.log("Message send via Telegram")
|
return updates;
|
||||||
return updates;
|
} else {
|
||||||
} else {
|
console.log(`something went wrong while sending message to telegram see detailed error below.`)
|
||||||
console.log(`something went wrong while sending message to telegram see detailed error below.`)
|
console.error(updates)
|
||||||
console.error(updates)
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
}).catch(error => {
|
||||||
})
|
throw error;
|
||||||
.catch(error => {
|
});
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user