diff --git a/.gitignore b/.gitignore index ee73f1b..2bccfbc 100644 --- a/.gitignore +++ b/.gitignore @@ -110,4 +110,5 @@ dist credentials.json .idea -package-lock.json \ No newline at end of file +package-lock.json +database.js \ No newline at end of file diff --git a/README.md b/README.md index 0bac374..6a864de 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,41 @@ # CuandoLaTesisBot +## Requirements + +``` +npm install +``` + +Remember to create the credentials file + +```json + { + "token": "", + "postgres": { + "user": "", + "host": "", + "database": "when-the-thesis", + "password": "", + "port": 5432 + } + } +``` + +Also you need to create a the when-the-thesis database and also the table with: + +``` +CREATE TABLE USERS( + ID SERIAL PRIMARY KEY, + USER_ID INT NOT NULL, + USER_NAME TEXT, + FT_DATE TEXT, + RT_DATE TEXT, + DT_DATE TEXT +); +``` + +### Deployment + +``` +node bot.js +``` \ No newline at end of file diff --git a/bot.js b/bot.js index 03b42aa..1035c6f 100644 --- a/bot.js +++ b/bot.js @@ -4,30 +4,22 @@ const TelegramBot = require('node-telegram-bot-api'); let token = require("./credentials.json").token; // Meme creator library -let memeMaker = require('meme-maker'); +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. -let options = { - image: 'spiderman.png', // Required - outfile: 'spiderman-meme.png', // Required - topText: 'TODAY IM', // Required - bottomText: 'AN ASS', // Optional - font: '/path/to/font.ttf', // Optional - fontSize: 50, // Optional - fontFill: '#FFF', // Optional - textPos: 'center', // Optional - strokeColor: '#000', // Optional - strokeWeight: 2 // Optional -}; -// Postgres -const Pool = require('pg').Pool; - -// Postgres config. -let postgresConfig = require("./credentials.json").postgres; - -// Postgres initialization -const pool = new Pool(postgresConfig); // memeMaker(options, function(err) { // if(e) throw new Error(err) @@ -35,36 +27,9 @@ const pool = new Pool(postgresConfig); // }); // Create a bot that uses 'polling' to fetch new updates -const bot = new TelegramBot(token, - { - polling: true, - }); +const bot = new TelegramBot(token, {polling: true,}); -// Matches "/echo [whatever]" -bot.onText(/\/cuantofaltaprro/, (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 - - const chatId = msg.chat.id; - const date1 = new Date(2019, 9, 26, 21, 30, 0 ); - const dateNow = new Date(); - const msec = date1 - dateNow; - let mins = Math.floor(msec / 60000); - let hrs = Math.floor(mins / 60); - const days = Math.floor(hrs / 24); - - mins = mins - hrs*60; - hrs = hrs - days*24; - //const resp = match[1]; // the captured "whatever" - - // send back the matched "whatever" to the chat - if(date1 < dateNow) { - bot.sendMessage(chatId, "Viejito prro ya estai en japón."); - } else { - bot.sendMessage(chatId, "Quedan: " + days + (days === 1 ? " día, " : " días, ") + hrs + (hrs === 1 ? " hora, " : " horas, ") + mins + (mins === 1 ? " minuto" : " minutos")); - } -}); +const {createUser, getUser, userFinishedTheirThesis, reviewDateUpdate} = require("./database"); bot.onText(/\/cuandoLaTesis/, (msg, match) => { // 'msg' is the received Message from Telegram @@ -76,4 +41,178 @@ bot.onText(/\/cuandoLaTesis/, (msg, match) => { 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)) - 1; + 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)) - 1; + 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)); \ No newline at end of file diff --git a/impact.ttf b/impact.ttf new file mode 100644 index 0000000..114e6c1 Binary files /dev/null and b/impact.ttf differ diff --git a/meme.jpg b/meme.jpg new file mode 100644 index 0000000..40b081f Binary files /dev/null and b/meme.jpg differ diff --git a/package.json b/package.json index 163df19..62c8d11 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,10 @@ "private": true, "scripts": {}, "dependencies": { - "meme-maker": "^2.1.2", + "canvas": "^2.6.1", "node-telegram-bot-api": "^0.30.0", - "pg": "^7.18.2", - "socks5-https-client": "latest" + "nodejs-meme-generator": "^1.0.4", + "pg": "^7.18.2" }, "devDependencies": {} }