Skip to content

Commit

Permalink
feat: channel import support
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Apr 5, 2019
1 parent f340cd6 commit 013e2af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ The same as [https://github.com/iovxw/rssbot/](https://github.com/iovxw/rssbot/)
You can add channel id to subscribe a feed for a channel in private chat after add the bot as administrator
for example `/sub <channel id > <feed url>` (channel id is startwith @)

You can send a opml file directly to import feed in private chat use `/import` in group
You can send a opml file directly to import feed in private chat
use `/import` in group

for channel import send a opml file name by channel id with a opml suffix name in private chat for example `@myChannel.opml`

viewall can only be uesed in private chat

# Depolyment
Expand Down Expand Up @@ -128,6 +132,7 @@ RSS 解析用的是 [rss-parser](https://www.npmjs.com/package/rss-parser),它
例如 `/sub <频道 id > <feed url>` (频道 id 是@打头的)

直接发送 opml 文件,可以导入 RSS 源
频道导入需要将文件名改成频道 id 并且以 opml 作为后缀在私聊中发送 例如 `@myChannel.opml`
viewall 只能在私聊中使用

# 部署
Expand Down
14 changes: 4 additions & 10 deletions controlers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,15 @@ ctrl.rss = async (ctx, next) => {
});
}
await twoKeyReply(builder.join('\n'), kbs)(ctx, next);
await next();
};

ctrl.unsubAll = async (ctx, next) => {
const userId = ctx.state.chat.id;
await RSS.unsubAll(userId);
await ctx.telegram.sendMessage(
ctx.state.chat.id,
i18n['UNSUB_ALL_SUCCESS'],
{
parse_mode: 'HTML',
disable_web_page_preview: true
}
);
await ctx.telegram.sendMessage(ctx.chat.id, i18n['UNSUB_ALL_SUCCESS'], {
parse_mode: 'HTML',
disable_web_page_preview: true
});
await next();
};

Expand Down Expand Up @@ -145,7 +140,6 @@ ctrl.viewAll = async (ctx, next) => {
});
ctx.state.replyText = builder.join('\n');
await twoKeyReply(kbs)(ctx, next);
await next();
};

module.exports = ctrl;
30 changes: 21 additions & 9 deletions middlewares/get-file-link.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
const logger = require('../utils/logger');

module.exports = async (ctx, next) => {
if (
ctx.message.reply_to_message &&
ctx.message.reply_to_message.text.startsWith('/import')
) {
const channelId = ctx.message.reply_to_message.text.match(/@\w+/)[0];
ctx.state.chat = await ctx.telegram.getChat(channelId);
} else {
ctx.state.chat = ctx.chat;
}
const fileId = ctx.message.document.file_id;
const fileName = ctx.message.document.file_name;
if (fileName.startsWith('@')) {
const channelId = fileName.match(/(@.+)\.opml$/)[1];
try {
ctx.state.chat = await ctx.telegram.getChat(channelId);
} catch (e) {
logger.error(e);
if (e.message === '400: Bad Request: chat not found')
throw new Error('CHANNEL_NOT_FOUND');
}
const me = await ctx.telegram.getMe();
const admins = await ctx.telegram.getChatAdministrators(
ctx.state.chat.id
);
const isAdmin = admins.some(function(item) {
return item.user.id === me.id;
});
if (!isAdmin) throw new Error('CHANNEL_ADMIN_REQUIRE');
}
const fileLink = await ctx.telegram.getFileLink(fileId);
if (fileLink) {
ctx.state.fileLink = fileLink;
Expand Down
14 changes: 8 additions & 6 deletions utils/two-key-reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ module.exports = (text, kbs) => {
text = null;
}
return async (ctx, next) => {
await ctx.telegram.deleteMessage(
ctx.state.chat.id,
ctx.state.processMesId
);
ctx.state.processMesId = null;
if (ctx.state.processMesId) {
await ctx.telegram.deleteMessage(
ctx.chat.id,
ctx.state.processMesId
);
ctx.state.processMesId = null;
}
await ctx.telegram.sendMessage(
ctx.state.chat.id,
ctx.chat.id,
text || ctx.state.replyText,
{
parse_mode: 'HTML',
Expand Down

0 comments on commit 013e2af

Please sign in to comment.