const TelegramBot = require('node-telegram-bot-api'); // replace the value below with the Telegram token you receive from @BotFather let token = require("./credentials.json").token; // Meme creator library const memeLib = require('nodejs-meme-generator'); const memeGenerator = new memeLib({ canvasOptions: { // optional canvasWidth: 500, canvasHeight: 500 }, fontOptions: { // optional fontSize: 46, fontFamily: 'impact', lineHeight: 2 } }); //Required options for the meme creator. // memeMaker(options, function(err) { // if(e) throw new Error(err) // console.log('Image saved: ' + options.outfile) // }); // Create a bot that uses 'polling' to fetch new updates const bot = new TelegramBot(token, {polling: true,}); const {createUser, getUser, userFinishedTheirThesis, reviewDateUpdate} = require("./database"); bot.onText(/\/cuandoLaTesis/, (msg, match) => { // 'msg' is the received Message from Telegram // 'match' is the result of executing the regexp above on the text content // of the message console.log(msg.from.id); console.log(msg.chat.id); }); bot.onText(/\/cuandoLaRevision/, (msg, match) => { // 'msg' is the received Message from Telegram // 'match' is the result of executing the regexp above on the text content // of the message // Save chat Id const chatId = msg.chat.id; const userId = msg.from.id; getUser(userId).then(res => { // We need to create a new user if(res.length === 0) { createUser(userId, msg.from.username).then(res => { bot.sendMessage(chatId, "Termina la tesis primero 👀.").then(() => {}); }).catch(err => console.log(err)); } else { let date = new Date(res[0].rt_date); let now = new Date(); date = new Date(date.setMonth(date.getMonth()+1)); if(date.getMonth() === 1) { date = new Date(date.setMonth(date.getMonth()+1)); } if (!res[0].ft_date) { bot.sendMessage(chatId, "Pa que preguntai weas si no hay terminado la tesis.").then(() => {}); } else { if (!res[0].rt_date) { bot.sendMessage(chatId, "Entrega el borrador, como tan pajero.").then(() => {}); } else { if (date.getTime() > now.getTime()) { const diffTime = Math.abs(date - now); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); bot.sendMessage(chatId, "Wait prrito todavía te faltan " + diffDays + " días.").then(() => {}); } else { const diffTime = Math.abs(now - date); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); const diffHrs = Math.floor((diffTime % 86400000) / 3600000); // hours const diffMinutes = Math.round(((diffTime % 86400000) % 3600000) / 60000); // minutes memeGenerator.generateMeme({ // you can use either topText or bottomText // or both of them at the same time topText: diffDays + " DÍAS " + ", " + diffHrs + " HORAS, " + parseInt(Math.floor(Math.random() * 90) + 2) + " GRADOS, INCLINACIÓN " + diffMinutes + " MINUTOS", bottomText: 'NO HAY REVISIÓN', url: 'https://i.imgflip.com/346hvl.png' }).then((data) => { bot.sendPhoto(chatId, data).then(() => {}); }); } } } } }).catch(err => { console.log(err);}); }); bot.onText(/\/cuandoElBorrador/, (msg, match) => { // 'msg' is the received Message from Telegram // 'match' is the result of executing the regexp above on the text content // of the message // Save chat Id const chatId = msg.chat.id; const userId = msg.from.id; getUser(userId).then(res => { // We need to create a new user if(res.length === 0) { createUser(userId, msg.from.username).then(res => { bot.sendMessage(chatId, "Termina la tesis primero 👀.").then(() => {}); }).catch(err => console.log(err)); } else { let date = new Date(res[0].ft_date); if (res[0].ft_date) { let now = new Date(); date = new Date(date.setMonth(date.getMonth()+1)); if (date.getTime() > now.getTime()) { const diffTime = Math.abs(date - now); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); bot.sendMessage(chatId, "Apurate m3n te quedan " + diffDays + " días.").then(() => {}); } else { bot.sendMessage(chatId, "GG m3n anda pedir más plazo.").then(() => {}); } } else { bot.sendMessage(chatId, "Termina la tesis primero 👀.").then(() => {}); } } }).catch(err => { console.log(err);}); }); bot.onText(/\/entregueElBorrador/, (msg, match) => { // 'msg' is the received Message from Telegram // 'match' is the result of executing the regexp above on the text content // of the message // Get message parameters. const array = match.input.split(' '); const chatId = msg.chat.id; const userId = msg.from.id; let date = new Date().toISOString().substr(0,10); // If the user enters a finish date then it will use it. if(array.length > 1) { // Parse given date. let possibleDate = Date.parse(array[1]); // If the given date is valid, then is stored as the finished date. if(!isNaN(possibleDate)) { date = new Date(possibleDate).toISOString().substr(0,10); } } getUser(userId).then(res => { // We need to create a new user if(res.length === 0) { bot.sendMessage(chatId, "Termina la tesis primero po weon >=(.").then(() => {}); } else { reviewDateUpdate(userId, date).then(() => { bot.sendMessage(chatId, "Buena prro entregaste el borrador ahora a esperar :) /Congratulations").then(() => {}); }).catch(err => console.log(err)); } }).catch(err => { console.log(err);}); }); bot.onText(/\/termineLaTesisCTM/, (msg, match) => { // 'msg' is the received Message from Telegram // 'match' is the result of executing the regexp above on the text content // of the message // Get message parameters. const array = match.input.split(' '); // Save chat Id const chatId = msg.chat.id; // Default finish thesis date is now. let date = new Date().toISOString().substr(0,10); // If the user enters a finish date then it will use it. if(array.length > 1) { // Parse given date. let possibleDate = Date.parse(array[1]); // If the given date is valid, then is stored as the finished date. if(!isNaN(possibleDate)) { date = new Date(possibleDate).toISOString().substr(0,10); } } const userId = msg.from.id; getUser(userId).then(res => { // We need to create a new user if(res.length === 0) { createUser(userId, msg.from.username).then(res => { userFinishedTheirThesis(userId, date).then(() => { bot.sendMessage(chatId, "Terminaste la tesis =D /Congratulations").then(() => {}); }).catch(err => console.log(err)); }).catch(err => console.log(err)); } else { userFinishedTheirThesis(userId, date).then(() => { bot.sendMessage(chatId, "Terminaste la tesis =D /Congratulations").then(() => {}); }).catch(err => console.log(err)); } }).catch(err => { console.log(err);}); }); bot.on("polling_error", (err) => console.log(err));