From e5d88f8fc1af09c544c33aede735b5e3d58fdecc Mon Sep 17 00:00:00 2001
From: Piyush Dixit <79581397+PiyushDixit96@users.noreply.github.com>
Date: Mon, 10 May 2021 17:52:52 +0530
Subject: Add files via upload
---
index.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 98 insertions(+)
create mode 100644 index.js
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..a01c65a
--- /dev/null
+++ b/index.js
@@ -0,0 +1,98 @@
+#!/usr/bin/env node
+const dotenv = require('dotenv');
+dotenv.config();
+
+const token = process.env['TELEGRAM_TOKEN'];
+const chat_id = process.env['TELEGRAM_CHAT_ID'];
+const api_key = process.env['BINANCE_API_KEY'];
+const secret_key = process.env['BINANCE_SECRET_KEY'];
+const NODE_ENV = process.env['NODE_ENV'];
+const port = process.env.PORT || 3000;
+const timeZone = process.env.TIME_ZONE_STRING || 'Asia/Kolkata';
+
+//Indian time string
+const event = new Date().toLocaleString('en-IN', { timeZone: timeZone, timeZoneName: 'short' });
+
+//sending telegram message
+const Slim_bot = require('slimbot');
+const bot = new Slim_bot(token);
+
+//only for heroku port error
+const express = require('express');
+const app = express();
+app.all('/', (req, res) => res.send('Bot is Running'));
+app.listen(port, () => console.log(`${event} - Server started on ${port} port`));
+
+//BinanceWS
+const binanceApi = require('binance');
+const binanceWS = new binanceApi.BinanceWS(true);
+try {
+ binanceRest = new binanceApi.BinanceRest({
+ key: api_key,
+ secret: secret_key,
+ recvWindow: 10000
+ })
+ binanceWS.onUserData(binanceRest, data => {
+ console.log(`${event} - Session Message: `, data);
+ process_data(data);
+
+ }, 60000).then(ws => {
+ console.log(`${event} - Monitoring Spot User Order Data for binance.com`);
+ toTelegram(`Binance Spot Order Monitor Started\nthis message shows that you or heroku(if your are using) restart the bot.`)
+ })
+} catch (err) {
+ console.error(`${event} - ${err}`)
+ toTelegram(err.toString())
+}
+
+function fixFloat(floatNum, Precision = 8) {
+ const num = Number.parseFloat(floatNum).toFixed(Precision);
+ const str = num.toString();
+ return str.replace(/(\.\d+?)0+\b/g, "$1") //fix 20.000 to 20.0 or 0.0000000120 to 0.000000012
+}
+
+
+function process_data(data) {
+ let txt;
+ if (data.eventType === 'executionReport') {
+ if (data.executionType === 'NEW') {
+ if (data.orderStatus === 'NEW') {
+ txt = `✅ ✅ ✅\nSpot Order CREATED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}`
+ }
+ else if (data.orderStatus === 'REJECTED') {
+ txt = `🚫 🚫 🚫\nSpot Order REJECTED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}`
+ }
+ } else if (data.executionType === 'CANCELED') {
+ if (data.orderStatus === 'CANCELED') {
+ txt = `🛑 🛑 🛑\nSpot Order CANCELED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}`
+ }
+ } else if (data.executionType === 'TRADE') {
+ if (data.orderStatus === 'PARTIALLY_FILLED') {
+ txt = `💰\nSpot Order PARTIALLY FILLED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nLast Filled: ${fixFloat(data.lastTradeQuantity)}\nRemaining: ${fixFloat(Number(data.quantity) - Number(data.accumulatedQuantity))}\nOrder ID: #ID${data.orderId}`
+ } else if (data.orderStatus === 'FILLED') {
+ txt = `💰 💰 💰\nSpot Order FULLY FILLED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nFilled: ${fixFloat(data.accumulatedQuantity)}\nOrder ID: #ID${data.orderId}`
+ }
+ } else if (['REPLACED', 'EXPIRED', 'PENDING_CANCEL'].includes(data.orderStatus)) {
+ txt = `🚫 🚫 🚫\nSpot Order ${data.orderStatus}\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${fixFloat(data.price)}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}`
+ } else {
+ txt = `⚠️ ⚠️ ⚠️\nUndefined\nExecution Type: ${data.executionType}\nOrder Status ${data.orderStatus}\nFull Details:\n${msg}`
+ }
+ toTelegram(txt)
+ }
+}
+
+function toTelegram(text) {
+ let parse_mode = {
+ parse_mode: 'html'
+ };
+ bot.sendMessage(chat_id, text, parse_mode)
+ .then(message => {
+ //console.log(message);
+ if (message['ok'] === true) {
+ console.log(`${event} - Message send to ${message['result']['chat']['first_name']} via Telegram`)
+ } else if (message['ok'] === false) {
+ console.log(`${event} - something went wrong while sending message to telegram see detailed error below.`)
+ console.error(message)
+ }
+ });
+}
\ No newline at end of file
--
cgit v1.2.3