-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
264 lines (210 loc) · 8.28 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict'
const NEM = require('./nodejs2nem/NEM.js');
const Telegram = require('telegram-node-bot')
const TelegramBaseController = Telegram.TelegramBaseController
const StartController = Telegram.StartController
const TextCommand = Telegram.TextCommand
var fs = require('fs');
var config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
var telegram_key = config.telegram_key
console.log("nis address: " + config.nis_address)
console.log("telegram_key: " + config.telegram_key)
var conf = { 'nis_address': config.nis_address};
var nem = new NEM(conf);
Array.prototype.contains = function(element){
return this.indexOf(element) > -1;
};
const tg = new Telegram.Telegram(telegram_key,
{
workers: 1,
webAdmin: {
port: config.local_port,
host: 'localhost'
}
})
function makeDir(dirname)
{
if (!fs.existsSync(dirname)){
fs.mkdirSync(dirname);
}
}
function getDirectories (srcpath) {
const fs = require('fs')
const path = require('path')
return fs.readdirSync(srcpath)
.filter(file => fs.statSync(path.join(srcpath, file)).isDirectory())
}
function readAddress(address, old_wallet, chat_id, cb){
//formatting the address removing the -
address = address.replace(/-/g,"")
console.log("querying: " + address)
var query = '/account/get?address=' + address
nem.nisGet(query, null
,function(err) {
console.log(err)
tg.api.sendMessage(chat_id, 'Error while querying wallet: [' + old_wallet.account.address + "]")
tg.api.sendMessage(chat_id, 'Error= {' + err + '}')
}
,function(res) {
if(res.error != undefined && res.error){
console.log(res)
cb(old_wallet, res)
return;
}
res.account.balance = res.account.balance/1000000
res.account.vestedBalance = res.account.vestedBalance/1000000
//response handler function
// output the response here
console.log(res);
// or call other user defined code
cb(old_wallet, res, chat_id)
return;
});
}
tg.onMaster(() => {
function checkAccounts(){
var dirs = getDirectories('./.storage')
for (var i = 0, len = dirs.length; i < len; i++) {
var chat_id = dirs[i]
console.log(chat_id)
// looping through all the chat ids
var wallets_dir = './.storage/' + chat_id
var files = fs.readdirSync(wallets_dir)
for (var w = 0; w < files.length; w++) {
var old_wallet = JSON.parse(fs.readFileSync(wallets_dir + '/' + files[w], 'utf8'))
var wallet_key = files[w]
readAddress(wallet_key, old_wallet, chat_id, function (old_wallet, wallet, chat_id) {
if(wallet.error != undefined && wallet.error){
console.log(wallet)
$.sendMessage('Error: ' + wallet.error + ' -> ' + wallet.message)
return
}
if(wallet.error != undefined && wallet.error){
console.log(wallet)
return
}
//TODO add more checks here if you want to be notified of different things
if(wallet.account.balance != old_wallet.account.balance){
tg.api.sendMessage(chat_id, 'Balance for [' + old_wallet.account.address + '] was [' + old_wallet.account.balance
+ "] and now it's [" + wallet.account.balance+ "]")
//saving results
console.log('saving wallet: ' + wallet.account.address)
var storage_dir = './.storage/' + chat_id
var json_string = JSON.stringify(wallet);
fs.writeFile(storage_dir + '/' + wallet.account.address, json_string, function (err) {
if (err) return console.log(err);
});
}
else{
console.log("balance didn't change: " + wallet.account.address + " " + wallet.account.balance)
}
})
}
}
}
//checking every 2 minutes
setInterval(checkAccounts, 1 * 120000)
})
tg.onMaster(() => {
var opt = nem.getOptions();
console.log(opt);
//setting up storage
makeDir('./.storage')
})
class BalanceController extends TelegramBaseController {
/**
* @param {Scope} $
*/
balanceHandler($) {
// setting up storage folder
var wallets_dir = './.storage/' + $.chatId.toString()
if (!fs.existsSync(wallets_dir)){
$.sendMessage("you don't have any wallets registered!")
}
var files = fs.readdirSync(wallets_dir);
for (var i = 0, len = files.length; i < len; i++) {
var wallet = JSON.parse(fs.readFileSync(wallets_dir + '/' + files[i], 'utf8'))
$.sendMessage(files[i] + " balance: " + wallet.account.balance)
}
}
get routes() {
return {
'balanceCommand': 'balanceHandler'
}
}
}
class HelpController extends TelegramBaseController {
/**
* @param {Scope} $
*/
helpHandler($) {
$.sendMessage('This bot registers a NEM wallet and notifies you when the balance changes')
$.sendMessage('/register to register a new wallet')
$.sendMessage('/balance to see your current balance')
}
get routes() {
return {
'helpCommand': 'helpHandler'
}
}
}
class OtherwiseController extends TelegramBaseController {
handle() {
console.log('otherwise')
}
}
class RegisterController extends TelegramBaseController {
/**
* @param {Scope} $
*/
registerHandler($) {
const form = {
wallet: {
q: 'Send me the wallet you\'d like to register',
error: 'sorry, wrong input',
validator: (message, callback) => {
var wallet_key = message.text.toString().toUpperCase().trim()
if(!wallet_key || wallet_key.length != 46){
$.sendMessage("wrong address, please provide a well formed address [NAYFRF-6C2DZK-KEQEE2-SNVBBD-G354SY-F4XHMY-JDFP]")
return
}
//reading/saving wallet
readAddress(wallet_key, null, null, function (old, wallet, chat_id) {
if(wallet.error != undefined && wallet.error){
console.log(wallet)
$.sendMessage('Error: ' + wallet.error + ' -> ' + wallet.message)
return
}
if(wallet.error != undefined && wallet.error){
console.log(wallet)
return
}
var storage_dir = "./.storage/" + $.chatId.toString()
makeDir(storage_dir)
var json_string = JSON.stringify(wallet)
fs.writeFile(storage_dir + '/' + wallet.account.address, json_string, function (err) {
if (err) return console.log(err);
console.log('File saved: ' + wallet.account.address);
});
$.sendMessage('Registered wallet: ' + wallet_key)
})
callback(true, message.text) //you must pass the result also
return
}
}
}
$.runForm(form, (result) => {
})
}
get routes() {
return {
'registerCommand': 'registerHandler'
}
}
}
tg.router
.when(new TextCommand('/help', 'helpCommand'), new HelpController())
.when(new TextCommand('/balance', 'balanceCommand'), new BalanceController())
.when(new TextCommand('/register', 'registerCommand'),new RegisterController())
.otherwise(new OtherwiseController())
console.log("Starting bot!")