-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
780 lines (693 loc) · 24.7 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const { ethers } = require('ethers');
const port = 5000;
const config = require('./config.json');
const Telegraf = require('telegraf')
const extra = require('telegraf/extra');
const markup = extra.markdown();
const { nanoid } = require('nanoid');
const ses = require('node-ses');
const client = ses.createClient({
key: config.AWS_SES.KEY,
secret: config.AWS_SES.SECRET,
amazon: config.AWS_SES.REGION
});
const MongoClient = require('mongodb').MongoClient;
const dbUrl = 'mongodb://localhost:27017';
const dbClient = new MongoClient(dbUrl, { useUnifiedTopology: true });
const dbName = 'notification_server';
let emailDbCollection;
let telegramDbCollection;
dbClient.connect(err => {
if (err) {
console.log("Error connecting to MongoDB:", err);
return;
}
const db = dbClient.db(dbName);
// it's ok to leave the DB connection open: https://stackoverflow.com/a/18651208
// EMAIL
emailDbCollection = db.collection('email_subscribers');
// load existing subscribers
emailDbCollection.find({}).toArray((err, docs) => {
if (err) { console.log("Error:", err); }
docs.map(userObj => {
let email = userObj.email;
emailSubscribers[email] = userObj;
authKeyToEmail[userObj.authKey] = email;
if (userObj.ethAddress) {
ethAddressToEmail[userObj.ethAddress] = email;
}
});
console.log('Loaded ' + docs.length + ' email subscribers from DB');
});
// TELEGRAM
telegramDbCollection = db.collection('telegram_subscribers');
// load existing subscribers
telegramDbCollection.find({}).toArray((err, docs) => {
if (err) { console.log("Error:", err); }
docs.map(userObj => {
let telegramId = userObj.telegramId;
activeTelegramUsers[telegramId] = userObj;
if (userObj.ethAddress) {
ethAddressToTelegramUser[userObj.ethAddress] = telegramId;
}
});
console.log('Loaded ' + docs.length + ' telegram subscribers from DB');
});
});
const storeEmailSubscriberInDb = userObj => {
emailDbCollection.insertOne(userObj, (err, result) => {
if (err) { console.log("Error:", err); }
console.log("Stored email subscriber in DB: ", userObj.email);
});
};
const removeEmailSubscriberFromDb = email => {
emailDbCollection.deleteOne({ email : email }, (err, result) => {
if (err) { console.log("Error:", err); }
console.log('Removed email ' + email + ' from DB');
});
};
const storeTelegramSubscriberInDb = userObj => {
telegramDbCollection.insertOne(userObj, (err, result) => {
if (err) { console.log("Error:", err); }
console.log("Stored telegram subscriber in DB: ", userObj.telegramId);
});
};
const updateTelegramSubscriberInDb = telegramId => {
telegramDbCollection.updateOne({ telegramId : telegramId },
{ $set: {
ethAddress: activeTelegramUsers[telegramId].ethAddress,
events: activeTelegramUsers[telegramId].events
}}, (err, result) => {
if (err) { console.log("Error:", err); }
console.log('Updated telegram user ' + telegramId + ' in DB');
});
};
const removeTelegramSubscriberFromDb = telegramId => {
telegramDbCollection.deleteOne({ telegramId : telegramId }, (err, result) => {
if (err) { console.log("Error:", err); }
console.log('Removed telegram user ' + telegramId + ' from DB');
});
};
const serverLaunchTime = Date.now();
const blockedTimeAfterLaunch = 5; // seconds
// when subscribing with ethers.js, sometimes contract events from BEFORE subscribing are
// fired immediately, the initial block-period is to avoid sending out notifications for these
const inBlockedPhase = () => {
return (Date.now() - serverLaunchTime) / 1000 < blockedTimeAfterLaunch;
};
const specialChars = {
telegram: {
newLine: '\n',
codeStart: '`',
codeEnd: '`',
italicStart: '_',
italicEnd: '_'
},
email: {
newLine: '<br>',
codeStart: '<i>',
codeEnd: '</i>',
italicStart: '<i>',
italicEnd: '</i>'
}
};
// ------------------------ CONTRACT EVENT SUBSCRIPTIONS ------------------------
let provider;
if (config.INFURA_API_KEY) {
provider = new ethers.providers.InfuraProvider('rinkeby', config.INFURA_API_KEY);
} else {
provider = new ethers.providers.JsonRpcProvider('http://localhost:7545');
}
let contracts = {
Fin4MainContract: new ethers.Contract(config.FIN4MAIN_ADDRESS,
require(config.CONTRACTS_BUILD_DIRECTORY + '/Fin4Main.json').abi, provider)
};
let contractEvents = {
Fin4TokenCreated: {
contractName: 'Fin4TokenManagement',
title: 'New token created',
audience: 'all',
sendAsMessage: true
},
ClaimSubmitted: {
contractName: 'Fin4Claiming',
title: 'Claim submitted',
audience: 'claimer',
sendAsMessage: false
},
ClaimApproved: {
contractName: 'Fin4Claiming',
title: 'Claim approved',
audience: 'claimer',
sendAsMessage: true
},
ClaimRejected: {
contractName: 'Fin4Claiming',
title: 'Claim rejected',
audience: 'claimer',
sendAsMessage: true
},
UpdatedTotalSupply: {
contractName: 'Fin4Claiming',
title: 'Updated total supply',
audience: 'claimer',
sendAsMessage: false
},
VerifierPending: {
contractName: 'Fin4Claiming',
title: 'Verifier pending',
audience: 'claimer',
sendAsMessage: false
},
VerifierApproved: {
contractName: 'Fin4Claiming',
title: 'Verifier approved',
audience: 'claimer',
sendAsMessage: true
},
VerifierRejected: {
contractName: 'Fin4Claiming',
title: 'Verifier rejected',
audience: 'claimer',
sendAsMessage: true
},
NewMessage: {
contractName: 'Fin4Messaging',
title: 'New message',
audience: 'receiver',
sendAsMessage: true
},
MessageMarkedAsRead: {
contractName: 'Fin4Messaging',
title: 'Message marked as read',
audience: 'receiver',
sendAsMessage: false
},
SubmissionAdded: {
contractName: 'Fin4Verifying',
title: 'Submission added',
audience: 'all',
sendAsMessage: false
}
};
contracts.Fin4MainContract.getSatelliteAddresses().then(addresses => {
// 2 Fin4TokenManagement
contracts.Fin4TokenManagement = new ethers.Contract(addresses[2],
require(config.CONTRACTS_BUILD_DIRECTORY + '/Fin4TokenManagement.json').abi, provider
);
// 3 Fin4Claiming
contracts.Fin4Claiming = new ethers.Contract(addresses[3],
require(config.CONTRACTS_BUILD_DIRECTORY + '/Fin4Claiming.json').abi, provider
);
// 5 Fin4Messaging
contracts.Fin4Messaging = new ethers.Contract(addresses[5],
require(config.CONTRACTS_BUILD_DIRECTORY + '/Fin4Messaging.json').abi, provider
);
// 6 Fin4Verifying
contracts.Fin4Verifying = new ethers.Contract(addresses[6],
require(config.CONTRACTS_BUILD_DIRECTORY + '/Fin4Verifying.json').abi, provider
);
Object.keys(contractEvents).map(eventName => {
let contractName = contractEvents[eventName].contractName;
let audience = contractEvents[eventName].audience;
contracts[contractName].on(eventName, (...args) => {
if (inBlockedPhase()) {
return;
}
let values = extractValues(contractName, args);
console.log('Received ' + eventName + ' Event from ' + contractName + ' contract');
if (audience === 'all') {
sendToAll(eventName, values);
} else {
sendToUser(values[audience], eventName, values)
}
});
});
});
// ------------------------ HELPER METHODS ------------------------
const extractValues = (contractName, args) => {
/*
Rearranging the contract event data like this seems necessary
because it arrives as a mix of array and object:
0: 0x...
1: 1
tokenAddr: 0x...
claimId: 1
When I sent this to the frontend or use JSON.stringify(), it keeps
only the array-part and the keys are lost. I want to pass them though.
*/
let raw = args.pop().args;
let values = {};
Object.keys(raw).map(key => {
if (isNaN(key)) { // keep it only if the key is not a number
let value = raw[key];
if (value._isBigNumber) {
value = value.toString();
}
values[key] = value;
}
});
values['contractName'] = contractName;
return values;
};
const isValidAddress = addr => {
try {
ethers.utils.getAddress(addr);
} catch (e) { return false; }
return true;
};
const sendToAll = (eventName, values) => {
// io.emit(eventName, values);
let eventObj = contractEvents[eventName];
if (!eventObj.sendAsMessage) {
return;
}
buildTelegramMessage(eventName, values, message => {
Object.keys(activeTelegramUsers)
.filter(telegramId => activeTelegramUsers[telegramId].events[eventName])
.map(telegramId => bot.telegram.sendMessage(telegramId, message, markup));
});
buildEmailMessage(eventName, values, message => {
Object.keys(emailSubscribers)
.map(email => emailSubscribers[email])
.filter(user => user.events[eventName]) // only users who subscribed to this event type
.map(user => sendEmail(user.email, eventObj.title, message));
});
};
const sendToUser = (ethAddress, eventName, values) => {
// emitOnSocket(ethAddress, eventName, values);
let eventObj = contractEvents[eventName];
if (!eventObj.sendAsMessage) {
return;
}
let telegramId = ethAddressToTelegramUser[ethAddress];
let sendViaTelegram = telegramId && activeTelegramUsers[telegramId].events[eventName];
if (telegramId && sendViaTelegram) {
buildTelegramMessage(eventName, values, message => {
bot.telegram.sendMessage(telegramId, message, markup);
});
}
let emailUser = ethAddressToEmail[ethAddress];
let sendByEmail = emailUser && emailSubscribers[emailUser].events[eventName];
if (emailUser && sendByEmail) {
buildEmailMessage(eventName, values, message => {
sendEmail(emailUser, eventObj.title, message)
});
}
};
const fetchTokenInfo = (tokenAddr, done) => {
if (tokenInfos[tokenAddr]) {
done();
return;
}
contracts.Fin4TokenManagement.getTokenInfo(tokenAddr).then(({ 1: name, 2: symbol }) => {
tokenInfos[tokenAddr] = {
name: name,
symbol: symbol
};
done();
});
};
const fetchVerifierInfo = (verifierAddr, done) => {
if (verifierInfos[verifierAddr]) {
done();
return;
}
contracts.Fin4Verifying.getVerifierTypeInfo(verifierAddr).then(({ 0: contractName }) => { // TODO use 1: nameTransKey
verifierInfos[verifierAddr] = {
contractName: contractName
};
done();
});
};
const formatToken = (obj, chars) => {
return chars.codeStart + '[' + obj.symbol + '] ' + obj.name + chars.codeEnd;
};
const tokenInfos = {};
const verifierInfos = {};
const buildTelegramMessage = (eventName, values, callback) => {
return buildMessage(eventName, values, callback, specialChars.telegram);
};
const buildEmailMessage = (eventName, values, callback) => {
return buildMessage(eventName, values, callback, specialChars.email);
};
const buildMessage = (eventName, values, callback, chars) => {
// let intro = 'A message from the ' + values.contractName + ' contract to ' + (toAll ? 'all' : 'you') + ':\n';
let message = '';
let text;
switch(eventName) {
case 'Fin4TokenCreated':
let descriptionParts = values.description.split('||');
message = 'New token created:' + chars.newLine + formatToken(values, chars);
if (descriptionParts.length > 1 && descriptionParts[0]) {
message += chars.newLine + descriptionParts[0];
}
callback(message);
break;
case 'ClaimApproved':
case 'ClaimRejected':
text = () => {
let tokenInfo = tokenInfos[values.tokenAddr];
if (eventName === 'ClaimApproved') {
return 'Your claim of ' + chars.codeStart + values.mintedQuantity + chars.codeEnd + ' on token ' + formatToken(tokenInfo, chars)
+ ' was successful, your new balance on this token is ' + chars.codeStart + values.newBalance + chars.codeEnd;
}
if (eventName === 'ClaimRejected') {
return 'Your claim on token ' + formatToken(tokenInfo, chars) + ' got rejected';
}
};
fetchTokenInfo(values.tokenAddr, () => {
callback(text());
});
break;
case 'VerifierApproved':
case 'VerifierRejected':
text = () => {
let tokenInfo = tokenInfos[values.tokenAddrToReceiveVerifierNotice];
let verifierInfo = verifierInfos[values.verifierTypeAddress];
message = 'The verifier ' + chars.codeStart + verifierInfo.contractName + chars.codeEnd + (eventName === 'VerifierApproved' ? ' approved' : ' rejected')
+ ' the provided proof for your claim on token ' + formatToken(tokenInfo, chars);
if (values.message) {
message += chars.newLine + 'Attached message: ' + chars.italicStart + values.message + chars.italicEnd;
}
return message;
};
fetchTokenInfo(values.tokenAddrToReceiveVerifierNotice, () => {
fetchVerifierInfo(values.verifierTypeAddress, () => {
callback(text());
});
});
break;
case 'NewMessage':
callback('You received a new message');
break;
}
};
// ------------------------ SOCKET ------------------------
/*
const emitOnSocket = (ethAddr, type, values) => {
let socket = getSocket(ethAddr);
if (socket) {
socket.emit(type, values);
}
};
const getSocket = ethAddr => {
let socketId = ethAddressToSocketId[ethAddr];
if (socketId && io.sockets.sockets[socketId]) {
return io.sockets.sockets[socketId];
}
return null;
};
// active frontends
let ethAddressToSocketId = {};
let socketIdToEthAddress = {};
*/
io.on('connection', socket => {
console.log('New socket connection');
socket.on('get-fin4-url', () => {
socket.emit('get-fin4-url-result', config.FIN4_URL);
});
socket.on('email-signup', msg => {
socket.emit('email-signup-result', emailSignup(msg));
});
socket.on('check-email-auth-key', authKey => {
socket.emit('check-email-auth-key-result', checkEmailAuthkey(authKey));
});
socket.on('unsubscribe-email', authKey => {
socket.emit('unsubscribe-email-result', unsubscribeEmail(authKey));
});
/*socket.on('register', ethAddress => {
ethAddressToSocketId[ethAddress] = socket.id;
socketIdToEthAddress[socket.id] = ethAddress;
console.log('REGISTERED ethAddress: ' + ethAddress, ' socketId: ', socket.id);
console.log('Total registered: ' + Object.keys(ethAddressToSocketId).length);
});
socket.on('disconnect', () => {
console.log('UNREGISTERED ethAddress: ' + socketIdToEthAddress[socket.id], ' socketId: ', socket.id);
delete ethAddressToSocketId[socketIdToEthAddress[socket.id]];
delete socketIdToEthAddress[socket.id];
console.log('Total registered: ' + Object.keys(ethAddressToSocketId).length);
});*/
});
// ------------------------ TELEGRAM BOT ------------------------
const bot = new Telegraf(config.TELEGRAM_BOT_TOKEN);
let activeTelegramUsers = {};
let ethAddressToTelegramUser = {};
bot.command('start', ctx => {
let id = ctx.chat.id;
if (activeTelegramUsers[id]) {
ctx.reply('Oha, it seems I already knew you, please run /stop first if you want to restart');
return;
}
activeTelegramUsers[id] = {
timestampAdded: Date.now(),
telegramId: id,
ethAddress: null,
events: {
Fin4TokenCreated: true,
ClaimApproved: false,
ClaimRejected: false,
VerifierApproved: false,
VerifierRejected: false,
NewMessage: false
}
};
storeTelegramSubscriberInDb(activeTelegramUsers[id]);
console.log('Telegram user ' + id + ' has connected');
ctx.reply('Welcome to the *FIN4Notifications bot*!\n\nFrom now on you will receive notifications about general events, like the creation of a new token. '
+ 'If you also want notifications concerning your account (claim approval etc.), you have to share your public Ethereum address in the format '
+ '```\nmy-address 0x...\n```Note that you thereby allow a link to be made between your Telegram Id and your Ethereum address. That info lives '
+ 'only in the database of the notification server, but servers can be hacked.'
+ '\nFinde more info about this on the site to subscribe by email:\n' + config.THIS_URL
+ '\n\nUse the /help command to see your subscription status and get more infos.'
+ '\nThe /change command describes how to change your subscription.'
+ '\nWith /stop you unsubscribe from all subscriptions.'
+ '\n\nFor transparency, this is the info I am seeing from you:```\n' + JSON.stringify(ctx.chat)
+ '```\nI stored only the `id` from it.', markup);
});
// enable this command via the BotFather on
bot.command('stop', ctx => {
let id = ctx.chat.id;
if (!activeTelegramUsers[id]) {
ctx.reply('Ups, I don\'t think I know you yet, please run the /start command first');
return;
}
let ethAddress = activeTelegramUsers[id].ethAddress;
if (ethAddress) {
delete ethAddressToTelegramUser[ethAddress];
console.log('Removed linkage of telegram id ' + id + ' with eth address ' + ethAddress);
}
delete activeTelegramUsers[id];
removeTelegramSubscriberFromDb(id);
console.log('Telegram user id ' + id + ' has disconnected')
ctx.reply('You are now unsubscribed from all contract events.');
});
bot.command('help', ctx => {
let id = ctx.chat.id;
let telegramUser = activeTelegramUsers[id];
let msg = '*Hi, this is the status of your subscription*:\n'
+ '\n- Your Id: `' + id + '`';
if (telegramUser) {
msg += '\n- Your subscription is active'
let ethAddress = telegramUser.ethAddress;
if (ethAddress) {
msg += '\n- Your Ethereum public address:\n `' + ethAddress + '`';
} else {
msg += '\n- I don\'t know your Ethereum public address';
}
msg += '\n- You are subscribed to these contract events:';
let subscribedEvents = Object.keys(telegramUser.events).filter(eventName => telegramUser.events[eventName]);
if (subscribedEvents.length === 0) {
msg += ' _none_';
}
subscribedEvents.map(eventName => {
msg += '\n - _' + contractEvents[eventName].title + '_';
});
msg += '\n\nTo change which contract events you want to be notified about, use the \change command.'
+ '\nTo unsubscribe from all contract events, use the \stop command.';
} else {
msg += '\n- You are not subscribed to any contract events'
+ '\n\nClick /start to get going.';
}
ctx.reply(msg, markup);
});
bot.command('change', ctx => {
let id = ctx.chat.id;
if (!activeTelegramUsers[id]) {
ctx.reply('Ups, I don\'t think I know you yet, please run the /start command first');
return;
}
let msg = 'Alright, let\'s change which contract events you will be notified about.'
+ ' Use the /help command to see which ones you are currently subscribed to.'
+ '\n\nThese are the available general contract events:';
let index = 1;
Object.keys(contractEvents).filter(eventName => contractEvents[eventName].sendAsMessage && contractEvents[eventName].audience === 'all').map(eventName => {
msg += '\n *' + index + '*: _' + contractEvents[eventName].title + '_';
index += 1;
});
msg += '\nThese are the available account-specific contract events:';
Object.keys(contractEvents).filter(eventName => contractEvents[eventName].sendAsMessage && contractEvents[eventName].audience !== 'all').map(eventName => {
msg += '\n *' + index + '*: _' + contractEvents[eventName].title + '_';
index += 1;
});
msg += '\n\nWrite me `events` followed by the contract events you want to be subscribed to.';
msg += ' For instance:\n`events 1,2,3,6`\nmeans, that you want to hear about all but the verifier events.'
msg += '\nNote that I can\'t subscribe you to any account-specific contract events if I don\'t know your Ethereum public address.'
ctx.reply(msg, markup);
});
bot.on('message', ctx => { // link ethAddress
let id = ctx.chat.id;
if (!activeTelegramUsers[id]) {
ctx.reply('Ups, I don\'t think I know you yet, please run the /start command first.');
return;
}
let text = ctx.message.text;
console.log('Received telegram message from ' + id + ': ' + text);
let keyword = text.split(' ')[0];
if (!(keyword === 'my-address' || keyword === 'events') || text.split(' ').length !== 2) {
ctx.reply('Hey, nice of you to talk to me. That\'s not something I know how to respond to though, sorry.');
}
if (keyword === 'my-address') {
let ethAddress = text.split(' ')[1];
if (!isValidAddress(ethAddress)) {
ctx.reply('Sorry, that is an invalid public address.');
return;
}
activeTelegramUsers[id].ethAddress = ethAddress;
activeTelegramUsers[id].events.ClaimApproved = true;
activeTelegramUsers[id].events.ClaimRejected = true;
activeTelegramUsers[id].events.VerifierApproved = true;
activeTelegramUsers[id].events.VerifierRejected = true;
activeTelegramUsers[id].events.NewMessage = true;
updateTelegramSubscriberInDb(id);
ethAddressToTelegramUser[ethAddress] = id;
ctx.reply('Great, I stored the linkage between your telegram id `' + id + '` and your Ethereum public address `' + ethAddress + '` and will make sure to forward you contract events that are meant for this address.', markup);
console.log('Stored linkage of telegram id ' + id + ' with eth address ' + ethAddress);
}
if (keyword === 'events') {
let eventIndicesRaw = text.split(' ')[1].split(',');
let allSendableEvents = Object.keys(contractEvents).filter(eventName => contractEvents[eventName].sendAsMessage);
let allSendableGeneralEvents = allSendableEvents.filter(eventName => contractEvents[eventName].audience === 'all');
let eventIndices = [];
// validate the indices
for (let i = 0; i < eventIndicesRaw.length; i++) {
let index = Number(eventIndicesRaw[i].trim()) - 1;
if (index < 0 || index >= allSendableEvents.length) {
ctx.reply('There is an error in your event indices, no change was made. I am expecting numbers ranging from `1` to `' + allSendableEvents.length + '`.', markup);
return;
}
eventIndices.push(index); // no need to check for duplicates, they don't hurt
}
allSendableEvents.map((eventName, idx) => {
let verdict = eventIndices.includes(idx);
if (!activeTelegramUsers[id].ethAddress && !allSendableGeneralEvents.includes(eventName)) {
verdict = false;
}
activeTelegramUsers[id].events[eventName] = verdict;
});
updateTelegramSubscriberInDb(id);
ctx.reply('That worked, your subscription is changed. Use /help to see your new status.', markup);
console.log('Telegram user ' + id + ' changed their subscription');
}
});
bot.launch();
// ------------------------ EMAIL ------------------------
// or do one object instead and search more?
let emailSubscribers = {};
let authKeyToEmail = {};
let ethAddressToEmail = {};
const emailSignup = msg => {
let email = msg.email;
let ethAddress = msg.ethAddress;
let events = msg.events;
if (emailSubscribers[email]) {
let message = 'You are already subscribed with that email address. If you wish to change your'
+ ' subscription, please un- and resubscribe.';
sendEmail(email, 'Already subscribed', message);
console.log(email + ' is already subscribed');
return message + ' An email with the link to unsubscribe has been sent to you.';
}
if (ethAddress && !isValidAddress(ethAddress)) {
return 'Sorry, that is an invalid public address';
}
let newAuthKey = nanoid(10);
emailSubscribers[email] = {
timestampAdded: Date.now(),
email: email,
ethAddress: ethAddress,
authKey: newAuthKey,
events: events
};
authKeyToEmail[newAuthKey] = email;
if (ethAddress) {
ethAddressToEmail[ethAddress] = email;
}
storeEmailSubscriberInDb(emailSubscribers[email]);
let subscribedEvents = Object.keys(events).filter(eventName => events[eventName]);
let message = 'You signed up to receive notifications from the FIN4Xplorer plattform via ' + email + '.'
+ '<br>You are subscribed to the these events: <i>';
for (let i = 0; i < subscribedEvents.length; i++) {
message += contractEvents[subscribedEvents[i]].title + ', ';
}
message = message.substring(0, message.length - 2) + '</i>.';
sendEmail(email, 'Subscription confirmed', message);
console.log('Subscribed ' + email + ' to notifications');
return message + '<br>A confirmation email has been sent to you.';
};
const checkEmailAuthkey = authKey => {
let email = authKeyToEmail[authKey];
if (email) { // also check if emailSubscribers[email]? It would have to be true too though, or not in particular cases?
return {
authKey: authKey,
email: email
}
}
return null;
};
const unsubscribeEmail = authKey => {
let email = authKeyToEmail[authKey];
if (email) {
delete authKeyToEmail[authKey];
let ethAddress = emailSubscribers[email].ethAddress;
if (ethAddress) {
delete ethAddressToEmail[ethAddress];
}
delete emailSubscribers[email];
removeEmailSubscriberFromDb(email);
console.log('Unsubscribed ' + email + ' from notifications');
return 'Sucessfully unsubscribed <i>' + email + '</i>';
}
return 'Failed to unsubscribe';
};
const sendEmail = (to, subject, message) => {
let unsubscribeFooter = 'You can unsubscribe from FIN4Xplorer notifications using <a href="' + config.THIS_URL
+ '/unsubscribe/?authKey=' + emailSubscribers[to].authKey + '">this link</a>.';
client.sendEmail({
to: to,
from: '[email protected]',
cc: '',
bcc: '',
subject: '[FIN4Xplorer Notification] ' + subject,
message: message + '<br><br>' + unsubscribeFooter,
altText: 'plain text'
}, (err, data, res) => {
if (err) {
console.log('Error sending email', err);
}
// console.log('Email sent', data);
});
};
// ------------------------ SERVE HTML ------------------------
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/unsubscribe', (req, res) => {
res.sendFile(__dirname + '/unsubscribe.html');
});
// ------------------------ START SERVER ------------------------
http.listen(port, () => {
console.log('listening on port: ', port);
});