-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (76 loc) · 2.15 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
require('dotenv').config();
const { Telegraf } = require('telegraf');
const Parser = require('rss-parser');
const { html } = require('common-tags');
const striptags = require('striptags');
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.start(startWatchFeed);
bot.launch();
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
/**
* Watches the RSS feed and sends Telegram message for each item.
*
* @param {object} ctx Telegraf library context
*/
async function startWatchFeed(ctx) {
try {
const feed = await new Parser().parseURL(process.env.FEED_URL);
const itemsToPublish = getItemsToPublish(feed.items);
itemsToPublish.forEach((item) =>
ctx.telegram.sendMessage(process.env.CHAT_ID, buildMessage(item), {
parse_mode: 'HTML',
})
);
} catch (e) {
console.error(e);
ctx.reply('Не удалось загрузить ленту');
}
setTimeout(startWatchFeed, process.env.UPDATE_PERIOD_MILLISECONDS, ctx);
}
/**
* Returns filtered items to publish by pubDate and update period
*
* @param {array} items RSS feed items
* @param {array} itemsToPublish filtered items to publish
*
* @returns {array} items
*/
function getItemsToPublish([firstItem, ...items], itemsToPublish = []) {
const isBreak = new Date(firstItem.pubDate) < dateSinceLastUpdate();
if (isBreak) {
return itemsToPublish;
} else {
return getItemsToPublish(items, [firstItem, ...itemsToPublish]);
}
}
/**
* Returns the date since last update period
*
* @returns {Date} date
*/
function dateSinceLastUpdate() {
const date = new Date();
date.setTime(date.getTime() - process.env.UPDATE_PERIOD_MILLISECONDS);
return date;
}
/**
* Builds Telegram message
*
* @param {object} item of RSS feed
*/
function buildMessage({link, title, content}) {
return html`
<b>Мероприятие</b>
<a href='${link}'>${title}</a>
${stripHtmlTags(content.trim())}
`;
}
/**
* Keeps only Telegram supported tags and strips unsupported tags
*
* @param {string} html
*/
function stripHtmlTags(html) {
return striptags(html, ['b', 'strong', 'i', 's', 'u', 'a']);
}