Skip to content

Commit

Permalink
start and allunsub support
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Dec 23, 2018
1 parent 2604974 commit b6fa709
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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源
Expand Down
12 changes: 12 additions & 0 deletions controlers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 9 additions & 3 deletions i18n/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RSS URL>
UNSUB_USAGE: Usage: /unsub <RSS URL>
SUB_USAGE: USAGE: /sub <RSS URL>
UNSUB_USAGE: USAGE: /unsub <RSS URL>
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
Expand All @@ -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)
6 changes: 6 additions & 0 deletions i18n/zh-cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 38 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})();
Expand All @@ -30,45 +35,61 @@ 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,
RSS.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
);

Expand All @@ -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}: <a href="${feed.url}">${feed.url}</a> ${i18n['ERROR_MANY_TIME']}`,
Expand Down
11 changes: 11 additions & 0 deletions middlewares/index.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions proxies/rssFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b6fa709

Please sign in to comment.