Skip to content

Commit 09e73f2

Browse files
authored
v2.6.20 (#448)
* feat: 新增配置项 `telegram_admin_ids` `telegram_bot_token` 用于接收 QQ bot 离线通知 * chore(release): v2.6.20
1 parent 38a660c commit 09e73f2

10 files changed

+67
-22
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ELF_RSS"
3-
version = "2.6.19"
3+
version = "2.6.20"
44
description = "QQ机器人 RSS订阅 插件,订阅源建议选择 RSSHub"
55
authors = ["Quan666 <[email protected]>"]
66
license = "GPL-3.0-only"

src/plugins/ELF_RSS2/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .rss_class import Rss
1212
from .utils import send_message_to_admin
1313

14-
VERSION = "2.6.19"
14+
VERSION = "2.6.20"
1515

1616
__plugin_meta__ = PluginMetadata(
1717
name="ELF_RSS",

src/plugins/ELF_RSS2/command/upload_group_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ async def handle_first_receive(
4242
url=target[0],
4343
group_ids=[str(event.group_id)],
4444
name="手动上传",
45-
proxy=get_proxy(True),
45+
proxy=get_proxy(),
4646
)

src/plugins/ELF_RSS2/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class Config:
4949
# pikpak 离线保存的目录, 默认是根目录,示例: ELF_RSS/Downloads ,目录不存在会自动创建, 不能/结尾
5050
pikpak_download_path: str = ""
5151

52+
telegram_admin_ids: List[int] = [] # Telegram 管理员 ID 列表,用于接收离线通知和管理机器人
53+
telegram_bot_token: Optional[str] = None # Telegram 机器人的 token
54+
5255

5356
config = ELFConfig(**get_driver().config.dict())
5457
logger.debug(f"RSS Config loaded: {config!r}")

src/plugins/ELF_RSS2/parsing/download_torrent.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Any, Dict, List, Optional
33

44
import aiohttp
5-
from nonebot import get_bot
65
from nonebot.adapters.onebot.v11 import Bot
76
from nonebot.log import logger
87

@@ -11,7 +10,7 @@
1110
from ..pikpak_offline import pikpak_offline_download
1211
from ..qbittorrent_download import start_down
1312
from ..rss_class import Rss
14-
from ..utils import convert_size, get_torrent_b16_hash, send_msg
13+
from ..utils import convert_size, get_bot, get_torrent_b16_hash, send_msg
1514

1615

1716
async def down_torrent(
@@ -20,7 +19,9 @@ async def down_torrent(
2019
"""
2120
创建下载种子任务
2221
"""
23-
bot: Bot = get_bot() # type: ignore
22+
bot: Bot = await get_bot() # type: ignore
23+
if bot is None:
24+
raise ValueError("There are not bots to get.")
2425
hash_list = []
2526
for tmp in item["links"]:
2627
if (

src/plugins/ELF_RSS2/parsing/send_message.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from typing import Any, Callable, Coroutine, DefaultDict, Dict, List, Tuple, Union
55

66
import arrow
7-
from nonebot import get_bot
87
from nonebot.adapters.onebot.v11 import Bot, Message, MessageSegment
98
from nonebot.adapters.onebot.v11.exception import NetworkError
109
from nonebot.log import logger
1110

1211
from ..config import config
1312
from ..rss_class import Rss
13+
from ..utils import get_bot
1414
from .cache_manage import insert_into_cache_db, write_item
1515

1616
sending_lock: DefaultDict[Tuple[Union[int, str], str], asyncio.Lock] = defaultdict(
@@ -22,9 +22,8 @@
2222
async def send_msg(
2323
rss: Rss, messages: List[str], items: List[Dict[str, Any]], header_message: str
2424
) -> bool:
25-
try:
26-
bot: Bot = get_bot() # type: ignore
27-
except ValueError:
25+
bot: Bot = await get_bot() # type: ignore
26+
if bot is None:
2827
return False
2928
if not messages:
3029
return False

src/plugins/ELF_RSS2/parsing/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
# 代理
8-
def get_proxy(open_proxy: bool) -> Optional[str]:
8+
def get_proxy(open_proxy: bool = True) -> Optional[str]:
99
if not open_proxy or not config.rss_proxy:
1010
return None
1111
return f"http://{config.rss_proxy}"

src/plugins/ELF_RSS2/qbittorrent_download.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import aiohttp
88
import arrow
99
from apscheduler.triggers.interval import IntervalTrigger
10-
from nonebot import get_bot
1110
from nonebot.adapters.onebot.v11 import ActionFailed, Bot, NetworkError
1211
from nonebot.log import logger
1312
from qbittorrent import Client
1413

1514
from .config import config
1615
from .utils import (
1716
convert_size,
17+
get_bot,
1818
get_bot_group_list,
1919
get_torrent_b16_hash,
2020
scheduler,
@@ -68,23 +68,21 @@ async def get_qb_client() -> Optional[Client]:
6868
else:
6969
qb.login()
7070
except Exception:
71-
bot: Bot = get_bot() # type: ignore
7271
msg = (
7372
"❌ 无法连接到 qbittorrent ,请检查:\n"
7473
"1. 是否启动程序\n"
7574
"2. 是否勾选了“Web用户界面(远程控制)”\n"
7675
"3. 连接地址、端口是否正确"
7776
)
7877
logger.exception(msg)
79-
await send_message_to_admin(msg, bot)
78+
await send_message_to_admin(msg)
8079
return None
8180
try:
8281
qb.get_default_save_path()
8382
except Exception:
84-
bot: Bot = get_bot() # type: ignore
8583
msg = "❌ 无法连登录到 qbittorrent ,请检查相关配置是否正确"
8684
logger.exception(msg)
87-
await send_message_to_admin(msg, bot)
85+
await send_message_to_admin(msg)
8886
return None
8987
return qb
9088

@@ -253,5 +251,7 @@ async def rss_trigger(bot: Bot, hash_str: str, group_ids: List[str], name: str)
253251
misfire_grace_time=60, # 允许的误差时间,建议不要省略
254252
job_defaults=job_defaults,
255253
)
256-
bot: Bot = get_bot() # type: ignore
254+
bot: Bot = await get_bot() # type: ignore
255+
if bot is None:
256+
return
257257
await send_msg(bot, f"👏 {name}\nHash:{hash_str}\n下载任务添加成功!", group_ids)

src/plugins/ELF_RSS2/rss_parsing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import aiohttp
44
import feedparser
5-
from nonebot import get_bot
65
from nonebot.adapters.onebot.v11 import Bot
76
from nonebot.log import logger
87
from tinydb import TinyDB
@@ -19,6 +18,7 @@
1918
filter_valid_group_id_list,
2019
filter_valid_guild_channel_id_list,
2120
filter_valid_user_id_list,
21+
get_bot,
2222
get_http_caching_headers,
2323
send_message_to_admin,
2424
)
@@ -69,7 +69,9 @@ async def save_first_time_fetch(rss: Rss, new_rss: Dict[str, Any]) -> None:
6969

7070
# 抓取 feed,读取缓存,检查更新,对更新进行处理
7171
async def start(rss: Rss) -> None:
72-
bot: Bot = get_bot() # type: ignore
72+
bot: Bot = await get_bot() # type: ignore
73+
if bot is None:
74+
return
7375

7476
# 先检查订阅者是否合法
7577
rss = await filter_and_validate_rss(rss, bot)

src/plugins/ELF_RSS2/utils.py

+43-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
from contextlib import suppress
66
from typing import Any, Dict, Generator, List, Mapping, Optional
77

8+
from aiohttp import ClientSession
89
from cachetools import TTLCache
910
from cachetools.keys import hashkey
10-
from nonebot import get_bot, require
11+
from nonebot import get_bot as _get_bot
12+
from nonebot import require
1113
from nonebot.adapters.onebot.v11 import Bot
1214
from nonebot.log import logger
1315

@@ -19,6 +21,9 @@
1921
from nonebot_plugin_guild_patch.permission import GUILD_ADMIN, GUILD_OWNER # noqa
2022

2123
from .config import config
24+
from .parsing.utils import get_proxy
25+
26+
bot_offline = False
2227

2328

2429
def get_http_caching_headers(
@@ -113,7 +118,11 @@ def get_torrent_b16_hash(content: bytes) -> str:
113118
return str(b16_hash, "utf-8")
114119

115120

116-
async def send_message_to_admin(message: str, bot: Bot) -> None:
121+
async def send_message_to_admin(message: str, bot: Optional[Bot] = None) -> None:
122+
if bot is None:
123+
bot: Bot = await get_bot() # type: ignore
124+
if bot is None:
125+
return
117126
await bot.send_private_msg(user_id=int(list(config.superusers)[0]), message=message)
118127

119128

@@ -129,7 +138,9 @@ async def send_msg(
129138
130139
发送消息到私聊或群聊
131140
"""
132-
bot: Bot = get_bot() # type: ignore
141+
bot: Bot = await get_bot() # type: ignore
142+
if bot is None:
143+
raise ValueError("There are not bots to get.")
133144
msg_id = []
134145
if group_ids:
135146
for group_id in group_ids:
@@ -208,3 +219,32 @@ def partition_list(
208219
) -> Generator[List[Any], None, None]:
209220
for i in range(0, len(input_list), partition_size):
210221
yield input_list[i : i + partition_size]
222+
223+
224+
async def send_message_to_telegram_admin(message: str) -> None:
225+
try:
226+
async with ClientSession(raise_for_status=True) as session:
227+
await session.post(
228+
f"https://api.telegram.org/bot{config.telegram_bot_token}/sendMessage",
229+
json={
230+
"chat_id": config.telegram_admin_ids[0],
231+
"text": message,
232+
},
233+
proxy=get_proxy(),
234+
)
235+
except Exception as e:
236+
logger.error(f"发送到 Telegram 失败:\n {e}")
237+
238+
239+
async def get_bot() -> Optional[Bot]:
240+
global bot_offline
241+
bot: Optional[Bot] = None
242+
try:
243+
bot = _get_bot() # type: ignore
244+
bot_offline = False
245+
except ValueError:
246+
if not bot_offline and config.telegram_admin_id and config.telegram_bot_token:
247+
await send_message_to_telegram_admin("QQ Bot 已离线!")
248+
logger.warning("Bot 已离线!")
249+
bot_offline = True
250+
return bot

0 commit comments

Comments
 (0)