-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
123 lines (110 loc) · 4.08 KB
/
bot.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
require("dotenv").config()
const {Telegraf} = require("telegraf")
const {Topics} = require("./topics")
const wait = hours => new Promise(resolve => setTimeout(resolve, hours * 60 * 60 * 1000))
const mongoose = require("mongoose")
const bot = new Telegraf(process.env.BOT_TOKEN)
const chatId = process.env.CHAT_ID
const dayjs = require("dayjs")
const Article = require("./models/Article")
const WebsiteService = require("./service/WebsiteService")
const NewsService = require("./service/NewsService")
const conf = require("./config");
const cron = require("node-cron");
const initDB = async () => {
try {
await mongoose.connect(process.env.MONGODB, {
useNewUrlParser: true,
useUnifiedTopology: true
})
console.log("Mongo connected...")
} catch (error) {
console.error("PROBLEMES DE CONEXIO")
console.error(error)
process.exit(0)
}
}
const notificationNews = async () => {
const websites = await WebsiteService.getAll()
for (let website of websites) {
let news_items = []
try {
console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> SCRAPPING: ${website.name}`)
news_items = await NewsService.parseUrl(website.url)
for (let item of news_items) {
try {
let url = item.link.trim().toLowerCase()
console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> ${url}`)
await Article.create({url})
await bot.telegram.sendMessage(chatId, url)
console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Enviat: ${item.link}`)
} catch (error) {
console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Exists: ${item.link}`)
console.log(error)
}
await wait(0.1)
}
} catch (error) {
console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : URL: ${website.name} ${error}`)
}
}
}
function hasArguments() {
return process.argv.length > 2;
}
const init = async () => {
await initDB();
if (hasArguments()) {
const args = process.argv.slice(2);
if (args[0] === '-h') {
console.log("TODO: HELP")
} else if (args[0] === '-add') {
const elementsToAdd = args.slice(1);
let index = 0;
while (index < elementsToAdd.length) {
let name = elementsToAdd[index++];
let url = elementsToAdd[index++];
if (name && url) {
name = name.trim();
url = url.trim();
await WebsiteService.addWebsite(name, url);
}
}
await mongoose.disconnect();
} else if (args[0] === '-rem') {
const elementsToRemove = args.slice(1);
for (let index = 0; index < elementsToRemove.length; index++) {
let name = elementsToRemove[index];
if (name) {
name = name.trim();
await WebsiteService.removeWebsite(name);
}
}
await mongoose.disconnect();
} else if (args[0] === '-list') {
const websites = await WebsiteService.getAll();
if (websites.length > 0) {
console.log("Registered websites:");
websites.map((website => {
console.log(`${website.name} - ${website.url}`)
}))
}
await mongoose.disconnect();
}
} else {
bot.start(ctx => ctx.reply("Bot iniciat!"))
Topics(bot)
bot
.launch()
.then(() => console.log(`TELEGRAM BOT: "${chatId}" running...`))
.catch(error => console.error(error))
process.once("SIGINT", () => bot.stop("SIGINT"))
process.once("SIGTERM", () => bot.stop("SIGTERM"))
for (let cronConf of conf.crons) {
cron.schedule(cronConf, function () {
notificationNews()
})
}
}
}
init();