系统业务任务插件化迁移:下沉7项非刚需任务并接入平滑迁移

- 系统任务保留刚需三项:登录巡检、消息计数入库、媒体补偿处理;移除新闻/Epic/排行/PDF/秀人维护等业务型系统任务定义\n- 新增 daily_news、epic_free、daily_ranking、sehuatang_push 四个插件,将原系统业务任务改为插件可调度动作\n- 扩展 xiuren_image 插件调度动作,新增秀人下载、绅士R15下载、图片缓存更新三项维护任务\n- 新增系统任务到插件任务的幂等迁移逻辑:按旧 job_key 映射到插件 action,同步 trigger_type/trigger_config/enabled,并通过 payload 标记防止反复覆盖\n- 在 Robot 启动流程中接入迁移执行与重载,并清理已迁移的历史系统任务记录,避免后台双份维护\n- 扩展插件调度数据库操作:支持按 plugin_name + action_key 精确查询,便于迁移与对账
This commit is contained in:
liuwei
2026-04-16 16:05:59 +08:00
parent 1166323ab5
commit 9652c2594e
13 changed files with 748 additions and 73 deletions

153
plugins/daily_news/main.py Normal file
View File

@@ -0,0 +1,153 @@
# -*- coding: utf-8 -*-
import asyncio
import base64
from typing import Any, Dict, List, Optional, Tuple
import requests
from base.func_news import News
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from utils.robot_cmd.robot_command import GroupBotManager
from wechat_ipad.models.appmsg_xml import LINK_XML_NEWS
class DailyNewsPlugin(MessagePluginInterface):
"""每日新闻定时插件。"""
FEATURE_KEY = "DAILY_NEWS"
FEATURE_DESCRIPTION = "📰 每日新闻自动播报 [每日8:30定时发送]"
@property
def name(self) -> str:
return "每日新闻"
@property
def version(self) -> str:
return "1.0.0"
@property
def description(self) -> str:
return "将百度新闻日报能力下沉为插件定时任务。"
@property
def author(self) -> str:
return "ABOT Team"
@property
def commands(self) -> List[str]:
# 该插件只负责后台调度,不处理前台命令。
return []
@property
def feature_key(self) -> Optional[str]:
return self.FEATURE_KEY
@property
def feature_description(self) -> Optional[str]:
return self.FEATURE_DESCRIPTION
def __init__(self):
super().__init__()
self.feature = self.register_feature()
def initialize(self, context: Dict[str, Any]) -> bool:
self.LOG.debug(f"正在初始化 {self.name} 插件...")
return True
def start(self) -> bool:
self.status = PluginStatus.RUNNING
return True
def stop(self) -> bool:
self.status = PluginStatus.STOPPED
return True
def can_process(self, message: Dict[str, Any]) -> bool:
return False
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
return False, None
def get_schedule_actions(self) -> List[Dict[str, Any]]:
"""声明插件可调度动作。"""
return [
{
"action_key": "baidu_news_daily_push",
"name": "百度新闻日报推送",
"description": "每天推送百度新闻文本、60秒新闻图和资讯卡片",
"trigger_type": "at_times",
"trigger_config": {"time_list": ["08:30"]},
"target_scope": "all_enabled_groups",
"target_config": {},
"payload": {},
"default_enabled": True,
}
]
async def run_scheduled_action(self, action_key: str, context: Dict[str, Any]) -> Dict[str, Any]:
if action_key != "baidu_news_daily_push":
return {
"success": False,
"summary": f"不支持的动作: {action_key}",
"detail": {"action_key": action_key},
}
if not self.bot:
return {"success": False, "summary": "bot 未注入", "detail": {}}
target_groups = [str(g).strip() for g in (context.get("target_groups") or []) if str(g).strip()]
if not target_groups:
# 兜底:当后台未指定范围时,直接按群功能开关收集目标群。
target_groups = [
gid for gid in GroupBotManager.get_group_list()
if GroupBotManager.get_group_permission(gid, self.feature).value == "enabled"
]
if not target_groups:
return {"success": False, "summary": "没有可推送目标群", "detail": {"target_count": 0}}
try:
# 新闻抓取为同步逻辑,放入线程池避免阻塞调度主循环。
text_news = await asyncio.to_thread(News().get_baidu_news)
image_url = await asyncio.to_thread(News().get_news_60s)
except Exception as e:
return {"success": False, "summary": f"新闻抓取失败: {e}", "detail": {"error": str(e)}}
# 图片接口返回 URL统一下载为 base64 再发送,兼容 wechat_ipad 图片发送接口。
image_base64 = ""
if image_url:
try:
image_base64 = await asyncio.to_thread(self._download_image_as_base64, image_url)
except Exception as e:
self.LOG.warning(f"每日新闻图片下载失败,将仅发送文本和卡片: {e}")
success_groups = []
failed_groups = {}
for gid in target_groups:
try:
if text_news:
await self.bot.send_text_message(gid, text_news)
if image_base64:
await self.bot.send_image_message(gid, image_base64)
await self.bot.send_link_xml_message(LINK_XML_NEWS, gid)
success_groups.append(gid)
except Exception as e:
failed_groups[gid] = str(e)
return {
"success": len(failed_groups) == 0,
"summary": f"每日新闻推送完成: 成功{len(success_groups)}群, 失败{len(failed_groups)}",
"detail": {
"target_count": len(target_groups),
"success_groups": success_groups,
"failed_groups": failed_groups,
},
}
@staticmethod
def _download_image_as_base64(url: str) -> str:
"""下载图片并转为 base64便于统一发送。"""
resp = requests.get(url, timeout=15)
resp.raise_for_status()
return base64.b64encode(resp.content).decode("utf-8")