diff --git a/README.md b/README.md index 28eca2fe1f6..a8c9a888339 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The same as [https://github.com/iovxw/rssbot/](https://github.com/iovxw/rssbot/) /sub - subscribe a RSS: /sub http://example.com/feed.xml /unsub - unsubscribe a RSS: /unsub http://example.com/feed.xml /unsubthis - reply a message from a RSS feed to unsubscribe it +/allunsub - unsubscribe all feeds /export - export subscriptions to opml file ``` @@ -87,6 +88,7 @@ RSS 解析用的是 [rss-parser](https://www.npmjs.com/package/rss-parser),它 /sub - 订阅 RSS: /sub http://example.com/feed.xml /unsub - 退订 RSS: /unsub http://example.com/feed.xml /unsubthis - 回复一个 RSS 发来的消息退订该 RSS +/allunsub - 退订所有源 /export - 导出订阅到opml文件 ``` 直接发送opml文件,可以导入RSS源 diff --git a/controlers/rss.js b/controlers/rss.js index cb79d046b59..4760466378c 100644 --- a/controlers/rss.js +++ b/controlers/rss.js @@ -67,4 +67,16 @@ ctrl.rss = async (ctx, next) => { await next(); }; +ctrl.unsubAll = async (ctx, next) => { + const userId = ctx.state.chat.id; + await RSS.unsubAll(userId); + ctx.telegram.deleteMessage(ctx.state.chat.id, ctx.state.processMesId); + ctx.telegram.sendMessage(ctx.state.chat.id, i18n['UNSUB_ALL_SUCCESS'], + { + parse_mode: 'HTML', + disable_web_page_preview: true + }); + await next(); +} + module.exports = ctrl; diff --git a/i18n/en.yaml b/i18n/en.yaml index e1be5466b64..1c9ad977284 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -5,10 +5,10 @@ FETCH_ERROR: Fetch error ALREADY_SUB: You have already subscribe this feed DB_ERROR: SQL error DID_NOT_SUB: You haven't subscribed this feed -SUB_USAGE: Usage: /sub -UNSUB_USAGE: Usage: /unsub +SUB_USAGE: USAGE: /sub +UNSUB_USAGE: USAGE: /unsub ADMIN_ONLY: Only enable for admins -UNSUBTHIS_USAGE: Usage:/unsubthis Reply the message from the feed you want to unsubscribe directly +UNSUBTHIS_USAGE: USAGE:/unsubthis Reply the message from the feed you want to unsubscribe directly SAME_NAME: There is a feed has the same title,Please use /unsub PROCESSING: Processing, please wait for a while SUB_SUCCESS: Subscribe successfully @@ -20,3 +20,9 @@ OPML_PARSE_ERRO: Unable to parse the opml NETWORK_ERROR: Network error IMPORT_SUCCESS: Import successfully FILESYSTEM_ERROR: Filesystem error +UNSUB_ALL_SUCCESS: Unubscribe all successfully +EXPORT: USAGE: USAGE: /export Export subscriptions to opml format +SEND_FILE_IMPORT: Send a opml file to import subscriptions +RSS_USAGE: USAGE: /rss Output subscriptions list add "raw" to show links +USB_ALL_USAGE: USAGE: /allunsub unsubscribe all feeds +WELCOME: Welcome to [NodeRSSBot](https://github.com/fengkx/NodeRSSBot) diff --git a/i18n/zh-cn.yaml b/i18n/zh-cn.yaml index b126b6aa996..cd90ff426cd 100644 --- a/i18n/zh-cn.yaml +++ b/i18n/zh-cn.yaml @@ -20,3 +20,9 @@ OPML_PARSE_ERRO: opml 文件解析失败 NETWORK_ERROR: 网络错误 IMPORT_SUCCESS: 成功导入 FILESYSTEM_ERROR: 读写文件发生错误 +UNSUB_ALL_SUCCESS: 成功退订所有订阅源 +EXPORT: 使用方法: /export 导出订阅源为 opml 格式 +SEND_FILE_IMPORT: 发送 opml 文件导入订阅源 +RSS_USAGE: 使用方法: /rss 输出订阅列表 加 raw 显示链接 +USB_ALL_USAGE: 使用方法: /allunsub 退订所有源 +WELCOME: 欢迎来到 [NodeRSSBot](https://github.com/fengkx/NodeRSSBot) diff --git a/index.js b/index.js index 0bff7cd8ed6..932dc8d3630 100644 --- a/index.js +++ b/index.js @@ -2,19 +2,24 @@ const Telegraf = require('telegraf'); const {token} = require('./config'); const agent = require('./utils/agent'); const initTable = require('./database/initTables'); -const getUrl = require('./middlewares/getUrl'); -const getUrlByTitle = require('./middlewares/getUrlByTitle'); -const testUrl = require('./middlewares/testUrl'); -const sendErro = require('./middlewares/sendError'); const RSS = require('./controlers/rss'); -const {isAdmin} = require('./middlewares/permission'); -const getFileLink = require('./middlewares/getFileLink'); -const importFromOpml = require('./middlewares/importFromOpml'); -const exportToopml = require('./middlewares/exportToOpml'); const {fork} = require('child_process'); const send = require('./utils/send'); const logger = require('./utils/logger'); const i18n = require('./i18n'); + +const { + getUrl, + exportToopml, + importFromOpml, + getFileLink, + sendError, + testUrl, + getUrlByTitle, + isAdmin +} = require('./middlewares'); + + (async () => { await initTable(); })(); @@ -30,17 +35,27 @@ bot.catch((err) => logger.error(err)); // for handling command form group bot.telegram.getMe().then((botInfo) => { bot.options.username = botInfo.username -}) +}); bot.on('document', - sendErro, + sendError, isAdmin, getFileLink, importFromOpml ); +bot.command('start', async (ctx) => { + let text = i18n['WELCOME']; + text += `\n${i18n['SUB_USAGE']}` + text += `\n${i18n['UNSUB_USAGE']}` + text += `\n${i18n['RSS_USAGE']}` + text += `\n${i18n['SEND_FILE_IMPORT']}` + text += `\n${i18n['EXPORT']}` + await ctx.replyWithMarkdown(text); +}); + bot.command('sub', - sendErro, + sendError, isAdmin, getUrl, testUrl, @@ -48,27 +63,33 @@ bot.command('sub', ); bot.command('unsub', - sendErro, + sendError, isAdmin, getUrl, RSS.unsub ); bot.command('unsubthis', - sendErro, + sendError, isAdmin, getUrlByTitle, RSS.unsub ); +bot.command('allunsub', + sendError, + isAdmin, + RSS.unsubAll +); + bot.command('rss', - sendErro, + sendError, isAdmin, RSS.rss ); bot.command('export', - sendErro, + sendError, exportToopml ); @@ -78,13 +99,13 @@ const chid = fork(`utils/fetch.js`); chid.on('message', function (message) { if (typeof message === "string") logger.info(message); - else if(message.success) { + else if (message.success) { const feed = message.eachFeed; const {sendItems} = message; if (sendItems.length > 0) send(bot, sendItems, feed) } else { - if(message.message === 'MAX_TIME') { + if (message.message === 'MAX_TIME') { const {feed} = message; send(bot, `${feed.feed_title}: ${feed.url} ${i18n['ERROR_MANY_TIME']}`, diff --git a/middlewares/index.js b/middlewares/index.js index e69de29bb2d..8dd7f0b518f 100644 --- a/middlewares/index.js +++ b/middlewares/index.js @@ -0,0 +1,11 @@ +const fs = require('fs'); + +const middlewares = {}; +const files = fs.readdirSync(__dirname); +const jsFiles = files.filter(f=>{return f.endsWith('.js') && !f.startsWith('index')}); +jsFiles.forEach(file => { + const fileName = file.substring(0, file.length-3); + middlewares[fileName] = require((__dirname + '/' + file)); +}) + +module.exports = middlewares; diff --git a/proxies/rssFeed.js b/proxies/rssFeed.js index 56262131806..49686e7c025 100644 --- a/proxies/rssFeed.js +++ b/proxies/rssFeed.js @@ -114,4 +114,14 @@ px.failAttempt = async (feedUrl) => { } } +px.unsubAll = async (userId) => { + try { + const db = await dbPomise; + await db.run(`DELETE FROM subscribes WHERE user_id=?`, userId); + return 'ok'; + } catch (e) { + throw new Error('DB_ERROR'); + } +} + module.exports = px;