From 533bd23f6cfd620ba762d2c15b7b6b96f7465939 Mon Sep 17 00:00:00 2001 From: Karl0ss Date: Thu, 8 Sep 2022 09:35:59 +0100 Subject: [PATCH] inital work on telegram notifications --- FeedFilter.js | 4 ++++ JDRssDownloader.js | 13 +++++++++++++ config-sample.json | 2 ++ package.json | 1 + telegramCommunication.js | 20 ++++++++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 telegramCommunication.js diff --git a/FeedFilter.js b/FeedFilter.js index 2675ec0..ce6bf61 100644 --- a/FeedFilter.js +++ b/FeedFilter.js @@ -3,6 +3,7 @@ const { linkAdder } = require('./JDLinkAdder'); const { getLinksFromURL } = require('./LinkGrabber') const { checkFileName } = require('./checkFileName') const { checkDownloadHistory } = require('./checkDownloadHistory') +const { telegrambot } = require('./telegramCommunication') async function filterFeed() { let myshowlist = JSON.parse(fs.readFileSync('config.json')).Shows @@ -53,6 +54,9 @@ async function filterFeed() { break } else { log.info(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') + if (TelegramBotConfig) { + telegrambot(download_list.length + ' links for ' + urlObj.fileName + ' have been sent to JDdownloader.') + } linkAdder(download_list) } } else { diff --git a/JDRssDownloader.js b/JDRssDownloader.js index 1d7766b..5f180e9 100644 --- a/JDRssDownloader.js +++ b/JDRssDownloader.js @@ -1,8 +1,11 @@ const fs = require("fs"); const { feedUpdater } = require('./FeedUpdater') const { filterFeed } = require('./FeedFilter') +const { telegrambot } = require('./telegramCommunication') const version = require('./package.json').version; +global.TelegramBotConfig = JSON.parse(fs.readFileSync('config.json')).TelegramBot + global.log = require('simple-node-logger').createSimpleLogger({ logFilePath: 'jdrssdownloader.log', timestampFormat: 'YYYY-MM-DD HH:mm:ss.SSS' @@ -10,6 +13,10 @@ global.log = require('simple-node-logger').createSimpleLogger({ async function main() { log.info('Running JDRssDownloader version ' + version) + + if (TelegramBotConfig) { + telegrambot('Running JDRssDownloader version ' + version) + } try { RSSFeedRefreshMins = JSON.parse(fs.readFileSync('config.json')).RSSFeedRefreshMins JDPostLinksMins = JSON.parse(fs.readFileSync('config.json')).JDPostLinksMins @@ -17,7 +24,13 @@ async function main() { log.error('config.json file is missing.') } log.info('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') + if (TelegramBotConfig) { + telegrambot('Refreshing RSS Items every ' + RSSFeedRefreshMins + ' Minutes') + } log.info('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes') + if (TelegramBotConfig) { + telegrambot('Checking for links and sending to JDdownloader every ' + JDPostLinksMins + ' Minutes') + } setInterval(await feedUpdater, RSSFeedRefreshMins * 60000); setInterval(await filterFeed, JDPostLinksMins * 60000); diff --git a/config-sample.json b/config-sample.json index 170f592..aeef84b 100644 --- a/config-sample.json +++ b/config-sample.json @@ -6,6 +6,8 @@ "JDPostLinksMins": 180, "Autostart": false, "OnlyHEVC": true, + "TelegramBotID":"", + "TelegramChatID":123456789, "Shows": [ { "Name": "", diff --git a/package.json b/package.json index 5e4bd81..3e1985c 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "cheerio": "^1.0.0-rc.11", "jdownloader-client": "^1.0.0", "lodash": "^4.17.21", + "node-telegram-bot-api": "^0.59.0", "rss-parser": "^3.12.0", "simple-node-logger": "^21.8.12" }, diff --git a/telegramCommunication.js b/telegramCommunication.js new file mode 100644 index 0000000..1824bcd --- /dev/null +++ b/telegramCommunication.js @@ -0,0 +1,20 @@ +const fs = require("fs"); +const TelegramBot = require('node-telegram-bot-api'); +const token = JSON.parse(fs.readFileSync('config.json')).TelegramBotID; +const chatId = JSON.parse(fs.readFileSync('config.json')).TelegramChatID; + +const bot = new TelegramBot(token, { polling: false }); + +const telegrambot = (message) => { + try { + bot.sendMessage(chatId, message, { + parse_mode: 'html' + }); + } catch (err) { + console.log('Something went wrong when trying to send a Telegram notification', err); + } +} + +module.exports = { + telegrambot +} \ No newline at end of file