bot complete

This commit is contained in:
Joaquín Torres 2020-03-15 00:12:16 -03:00
parent ea13ddcf6b
commit 711f4dfdc6
6 changed files with 233 additions and 54 deletions

3
.gitignore vendored
View File

@ -110,4 +110,5 @@ dist
credentials.json credentials.json
.idea .idea
package-lock.json package-lock.json
database.js

View File

@ -1,2 +1,41 @@
# CuandoLaTesisBot # 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
```

239
bot.js
View File

@ -4,30 +4,22 @@ const TelegramBot = require('node-telegram-bot-api');
let token = require("./credentials.json").token; let token = require("./credentials.json").token;
// Meme creator library // 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. //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) { // memeMaker(options, function(err) {
// if(e) throw new Error(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 // Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, const bot = new TelegramBot(token, {polling: true,});
{
polling: true,
});
// Matches "/echo [whatever]" const {createUser, getUser, userFinishedTheirThesis, reviewDateUpdate} = require("./database");
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"));
}
});
bot.onText(/\/cuandoLaTesis/, (msg, match) => { bot.onText(/\/cuandoLaTesis/, (msg, match) => {
// 'msg' is the received Message from Telegram // 'msg' is the received Message from Telegram
@ -76,4 +41,178 @@ bot.onText(/\/cuandoLaTesis/, (msg, match) => {
console.log(msg.chat.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)) - 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)); bot.on("polling_error", (err) => console.log(err));

BIN
impact.ttf Normal file

Binary file not shown.

BIN
meme.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@ -3,10 +3,10 @@
"private": true, "private": true,
"scripts": {}, "scripts": {},
"dependencies": { "dependencies": {
"meme-maker": "^2.1.2", "canvas": "^2.6.1",
"node-telegram-bot-api": "^0.30.0", "node-telegram-bot-api": "^0.30.0",
"pg": "^7.18.2", "nodejs-meme-generator": "^1.0.4",
"socks5-https-client": "latest" "pg": "^7.18.2"
}, },
"devDependencies": {} "devDependencies": {}
} }