-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
426 lines (387 loc) · 12.4 KB
/
app.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
var WavesAPI = require('./waves-api');
const Waves = WavesAPI.create(WavesAPI.MAINNET_CONFIG);
require('dotenv').config()
const Telegraf = require('telegraf');
const session = require('telegraf/session');
const Stage = require('telegraf/stage');
const Markup = require('telegraf/markup');
const Scene = require('telegraf/scenes/base');
const {
BOT_TOKEN,
DATABASE_URI,
ADMIN_ID,
} = process.env
function parseBotDataText (data) {
if (data.message !== undefined) {
return data.message.text;
} else if (data.update.callback_query !== undefined) {
return data.update.callback_query.message.text;
} else if (data.update.message !== undefined) {
return data.update.message.text;
}
}
function parseBotDataFrom (data) {
if (data.message !== undefined) {
return data.message.from;
} else if (data.update.callback_query !== undefined) {
return data.update.callback_query.from;
} else if (data.update.message !== undefined) {
return data.update.message.from;
}
}
function default_response (ctx, text, isMarkdown) {
if (isMarkdown) {
return ctx.replyWithMarkdown(text, Markup.keyboard([
['Send', 'Receive'],
['Wallet', 'Settings']
]).oneTime().resize().extra());
} else {
return ctx.reply(text, Markup.keyboard([
['Send', 'Receive'],
['Wallet', 'Settings']
]).oneTime().resize().extra());
}
}
const sendMoneyAmountScene = new Scene('send_money_amount');
sendMoneyAmountScene.enter((ctx, next) => {
new Promise (function(resolve, reject) {
return ctx.reply('Enter amount');
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
sendMoneyAmountScene.on('message', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataText = parseBotDataText(ctx);
let botDataFrom = parseBotDataFrom(ctx);
if (botDataText === 'Cancel') {
ctx.scene.leave();
return default_response(ctx, `Cancel`, false);
}
let amount = botDataText;
let address = ctx.session.recipients_address;
if (isNaN(amount)) {
return ctx.reply('Enter number amount');
}
Waves.API.Node.addresses.balance(address).then((balance) => {
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
Waves.API.Node.addresses.balance(mongo_result[0].waves_address).then((waves_result) => {
amount = Number(amount*100000000);
let waves_balance = waves_result.balance;
waves_balance = Number(waves_balance)+Number(0.001);
if (amount>waves_balance) {
return ctx.reply('Insufficient funds');
}
const seed = Waves.Seed.fromExistingPhrase(mongo_result[0].waves_phrase);
const transferData = {
recipient: address,
assetId: 'WAVES',
amount: amount,
feeAssetId: 'WAVES',
fee: 100000,
attachment: 'Waves bot - https://t.me/waves_wallet_bot',
timestamp: Date.now()
};
Waves.API.Node.transactions.broadcast('transfer', transferData, seed.keyPair).then((responseData) => {
ctx.scene.leave();
ctx.session.recipients_address = null;
return default_response(ctx, `Successfully sent!`, false);
});
});
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
}).catch(error => {
return ctx.reply('Invalid address');
})
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
const sendMoneyScene = new Scene('send_money');
sendMoneyScene.enter((ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
Waves.API.Node.addresses.balance(mongo_result[0].waves_address).then((waves_result) => {
if (waves_result.balance === 0) {
ctx.scene.leave();
return default_response(ctx, `You have no money on your wallet`, false);
} else {
return ctx.replyWithMarkdown('Enter the recipients address', Markup.keyboard([
['Cancel']
]).oneTime().resize().extra());
}
});
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
sendMoneyScene.hears('Cancel', (ctx, next) => {
ctx.scene.leave();
return default_response(ctx, `Cancel`, false);
});
sendMoneyScene.on('message', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataText = parseBotDataText(ctx);
let botDataFrom = parseBotDataFrom(ctx);
let address = botDataText;
Waves.API.Node.addresses.balance(address).then((balance) => {
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
Waves.API.Node.addresses.balance(mongo_result[0].waves_address).then((waves_result) => {
if (address === mongo_result[0].waves_address) {
return ctx.reply('You can not send money to yourself');
}
ctx.scene.leave();
ctx.session.recipients_address = address;
return ctx.scene.enter('send_money_amount');
});
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
}).catch(error => {
return ctx.reply('Invalid address');
})
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
const bot = new Telegraf(BOT_TOKEN);
const stage = new Stage([sendMoneyScene, sendMoneyAmountScene])
bot.use(session())
bot.use(stage.middleware())
const mongoose = require('mongoose');
mongoose.connect(DATABASE_URI {
useNewUrlParser: true
});
let db = mongoose.connection;
db.on('error', function() {
console.log('Error connection to MongoDB');
});
db.once('open', function() {
console.log('Successfuly connection to MongoDB');
});
let users_schema = mongoose.Schema({
telegram_id: { type: Number, required: true },
waves_address: { type: String, required: true },
waves_phrase: { type: String, required: true },
bot_lang: { type: String, required: true },
});
let Users = mongoose.model('Users', users_schema);
bot.start((ctx) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
if (mongo_result.length === 0) {
return ctx.reply('Welcome! Please shoose language', Markup.inlineKeyboard([
Markup.callbackButton('🇺🇸 English', 'select_eng_lang'),
]).extra());
} else {
return default_response(ctx, `Bot did not understand you`, false);
}
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.action('select_eng_lang', (ctx, next) => {
new Promise (function(resolve, reject) {
ctx.reply('English is selected');
let botDataFrom = parseBotDataFrom(ctx);
const seed = Waves.Seed.create();
const newUser = new Users({
_id: new mongoose.Types.ObjectId(),
telegram_id: Number(botDataFrom.id),
waves_address: seed.address,
waves_phrase: seed.phrase,
bot_lang: 'en'
});
newUser
.save()
.then(mongo_create_new_user => {
return default_response(ctx, `Bot has generated your wallet`, false);
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR WHEN CREATED NEW USER!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Send', (ctx, next) => {
new Promise (function(resolve, reject) {
return ctx.scene.enter('send_money');
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Receive', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
return default_response(ctx, `*${mongo_result[0].waves_address}*`, true);
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Wallet', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
Waves.API.Node.addresses.balance(mongo_result[0].waves_address).then((waves_result) => {
let waves_balance = Number(waves_result.balance/100000000);
return ctx.replyWithMarkdown(`*${waves_balance} WAVES*`, Markup.inlineKeyboard([
Markup.callbackButton('Transactions history', 'transactions_history'),
]).extra())
});
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Settings', (ctx, next) => {
new Promise (function(resolve, reject) {
return ctx.reply('Settings', Markup.keyboard([
['Export seed phrase', 'Cancel'],
]).oneTime().resize().extra());
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Export seed phrase', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
return default_response(ctx, `Mnemonic phrase - *${mongo_result[0].waves_phrase}*\n\n*Save and delete this message*`, true);
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.command('users_amount', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
if (botDataFrom.id !== ADMIN_ID) {
return false;
}
Users.find()
.exec()
.then(mongo_result => {
return default_response(ctx, `Users amount - *${mongo_result.length}*`, true);
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.hears('Cancel', (ctx, next) => {
new Promise (function(resolve, reject) {
return default_response(ctx, `Cancel`, false);
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.action('transactions_history', (ctx, next) => {
new Promise (function(resolve, reject) {
return default_response(ctx, `Coming soon...`, false);
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.on('text', (ctx, next) => {
new Promise (function(resolve, reject) {
let botDataFrom = parseBotDataFrom(ctx);
Users.find({telegram_id: botDataFrom.id})
.exec()
.then(mongo_result => {
if (mongo_result.length === 0) {
return ctx.reply('Welcome! Please shoose language', Markup.inlineKeyboard([
Markup.callbackButton('🇺🇸 English', 'select_eng_lang'),
]).extra());
} else {
return default_response(ctx, `Bot did not understand you`, false);
}
})
.catch(mongo_error => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
})
.catch ((error) => {
bot.telegram.sendMessage(ADMIN_ID, 'BOT ERROR!');
return default_response(ctx, `Bot error`, false);
});
});
bot.startPolling();