diff --git a/rssbot.py b/rssbot.py index 574942d..123416c 100644 --- a/rssbot.py +++ b/rssbot.py @@ -30,6 +30,7 @@ from random import randrange from textc import textc from re import search, I from rsschecker import RSSCheckerThread +from rsslist import getInlineKeyBoardForRSSList, InlineKeyBoardForRSSList def getMediaInfo(m: dict, config: RSSConfig = RSSConfig()) -> str: @@ -73,7 +74,7 @@ class InlineKeyBoardCallBack(Enum): SendMedia = 10 -def getInlineKeyBoardWhenRSS(hashd: str, m: dict) -> str: +def getInlineKeyBoardWhenRSS(hashd: str, m: dict) -> dict: d = [] i = 0 d.append([]) @@ -476,14 +477,15 @@ class messageHandle(Thread): self._main._db.setUserStatus( self._fromUserId, userStatus.normalStatus) return - if self._botCommand is None or self._botCommand not in ['/help', '/rss']: + if self._botCommand is None or self._botCommand not in ['/help', '/rss', '/rsslist']: self._botCommand = '/help' di = {'chat_id': self._chatId} if self.__getChatType() in ['supergroup', 'group'] and self._fromUserId is not None: di['reply_to_message_id'] = self._messageId if self._botCommand == '/help': di['text'] = '''/help 显示帮助 -/rss url 订阅RSS''' +/rss url 订阅RSS +/rsslist [chatId] 获取RSS订阅列表''' elif self._botCommand == '/rss': self._botCommandPara = self._getCommandlinePara() self._uri = None @@ -495,6 +497,22 @@ class messageHandle(Thread): di['text'] = '没有找到URL' else: di['text'] = '正在获取信息中……' + elif self._botCommand == '/rsslist': + self._botCommandPara = self._getCommandlinePara() + targetChatId = self._chatId + for i in self._botCommandPara: + if search(r'^[\+-]?[0-9]+$', i) is not None: + targetChatId = int(i) + if targetChatId == self._chatId: + try: + rssList = self._main._db.getRSSListByChatId(self._chatId) + di['text'] = '列表如下:' + di['reply_markup'] = getInlineKeyBoardForRSSList( + self._chatId, rssList) + except: + di['text'] = '获取列表失败。' + else: + di['text'] = '正在确认操作者权限……' re = self._main._request('sendMessage', 'post', json=di) if self._botCommand == '/rss' and self._uri is not None and re is not None and 'ok' in re and re['ok']: re = re['result'] @@ -543,14 +561,16 @@ class callbackQueryHandle(Thread): def run(self): self._callbackQueryId = self._data['id'] - l = self._data['data'].split(',', 3) - if len(l) != 3: + l = self._data['data'].split(',') + if len(l) < 3: self.answer('错误的按钮数据。') return + self._inputList = l try: self._loc = int(l[0]) - self._hashd = l[1] - self._command = int(l[2]) + if self._loc == 0: + self._hashd = l[1] + self._command = int(l[2]) except: self.answer('错误的按钮数据。') return diff --git a/rsslist.py b/rsslist.py new file mode 100644 index 0000000..9366501 --- /dev/null +++ b/rsslist.py @@ -0,0 +1,70 @@ +# (C) 2021 lifegpc +# This file is part of rssbot. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from RSSEntry import RSSEntry +from typing import List +from enum import Enum, unique +from math import ceil + + +@unique +class InlineKeyBoardForRSSList(Enum): + FirstPage = 0 + LastPage = 1 + PrevPage = 2 + NextPage = 3 + Close = 4 + Content = 5 + + +def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page=1) -> dict: + d = [] + i = -1 + lineLimit = 7 + l = len(RSSEntries) + pn = ceil(l / lineLimit) + if l != 0: + page = max(min(pn, page), 1) + s = max(lineLimit * (page - 1), 0) + n = min(lineLimit * page, l) + while s < n: + rss = RSSEntries[s] + d.append([]) + i = i + 1 + d[i].append({'text': rss.title[0:20], + 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.Content.value},{s}'}) + s = s + 1 + if pn != 1: + d.append([]) + i = i + 1 + if page != 1: + d[i].append( + {'text': '上一页', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.PrevPage.value},{page}'}) + if page != pn: + d[i].append( + {'text': '下一页', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.NextPage.value},{page}'}) + d.append([]) + i = i + 1 + if page != 1: + d[i].append( + {'text': '首页', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.FirstPage.value}'}) + if page != pn: + d[i].append( + {'text': '尾页', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.LastPage.value}'}) + d.append([]) + i = i + 1 + d[i].append( + {'text': '关闭', 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.Close.value}'}) + return {'inline_keyboard': d}