-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (100 loc) · 3.76 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
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
require("dotenv").config();
const { Telegraf } = require("telegraf");
const Parser = require("rss-parser");
const bot = new Telegraf(process.env.BOT_TOKEN);
const parser = new Parser();
const channelId = process.env.CHANNEL_ID;
const rssFeedUrl = process.env.RSS_FEED_URL;
const intervalDuration = process.env.INTERVAL_DURATION;
const fs = require("fs");
const path = require("path");
const LAST_POST_DATE_FILE = path.join(__dirname, "lastPostDate.json");
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Load the last posted date from the file
function loadLastPostedDate() {
try {
const data = fs.readFileSync(LAST_POST_DATE_FILE, "utf8");
return new Date(JSON.parse(data));
} catch (error) {
// If the file doesn't exist or contains invalid data, use the Unix epoch
return new Date(0);
}
}
// Save the last posted date to the file
function saveLastPostedDate(date) {
fs.writeFileSync(LAST_POST_DATE_FILE, JSON.stringify(date.toISOString()));
}
async function postNewRssItems() {
const lastPostedDate = loadLastPostedDate();
try {
const feed = await parser.parseURL(rssFeedUrl);
for (const item of feed.items) {
const itemDate = new Date(item.pubDate || item.date);
if (itemDate <= lastPostedDate) {
continue; // Skip items that were published before the last posted date
}
saveLastPostedDate(itemDate);
// To avoid hitting rate limits or flooding the channel 10 seconds delay is added
await delay(10000);
// Extract title (handling CDATA)
const title = item.title.replace("<![CDATA[", "").replace("]]>", "");
// Extract description (handling CDATA and limiting to a preview)
let description = item.contentSnippet || item.description || "";
description = description.replace("<![CDATA[", "").replace("]]>", "");
const descriptionPreview =
description.length > 200
? description.slice(0, 200) + "..."
: description;
// Construct the Telegram message (adjust formatting as needed)
const message = `
<b>${title}</b>
${descriptionPreview}
<a href="${item.link}">Read more</a>
`;
await bot.telegram.sendMessage(channelId, message, {
parse_mode: "HTML",
});
}
} catch (error) {
console.error("Error fetching or posting RSS items:", error);
}
}
bot.command("latest", async () => {
try {
const feed = await parser.parseURL(rssFeedUrl);
const item = feed.items[0]; // Get the latest item
// Extract title (handling CDATA)
const title = item.title.replace("<![CDATA[", "").replace("]]>", "");
// Extract description (handling CDATA and limiting to a preview)
let description = item.contentSnippet || item.description || "";
description = description.replace("<![CDATA[", "").replace("]]>", "");
const descriptionPreview =
description.length > 200
? description.slice(0, 200) + "..."
: description;
// Construct the Telegram message (adjust formatting as needed)
const message = `
<b>${title}</b>
${descriptionPreview}
<a href="${item.link}">Read more</a>
`;
await bot.telegram.sendMessage(channelId, message, {
parse_mode: "HTML",
});
} catch (error) {
console.error("Error fetching or posting RSS items:", error);
}
});
console.log("About to initialize bot");
bot.launch();
console.log("Bot initialized");
setInterval(() => {
postNewRssItems()
.then(() => console.log("New RSS items posted"))
.catch((error) => {
console.error("Error posting RSS items:", error.message); // Log the error message
console.error(error.stack); // Log the error stack trace
});
}, intervalDuration);