Skip to content

Commit 013e2af

Browse files
committed
feat: channel import support
1 parent f340cd6 commit 013e2af

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ The same as [https://github.com/iovxw/rssbot/](https://github.com/iovxw/rssbot/)
2626
You can add channel id to subscribe a feed for a channel in private chat after add the bot as administrator
2727
for example `/sub <channel id > <feed url>` (channel id is startwith @)
2828

29-
You can send a opml file directly to import feed in private chat use `/import` in group
29+
You can send a opml file directly to import feed in private chat
30+
use `/import` in group
31+
32+
for channel import send a opml file name by channel id with a opml suffix name in private chat for example `@myChannel.opml`
33+
3034
viewall can only be uesed in private chat
3135

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

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

133138
# 部署

controlers/rss.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,15 @@ ctrl.rss = async (ctx, next) => {
9595
});
9696
}
9797
await twoKeyReply(builder.join('\n'), kbs)(ctx, next);
98-
await next();
9998
};
10099

101100
ctrl.unsubAll = async (ctx, next) => {
102101
const userId = ctx.state.chat.id;
103102
await RSS.unsubAll(userId);
104-
await ctx.telegram.sendMessage(
105-
ctx.state.chat.id,
106-
i18n['UNSUB_ALL_SUCCESS'],
107-
{
108-
parse_mode: 'HTML',
109-
disable_web_page_preview: true
110-
}
111-
);
103+
await ctx.telegram.sendMessage(ctx.chat.id, i18n['UNSUB_ALL_SUCCESS'], {
104+
parse_mode: 'HTML',
105+
disable_web_page_preview: true
106+
});
112107
await next();
113108
};
114109

@@ -145,7 +140,6 @@ ctrl.viewAll = async (ctx, next) => {
145140
});
146141
ctx.state.replyText = builder.join('\n');
147142
await twoKeyReply(kbs)(ctx, next);
148-
await next();
149143
};
150144

151145
module.exports = ctrl;

middlewares/get-file-link.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
const logger = require('../utils/logger');
2+
13
module.exports = async (ctx, next) => {
2-
if (
3-
ctx.message.reply_to_message &&
4-
ctx.message.reply_to_message.text.startsWith('/import')
5-
) {
6-
const channelId = ctx.message.reply_to_message.text.match(/@\w+/)[0];
7-
ctx.state.chat = await ctx.telegram.getChat(channelId);
8-
} else {
9-
ctx.state.chat = ctx.chat;
10-
}
114
const fileId = ctx.message.document.file_id;
5+
const fileName = ctx.message.document.file_name;
6+
if (fileName.startsWith('@')) {
7+
const channelId = fileName.match(/(@.+)\.opml$/)[1];
8+
try {
9+
ctx.state.chat = await ctx.telegram.getChat(channelId);
10+
} catch (e) {
11+
logger.error(e);
12+
if (e.message === '400: Bad Request: chat not found')
13+
throw new Error('CHANNEL_NOT_FOUND');
14+
}
15+
const me = await ctx.telegram.getMe();
16+
const admins = await ctx.telegram.getChatAdministrators(
17+
ctx.state.chat.id
18+
);
19+
const isAdmin = admins.some(function(item) {
20+
return item.user.id === me.id;
21+
});
22+
if (!isAdmin) throw new Error('CHANNEL_ADMIN_REQUIRE');
23+
}
1224
const fileLink = await ctx.telegram.getFileLink(fileId);
1325
if (fileLink) {
1426
ctx.state.fileLink = fileLink;

utils/two-key-reply.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ module.exports = (text, kbs) => {
55
text = null;
66
}
77
return async (ctx, next) => {
8-
await ctx.telegram.deleteMessage(
9-
ctx.state.chat.id,
10-
ctx.state.processMesId
11-
);
12-
ctx.state.processMesId = null;
8+
if (ctx.state.processMesId) {
9+
await ctx.telegram.deleteMessage(
10+
ctx.chat.id,
11+
ctx.state.processMesId
12+
);
13+
ctx.state.processMesId = null;
14+
}
1315
await ctx.telegram.sendMessage(
14-
ctx.state.chat.id,
16+
ctx.chat.id,
1517
text || ctx.state.replyText,
1618
{
1719
parse_mode: 'HTML',

0 commit comments

Comments
 (0)