-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
57 lines (50 loc) · 1.68 KB
/
index.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
const Binance = require('binance')
const Telegraf = require('telegraf')
const secrets = require('./secrets')
const binance = new Binance.BinanceRest({
key: secrets.binanceKey,
secret: secrets.binanceSecret,
handleDrift: true
})
const binanceWS = new Binance.BinanceWS(true)
const bot = new Telegraf(secrets.bot)
// On command `/start`
bot.start((ctx) => {
console.log('Use this as the "you" field in secrets.json :')
console.log(ctx.update.message.from.id)
ctx.reply('👋')
})
// On command `/balance`
bot.command('/balance', (ctx) => {
binance.account().then((data) => {
ctx.reply(
data.balances.map(x => {
const value = parseFloat(x.free) + parseFloat(x.locked)
if (value) return `<b>${x.asset}</b> : <code>${value}</code>`
else return undefined
}).filter(x => x).join(`\n`), {parse_mode: 'HTML'}
)
}).catch(x => {
console.log(x)
bot.telegram.sendMessage(secrets.you, JSON.stringify(x))
})
})
// On every event that happens with your binance account
binanceWS.onUserData(binance, (data) => {
if (data.eventType === "executionReport") {
console.log(data)
bot.telegram.sendMessage(
secrets.you,
`<i>${data.orderStatus}</i> <b>${data.symbol}</b> ${data.side}@${parseFloat(data.price)} : <code>${parseFloat(data.accumulatedQuantity)}/${parseFloat(data.quantity)}</code>`,
{parse_mode: 'HTML'}
)
} else {
// Uncomment the following line if you want the raw JSON data for events that are not execution reports
//bot.telegram.sendMessage(secrets.you, JSON.stringify(data))
}
}).catch(x => {
console.log(x)
bot.telegram.sendMessage(secrets.you, JSON.stringify(x))
})
// Start the bot !
bot.startPolling()