-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (114 loc) · 3.88 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
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
const Telegraf = require('telegraf')
const LocalSession = require('telegraf-session-local')
const commandParts = require('telegraf-command-parts')
const request = require('request')
const throttledRequest = require('throttled-request')(request)
throttledRequest.configure({
requests: 1,
milliseconds: 2000
})
const getStatUrl = (wallet) => {
return `https://api.etn.spacepools.org/v1/stats/address/${wallet}`
}
const getPriceUrl = () => {
return 'https://api.coinmarketcap.com/v1/ticker/electroneum/'
}
const bot = new Telegraf(process.env.BOT_TOKEN)
const isValidWallet = function (w) {
return (/^etn(.)/.test(w) && w.length > 60)
}
const helpOutput = [
'/add_wallet [ENT WALLET]-- add wallet to list',
'/wallets -- wallets list',
'/stat -- stat for each wallet',
'/price -- price info',
]
bot.use((new LocalSession({database: 'var/session.json'})).middleware())
bot.use(commandParts())
bot.start((ctx) => {
console.log('started:', ctx.from.id)
ctx.session = {
wallets: {},
}
ctx.reply('Welcome!')
return ctx.reply(helpOutput.join('\n'))
})
bot.command('add_wallet', (ctx) => {
const wallet = ctx.state.command.args
if (isValidWallet(wallet)) {
if (ctx.session.wallets[wallet]) {
return ctx.reply('Wallet is already added')
}
ctx.session.wallets[wallet] = {}
console.log('Add wallet', wallet)
return ctx.reply('Wallet was added')
}
return ctx.reply('Invalid wallet')
})
bot.command('wallets', (ctx) => {
let wallets = Object.keys(ctx.session.wallets)
if (wallets.length === 0) {
return ctx.reply(`You don't have any wallets`)
}
return ctx.reply('Yours wallets:\n' + wallets.join('\n'))
})
bot.command('stat', (ctx) => {
let wallets = Object.keys(ctx.session.wallets)
if (wallets.length === 0) {
return ctx.reply(`You don't have any wallets`)
}
ctx.reply('Please, wait ⌛')
wallets.map(async (wallet) => {
try {
let url = getStatUrl(wallet)
throttledRequest(url, {json: true}, (err, res, body) => {
if (err) {
console.log('Error occurred')
console.log(err)
return ctx.reply('error')
}
if (body.stats) {
const output = [
`💳 Wallet: ${wallet}`,
`🏦 Pending Balance: ${body.stats.balance / 100} ETN`,
`💵 Total Paid: ${body.stats.paid / 100} ETN`,
`⚙️ Hash Rate: ${body.stats.hashrate || 0}`
]
return ctx.reply(output.join('\n'))
}
return ctx.reply('Error')
})
} catch (e) {
console.log('Error: ' + e.message)
ctx.reply('error')
}
})
})
const chartEmoji = (value) => {
return value > 0 ? '📈' : '📉'
}
const formatPercents = (value) => {
return value > 0 ? '+' + value : value
}
bot.command('price', (ctx) => {
ctx.reply('Please, wait ⌛')
throttledRequest(getPriceUrl(), {json: true}, (err, res, data) => {
if (data[0]) {
data = data[0]
let output = [
`💰 ETN to USD: ${data.price_usd}`,
`💰 ETN to BTC: ${data.price_btc}`,
`${chartEmoji(data.percent_change_1h)} Change 1 hour: ${formatPercents(data.percent_change_1h)}%`,
`${chartEmoji(data.percent_change_24h)} Change 24 hours: ${formatPercents(data.percent_change_24h)}%`,
`${chartEmoji(data.percent_change_7d)} Change 7 days: ${formatPercents(data.percent_change_7d)}%`,
]
ctx.reply(output.join('\n'))
} else {
return ctx.reply('Error')
}
})
})
bot.command('help', (ctx) => {
ctx.reply(helpOutput.join('\n'))
})
bot.startPolling()