telebot-rssfeed/config.js

111 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-07-02 20:04:32 +02:00
const fs = require('fs');
const Parser = require('rss-parser');
const parser = new Parser();
const Markup = require('telegraf/markup');
const Extra = require('telegraf/extra');
const db_js = require('./db.js');
var db = new db_js("rss_chan");
//=== TOOL FUNCTIONS
const get_rss = function(url,min_id){
2020-07-03 11:21:10 +02:00
return parser.parseURL(url).then(feed=>feed.items).then(items=>{
let idx = items.findIndex(i => i.prop_1 === min_id);
if(idx>=0) return items.slice(0,idx);
else return items;
2020-07-03 11:19:19 +02:00
});
2020-07-02 20:04:32 +02:00
}
const article_to_mess = function(art){
return `*${art.title.replace('\r\n','').trim()}*\n\n${art.content}\n\n[Read More](${art.link})`;
}
//=== MAIN MODULE CONFIG
2020-07-02 20:32:54 +02:00
const rssi_ex = [
2020-07-02 20:04:32 +02:00
{
chat:'CHAT_ID',
url: 'RSS_URL',
}
];
module.exports = function(rssi){
this.rssi = rssi;
return {
name : "RSS-FEED",
key : "rssf",
version : 0.1,
requirements : {
gps : false,
},
text : [
2020-07-02 20:32:54 +02:00
{
trigger : ["/refresh"],
public : false,
desc : {
0: "Force refresh the feeds",
},
requirements : (bot,event,message)=>{
return new Promise((resolve, reject)=>{
if(event.from && bot.admin.indexOf(event.from.id) == -1)
return reject(403);
return resolve(200);
});
},
action : (bot,event,message)=>{
2020-07-02 20:58:17 +02:00
return Promise.all(this.rssi.map(entry=>
db.get_v(entry.chat).then(v=>
get_rss(entry.url,v).then(articles=>
2020-07-03 11:19:19 +02:00
Promise.all(articles.map(art=>
this.bot.telegram.sendMessage(entry.chat,article_to_mess(art),{"parse_mode":"Markdown"})
)).then(rr=>
db.set_v(entry.chat,parseInt(articles.pop().guid))
)
2020-07-02 20:58:17 +02:00
)
)
)).then(r=>event.reply("Refreshed all Feeds"));
2020-07-02 20:32:54 +02:00
}
}
2020-07-02 20:04:32 +02:00
],
reply : [],
regex : [],
media : [],
callback : [],
inline : [],
new_member : (bot,event)=>{},
weburl : [],
cron : [
{
cstr : '0 0,30 */1 * * *',
public : true,
desc: {
0: "Informs Group using RSS feed",
1: "Informe le Groupe avec le feed RSS",
4: "Informiert die Gruppe durch dem RSS feed",
},
params : [ null, true],
timezone : 'Europe/Zurich',
action : (bot)=> {
this.bot = bot;
return ()=>{
2020-07-02 20:58:17 +02:00
return Promise.all(this.rssi.map(entry=>
2020-07-02 20:04:32 +02:00
db.get_v(entry.chat).then(v=>
get_rss(entry.url,v).then(articles=>
2020-07-03 11:19:19 +02:00
Promise.all(articles.map(art=>
this.bot.telegram.sendMessage(entry.chat,article_to_mess(art),{"parse_mode":"Markdown"})
)).then(rr=>
db.set_v(entry.chat,parseInt(articles.pop().guid))
)
2020-07-02 20:04:32 +02:00
)
)
2020-07-02 20:58:17 +02:00
));
2020-07-02 20:04:32 +02:00
};
}
}
]
}
}