diff --git a/bot.js b/bot.js index 9dcf0e9..27228e0 100644 --- a/bot.js +++ b/bot.js @@ -1,5 +1,5 @@ module.exports = { - start: function(db, bot) { + start: function(db, bot, config) { var HashMap = require('hashmap'); // Strings diff --git a/index.js b/index.js index 7afdd17..e78898a 100644 --- a/index.js +++ b/index.js @@ -4,15 +4,22 @@ var request = require('request'), zlib = require('zlib'), schedule = require('node-schedule'), sqlite3 = require('sqlite3').verbose(), - bot = require('./bot.js') + botUI = require('./bot.js'), + config = require('./config.js'); +const TelegramBot = require('node-telegram-bot-api'); var db = new sqlite3.Database('db.sqlite'); +// Telegram bot. Polling to fetch new messages +const bot = new TelegramBot(config.token, { + polling: true +}); function getFeeds() { // Get all unique active feeds to retrieve gf_Query = 'SELECT DISTINCT FeedURL from QUERIES where Active =\'1\''; db.all(gf_Query, function(error, rows) { + if (rows.length == 0) console.log("No feeds") // For each unique feed retrieved... rows.forEach(function(row) { @@ -25,7 +32,7 @@ function getFeeds() { }); } -function match(title, queryKeywords, chatId) { +function match(post, queryKeywords, chatId) { // Parse current query element into an Object var queryKeywords = JSON.parse(queryKeywords); @@ -33,16 +40,20 @@ function match(title, queryKeywords, chatId) { // Declare the match valid in advance var matchStillValid = true; + var title = post["rss:title"]["#"]; + // For each keyword of current query test the match with Feed Title for (var keyword in queryKeywords) { - // Build a new regular expression with current keyword to pass it "i" flag for case-insensitive check + // Build a new regular expression with current keyword to pass it + // "i" flag for case-insensitive check var re = new RegExp(queryKeywords[keyword], 'i'); // Test the match for current keyword var match = title.match(re) ? true : false; - // If even a single keyword does not pass the test make the entire query useless + // If even a single keyword does not pass the test make the entire + // query useless if (match == false) { matchStillValid = false; } @@ -51,9 +62,13 @@ function match(title, queryKeywords, chatId) { // If every keyword in current query passed the test we have our match! // Time to start with notifications! - if (matchStillValid) - { - //do stuff... + if (matchStillValid) { + // TODO: prevalid Feed-relative hardcoded values and avoid composing + // the notification message with invalid ones + console.log("matched") + var description = post["rss:description"]["#"]; + var link = post["rss:link"]["#"] + bot.sendMessage(chatId, "New match! \n"+ title + "\n" + description + "\n" + link, { parse_mode: "HTML" }) } @@ -67,6 +82,7 @@ function fetch(url) { timeout: 10000, pool: false }); + console.log("Fetching "+url) req.setMaxListeners(50); // Some feeds do not respond without user-agent and accept headers. req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'); req.setHeader('accept', 'text/html,application/xhtml+xml'); @@ -90,19 +106,16 @@ function fetch(url) { // Select every active query (and its owner) for current feed... var rq_Query = 'SELECT Owner, Keywords AS keywordGroup FROM QUERIES where FeedURL = \'' + url + '\' AND Active = \'1\''; db.all(rq_Query, function(error, rows) { - feedparser.on('readable', function() { var post; while (post = this.read()) { - var title = post["rss:title"]["#"]; - // ...and for every results... rows.forEach(function(row) { // ...look for match - match(title, row.keywordGroup, row.Owner); + match(post, row.keywordGroup, row.Owner); }); @@ -164,12 +177,10 @@ function done(err) { //process.exit(); } -/* -var job = schedule.scheduleJob('0 * * * * *', function(){ - readSettings(); - fetch(FeedURL); -});*/ - // Do things -bot.start(db) -getFeeds(); \ No newline at end of file +// Bot up and running +botUI.start(db, bot, config) +// Run the entire thing every 5 seconds +var job = schedule.scheduleJob('*/5 * * * * *', function(){ + getFeeds(); +}); \ No newline at end of file diff --git a/package.json b/package.json index 0cbd6f2..b7e0d54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rssnotifier", - "version": "0.0.1", + "version": "0.1", "description": "A resident RSS scanner looking for matching entries", "main": "index.js", "scripts": {