加入定时任务

This commit is contained in:
liuwei
2025-05-06 16:33:08 +08:00
parent 6111da93fa
commit 2ff2f4c23b
2 changed files with 86 additions and 64 deletions

19
main.py
View File

@@ -40,6 +40,25 @@ def main(chat_type: int):
robot.LOG.info("wechat_ipad客户端启动成功")
else:
robot.LOG.error("wechat_ipad客户端启动失败")
# # 每天 8:30 发送新闻
robot.onEveryTime("08:30", robot.news_baidu_report_auto)
#
# # epic
robot.onEveryTime("10:30", robot.send_epic_free_games)
#
# # message report 1:数据自动从redis 转到sqllite
robot.onEveryTime("02:30", robot.message_count_to_db)
# # 从db中提取并发送给相关群
robot.onEveryTime("09:30", robot.generate_and_send_ranking)
#
# # sehuatang
# robot.onEveryTime("15:30", robot.generate_sehuatang_pdf)
#
# # 秀人网每天自动下载帖子
robot.onEveryTime("01:30", robot.xiu_ren_download_task)
#
# # 秀人网每天自动发pdf
# robot.onEveryTime("17:30", robot.xiu_ren_pdf_send)
# 启动Dashboard服务器
dashboard_server = None

131
robot.py
View File

@@ -9,6 +9,8 @@ import toml
import wechat_ipad
from loguru import logger
from base.func_epic import is_friday, get_free
from base.func_news import News
from configuration import Config
from job_mgmt import Job
from plugin_common.event_system import EventType, EventSystem
@@ -26,6 +28,7 @@ from utils.wechat.contact_manager import ContactManager
from utils.wechat.message_to_db import MessageStorage
from wechat_ipad import WechatAPIClient
from wechat_ipad.models.message import WxMessage, MessageType
from xiuren.meitu_dl import meitu_dowload_pub_pic
class Robot(Job):
@@ -375,13 +378,13 @@ class Robot(Job):
if is_group:
rsp = self.gbm.handle_command(group_id, content.clean_content)
if rsp is not None:
await self.send_text(rsp, group_id)
await self.ipad_bot.send_text_message(group_id, rsp)
else:
# 处理特殊命令
if content == "^更新$":
self.config.reload()
self.gbm.load_local_cache()
await self.send_text("已更新", "filehelper")
await self.ipad_bot.send_text_message("filehelper", "已更新")
if is_group:
self.LOG.debug(f"入库和记录群消息: {message}")
@@ -403,55 +406,6 @@ class Robot(Job):
except Exception as e:
self.LOG.error(f"处理wechat_ipad消息出错: {e}")
def _convert_to_wx_message(self, message):
"""将wechat_ipad消息转换为WxMessage对象"""
from wechat_ipad.models.message import WxMessage
return WxMessage.from_json(message)
async def send_text(self, content, to_user, at_user=None):
"""发送文本消息"""
if not self.ipad_bot or not self.ipad_running:
self.LOG.error("wechat_ipad客户端未初始化或未运行")
return False
try:
if at_user and to_user.endswith("@chatroom"):
# 在群里@某人
await self.ipad_bot.send_text_message(to_user, content, at_list=[at_user])
else:
# 普通发送
await self.ipad_bot.send_text_message(to_user, content)
return True
except Exception as e:
self.LOG.error(f"发送文本消息失败: {e}")
return False
async def send_image(self, image_path, to_user):
"""发送图片消息"""
if not self.ipad_bot or not self.ipad_running:
self.LOG.error("wechat_ipad客户端未初始化或未运行")
return False
try:
await self.ipad_bot.send_image_message(to_user, image_path)
return True
except Exception as e:
self.LOG.error(f"发送图片消息失败: {e}")
return False
async def send_file(self, file_path, to_user):
"""发送文件消息"""
if not self.ipad_bot or not self.ipad_running:
self.LOG.error("wechat_ipad客户端未初始化或未运行")
return False
try:
await self.ipad_bot.send_file(to_user, file_path)
return True
except Exception as e:
self.LOG.error(f"发送文件消息失败: {e}")
return False
def stop_wechat_ipad(self):
"""停止wechat_ipad客户端"""
self.ipad_running = False
@@ -482,22 +436,10 @@ class Robot(Job):
return
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.send_text(msg, r)
await self.ipad_bot.send_text_message(r, msg)
except Exception as e:
self.LOG.error(f"send_group_txt_message:{feature.description} error{e}")
async def send_group_file_message(self, path: str, feature: Feature):
"""向所有启用了特定功能的群发送文件消息"""
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, feature) == PermissionStatus.ENABLED:
await self.send_file(path, r)
except Exception as e:
self.LOG.error(f"send_group_file_message:{feature.description} error{e}")
async def process_plugin_message(self, msg) -> bool:
"""使用插件处理消息"""
# 获取所有消息处理插件
@@ -550,3 +492,64 @@ class Robot(Job):
except Exception as e:
self.LOG.error(f"获取联系人信息失败: {e}")
return {}
# ============================================== 业务内容==========================================================
def news_baidu_report_auto(self) -> None:
try:
news = News().get_baidu_news()
self.send_group_txt_message(news, Feature.DAILY_NEWS)
except Exception as e:
self.LOG.error(f"newsBaiduReportAuto error{e}")
def news_en_report(self, website, sender: str = None) -> None:
try:
news = News().get_eng_news(website)
self.ipad_bot.send_text_message(sender, news)
except Exception as e:
self.LOG.error(f"newsEnReport error{e}")
# 使用装饰器标记定时任务 星期五 10:30 执行
def send_epic_free_games(self):
try:
if is_friday():
games = get_free()
self.send_group_txt_message(games, Feature.EPIC)
except Exception as e:
self.LOG.error(f"sendEpicFreeGames error{e}")
# 使用装饰器标记定时任务
def message_count_to_db(self):
try:
self.message_storage.write_to_db()
except Exception as e:
self.LOG.error(f"write_to_db error{e}")
# def generate_sehuatang_pdf(self):
# try:
# self.LOG.info("开始生成PDF,generate_sehuatang_pdf")
# path = pdf_file_path()
# # 暂时只发4K群
# self.send_group_file_message(path, Feature.PDF_CAPABILITY)
# except Exception as e:
# self.LOG.error(f"generateSehuatangPdf error{e}")
def xiu_ren_download_task(self):
try:
# 每天下载10组图然后发一个帖子PDF
meitu_dowload_pub_pic()
except Exception as e:
self.LOG.error(f"xiu_ren_download_task error{e}")
def generate_and_send_ranking(self):
try:
receivers = self.gbm.get_group_list()
if not receivers:
return
for r in receivers:
if self.gbm.get_group_permission(r, Feature.DAILY_SUMMARY) == PermissionStatus.ENABLED:
output = self.message_storage.generate_and_send_ranking(r, self.allContacts)
self.ipad_bot.send_text_message(r, output)
except Exception as e:
self.LOG.error(f"SendRanking error{e}")