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){ return parser.parseURL(url).then(feed=>feed.items).then(items=>{ let idx = items.findIndex(i => parseInt(i.guid) === min_id); if(idx>=0) return items.slice(0,idx); else return items; }); } 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 const rssi_ex = [ { 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 : [ { 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)=>{ return Promise.all(this.rssi.map(entry=> db.get_v(entry.chat).then(v=> get_rss(entry.url,v).then(articles=> Promise.all(articles.map(art=> this.bot.telegram.sendMessage(entry.chat,article_to_mess(art),{"parse_mode":"Markdown"}) )).then(rr=> (articles.length == 0) || db.set_v(entry.chat,parseInt(articles.pop().guid)) ) ) ) )).then(r=>event.reply("Refreshed all Feeds")); } } ], 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 ()=>{ return Promise.all(this.rssi.map(entry=> db.get_v(entry.chat).then(v=> get_rss(entry.url,v).then(articles=> Promise.all(articles.map(art=> this.bot.telegram.sendMessage(entry.chat,article_to_mess(art),{"parse_mode":"Markdown"}) )).then(rr=> (articles.length == 0) || db.set_v(entry.chat,parseInt(articles.pop().guid)) ) ) ) )); }; } } ] } }