-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
118 lines (106 loc) · 3.69 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
const fs = require('fs');
const path = require('path');
const nodemailer = require('nodemailer');
const ejs = require('ejs');
const TelegramBot = require('node-telegram-bot-api');
const emailRegex = /\S+@\S+\.\S+/;
const codeRegex = /^[0-9]{6}$/;
//This is what must be changed
const token = 'YOUR_BOT_TOKENS';
const email = 'YOUR_GMAIL';
const password = 'YOUR_PASSWORD_GMAIL';
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: email,
pass: password
}
});
let state = 'start';
let verifiedEmails = {};
const ruserFile = path.join(__dirname, 'ruser.json');
try {
const rawData = fs.readFileSync(ruserFile);
verifiedEmails = JSON.parse(rawData);
} catch (err) {
if (err.code === 'ENOENT') {
fs.writeFileSync(ruserFile, JSON.stringify({}));
} else {
console.error(err);
}
}
if (Object.keys(verifiedEmails).length === 0) {
console.log('ruser.json is empty');
}
const bot = new TelegramBot(token, { polling: true });
bot.on('message', (msg) => {
const chatId = msg.chat.id;
const message = msg.text.trim();
const verifiedEmail = Object.keys(verifiedEmails).find(email => verifiedEmails[email].chatId === chatId);
if (verifiedEmail) {
bot.sendMessage(chatId, 'Anda sudah terverifikasi. Terima kasih!');
return;
}
switch (state) {
case 'start':
if (!verifiedEmails) {
verifiedEmails = {};
}
bot.sendMessage(chatId, 'Silakan verifikasi email terlebih dahulu dengan menempelkan alamat email:');
state = 'email';
break;
case 'email':
if (emailRegex.test(message)) {
if (verifiedEmails[message]) {
bot.sendMessage(chatId, 'Maaf, email yang Anda masukkan telah digunakan. Mohon gunakan email lain.');
state = 'start';
break;
}
const code = Math.floor(Math.random() * 900000) + 100000;
ejs.renderFile(path.join(__dirname, '/views/template.ejs'), { code }, (err, html) => {
if (err) {
console.log(err);
} else {
const mailOptions = {
from: email,
to: message,
subject: 'Ayaka confirmation ><',
html: html
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
verifiedEmails[message] = { code: code, chatId: chatId };
fs.writeFileSync(ruserFile, JSON.stringify(verifiedEmails));
bot.sendMessage(chatId, `Kode verifikasi telah dikirim ke ${message}. Silakan masukkan kode verifikasi:`);
state = 'code';
}
});
} else {
bot.sendMessage(chatId, 'Alamat email tidak valid. Silakan coba lagi.');
}
break;
case 'code':
const verifiedEmail = Object.keys(verifiedEmails).find(email => verifiedEmails[email].chatId === chatId);
if (!verifiedEmail) {
bot.sendMessage(chatId, 'Anda belum memasukkan email. Silakan masukkan email terlebih dahulu.');
state = 'start';
break;
}
if (codeRegex.test(message) && verifiedEmails[verifiedEmail].code === Number(message)) {
bot.sendMessage(chatId, 'Email Anda berhasil diverifikasi. Terima kasih!');
state = 'start';
} else {
bot.sendMessage(chatId, 'Kode konfirmasi tidak valid. Silakan coba lagi.');
}
break;
default:
bot.sendMessage(chatId, 'Terjadi kesalahan. Silakan ulangi lagi.');
state = 'start';
break;
}
});