855 协议版本-调整完毕内容

This commit is contained in:
liuwei
2025-04-30 13:22:33 +08:00
parent 869bce8a18
commit 454d084715
88 changed files with 1565 additions and 7816 deletions

829
robot.py
View File

@@ -1,39 +1,30 @@
# -*- coding: utf-8 -*-
import logging
import re
import time
import asyncio
import threading
import tomllib
import toml
import dacite
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 gewechat.call_back_message.message import WxMessage, MessageType
from gewechat.client import gewe_client
from gewechat.response.model.group.chatroom_info import ChatroomInfo
from gewechat.response.model.personal.profile import Profile
from utils.json_converter import json_to_object
from utils.wechat.message_to_db import MessageStorage
from job_mgmt import Job
from plugin_common.event_system import EventType, EventSystem
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
from plugin_common.plugin_manager import PluginManager
from plugin_common.plugin_registry import PluginRegistry
from utils.robot_cmd.robot_command import GroupBotManager
from job_mgmt import Job
from utils.robot_cmd.robot_command import Feature
from utils.robot_cmd.robot_command import PermissionStatus
from sehuatang.shehuatang import pdf_file_path
from utils.wechat.contact_manager import ContactManager
from xiuren.meitu_dl import meitu_dowload_pub_pic
from xiuren.xiuren_pdf import generate_pdf_from_images
from utils.robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
from db.connection import DBConnectionManager
from message_util import MessageUtil
from db.contacts_db import ContactsDBOperator
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
class Robot(Job):
@@ -41,13 +32,20 @@ class Robot(Job):
"""
def __init__(self, config: Config) -> None:
self.client = gewe_client.client
if not self.client:
logging.getLogger("Robot").error("gewe_client.client 不存在Robot 初始化失败,程序退出。")
return
super().__init__()
self.config = config
self.app_id = gewe_client.app_id
self.LOG = logging.getLogger("Robot")
self.LOG = logger
# wechat_ipad 相关属性
self.ipad_bot: WechatAPIClient
self.ipad_config = None
self.ipad_running = False
self.ipad_thread = None
self.ipad_loop = None
self.wxid = None
self.nickname = None
self.alias = None
self.phone = None
self.LOG.info(f"DB+REDIS 连接池开始初始化")
# 使用单例模式获取实例
@@ -58,45 +56,30 @@ class Robot(Job):
self.LOG.info(f"数据库连接管理器初始化完成")
# 为了兼容现有代码,保留原有的连接池
self.db_pool = self.db_manager .mysql_pool
self.db_pool = self.db_manager.mysql_pool
self.redis_pool = self.db_manager.redis_pool
self.contacts_db = ContactsDBOperator(self.db_manager)
# 初始化联系人管理器并设置联系人
# 初始化联系人管理器
self.contact_manager = ContactManager.get_instance()
self.allContacts = self.get_all_contacts()
self.contact_manager.set_contacts(self.allContacts)
self.allContacts = {} # 将在登录后填充
# 获取个人信息
profile_dict = self.client.get_profile(self.app_id)
try:
profile: Profile = dacite.from_dict(Profile, profile_dict)
except Exception as e:
self.LOG.info(f"Profile dict 转换失败: {e}")
return
if profile.data.wxid is None:
self.LOG.info(f"获取个人信息失败,退出程序!")
return
self.wxid = profile.data.wxid
# 初始化消息工具类 - 使用联系人管理器
self.message_util = MessageUtil()
self.groups = {} # 存储按group_id分组的消息列表每个group_id最多保留10条消息
GroupBotManager.load_local_cache()
# 权限模块加载
self.gbm = GroupBotManager()
# 初始化插件系统
self.LOG.info("开始初始化插件系统...")
self.plugin_registry = PluginRegistry()
self.event_system = EventSystem()
self.plugin_modules = {} # 存储已加载的插件模块
self.plugins = {} # 存储已加载的插件实例
self.message_util = None
# 设置插件系统上下文
self.system_context = {
"config": config,
"client": gewe_client,
"event_system": self.event_system,
"plugin_registry": self.plugin_registry,
"db_pool": self.db_pool,
@@ -111,174 +94,366 @@ class Robot(Job):
# 加载插件
self.LOG.info("插件系统初始化完成")
# 消息存档模块初始化,自动完成入库动作
self.message_storage = MessageStorage(self.client)
@staticmethod
def value_check(args: dict) -> bool:
if args:
return all(value is not None for key, value in args.items() if key != 'proxy')
return False
def toChitchat(self, msg: WxMessage) -> bool:
"""闲聊,接入 ChatGPT
"""
# 去除@的人和空格等字符
q = re.sub(r"@.*?[\u2005|\s]", "", msg.content.raw_content).replace(" ", "")
if q == "#今日百度新闻":
self.news_baidu_report((msg.roomid if msg.from_group() else msg.sender))
return True
elif q in ["nbc", "cnn", "abc", "fox", "bbc"]:
self.news_en_report(q, (msg.roomid if msg.from_group() else msg.sender))
return True
else:
# 如果是群消息并且群没开启AI则不处理该动作
if msg.from_group() and self.gbm.get_group_permission(msg.roomid,
Feature.AI_CAPABILITY) == PermissionStatus.ENABLED:
resp = self.gbm.get_enabled_features(msg.roomid)
self.message_util.send_text(resp, (msg.roomid if msg.from_group() else msg.sender), msg.sender)
return True
else:
return True
def processMsg(self, msg: WxMessage) -> None:
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
此处可进行自定义发送的内容,如通过 msg.content 关键字自动获取当前天气信息,并发送到对应的群组@发送者
群号msg.roomid 微信IDmsg.sender 消息内容msg.content
content = "xx天气信息为"
receivers = msg.roomid
self.sendTextMsg(content, receivers, msg.sender)
"""
def init_wechat_ipad(self):
"""初始化wechat_ipad客户端"""
try:
# 检测群聊是否已加入机器人管理,如果没有则自动添加并开启机器人功能
if msg.from_group() and msg.roomid not in GroupBotManager.local_cache["group_list"]:
self.LOG.info(f"检测到新群聊: {msg.roomid},自动添加到机器人管理列表并启用机器人功能")
# 读取config.toml文件
with open("wechat_ipad/config.toml", "rb") as f:
self.ipad_config = tomllib.load(f)
self.LOG.info("正在初始化wechat_ipad客户端...")
# 检查必要的配置
server_url = self.ipad_config.get("server_url", "")
if server_url == "":
self.LOG.error("server_url不能为空wechat_ipad初始化失败")
return False
server_ip = self.ipad_config.get("server_ip", "")
server_port = self.ipad_config.get("server_port", 8058)
# 创建事件循环
self.ipad_loop = asyncio.new_event_loop()
# 在新线程中启动wechat_ipad客户端
self.ipad_thread = threading.Thread(
target=self._run_wechat_ipad_client,
args=(server_ip, server_port),
daemon=True
)
self.ipad_thread.start()
self.LOG.info("wechat_ipad客户端初始化完成")
return True
except Exception as e:
self.LOG.error(f"初始化wechat_ipad客户端失败: {e}")
return False
def _run_wechat_ipad_client(self, server_ip, server_port):
"""在新线程中运行wechat_ipad客户端"""
asyncio.set_event_loop(self.ipad_loop)
self.ipad_loop.run_until_complete(self._wechat_ipad_core(server_ip, server_port))
async def _wechat_ipad_core(self, server_ip, server_port):
"""wechat_ipad核心逻辑基于bot-core.py"""
try:
self.LOG.info("启动wechat_ipad bot")
# 调用登录接口
self.ipad_bot = wechat_ipad.WechatAPIClient(server_ip, server_port)
wxid = self.ipad_config.get("wxid", "")
device_name = self.ipad_config.get("device_name", "")
device_id = self.ipad_config.get("device_id", "")
if device_name == "":
device_name = self.ipad_bot.create_device_name()
if device_id == "":
device_id = self.ipad_bot.create_device_id()
# 登录逻辑
if not await self.ipad_bot.is_logged_in(wxid):
await self._handle_ipad_login(wxid, device_name, device_id)
else: # 已登录
self.ipad_bot.wxid = wxid
profile = await self.ipad_bot.get_profile()
self.ipad_bot.nickname = profile.get("NickName").get("string")
self.ipad_bot.alias = profile.get("Alias")
self.ipad_bot.phone = profile.get("BindMobile").get("string")
# 更新Robot类的属性
self.wxid = self.ipad_bot.wxid
self.nickname = self.ipad_bot.nickname
self.alias = self.ipad_bot.alias
self.phone = self.ipad_bot.phone
self.LOG.info(
f"wechat_ipad登录账号信息: wxid: {self.wxid} 昵称: {self.nickname} 微信号: {self.alias} 手机号: {self.phone}")
self.LOG.info(f"wechat_ipad登录设备信息: device_name: {device_name} device_id: {device_id}")
self.LOG.info("wechat_ipad登录成功")
# 登录成功后加载联系人信息
self.allContacts = self.get_all_contacts()
self.contact_manager.set_contacts(self.allContacts)
# 开启自动心跳(作为后台任务)
heartbeat_task = asyncio.create_task(self._heartbeat_task())
self.message_storage = MessageStorage(self.ipad_bot)
# 初始化消息工具类 - 使用联系人管理器
self.message_util = MessageUtil(self.ipad_bot)
# 先接受堆积消息
self.LOG.info("处理堆积消息中")
count = 0
while True:
data = await self.ipad_bot.sync_message()
data = data.get("AddMsgs")
if not data:
if count > 2:
break
else:
count += 1
continue
self.LOG.debug(f"接受到 {len(data)} 条消息")
await asyncio.sleep(1)
self.LOG.info("处理堆积消息完毕")
# 标记为运行中
self.ipad_running = True
# 开始处理消息
self.LOG.info("开始处理wechat_ipad消息")
while self.ipad_running:
try:
data = await self.ipad_bot.sync_message()
except Exception as e:
self.LOG.warning(f"获取新消息失败 {e}")
await asyncio.sleep(5)
continue
data = data.get("AddMsgs")
if data:
for message in data:
# 处理消息
wxmsg: WxMessage = WxMessage.from_json(message)
await self._process_ipad_message(wxmsg)
# 使用异步睡眠替代忙等待循环
await asyncio.sleep(0.5)
except Exception as e:
self.LOG.error(f"wechat_ipad客户端运行出错: {e}")
self.ipad_running = False
async def _handle_ipad_login(self, wxid, device_name, device_id):
"""处理wechat_ipad登录"""
while not await self.ipad_bot.is_logged_in(wxid):
# 需要登录
try:
if await self.ipad_bot.get_cached_info(wxid):
# 尝试唤醒登录
uuid = await self.ipad_bot.awaken_login(wxid)
self.LOG.info(f"获取到登录uuid: {uuid}")
else:
# 二维码登录
if not device_name:
device_name = self.ipad_bot.create_device_name()
if not device_id:
device_id = self.ipad_bot.create_device_id()
uuid, url = await self.ipad_bot.get_qr_code(device_id=device_id, device_name=device_name,
print_qr=True)
self.LOG.info(f"获取到登录uuid: {uuid}")
self.LOG.info(f"获取到登录二维码: {url}")
except Exception as e:
self.LOG.error(f"登录过程出错: {e}")
# 二维码登录
if not device_name:
device_name = self.ipad_bot.create_device_name()
if not device_id:
device_id = self.ipad_bot.create_device_id()
uuid, url = await self.ipad_bot.get_qr_code(device_id=device_id, device_name=device_name, print_qr=True)
self.LOG.info(f"获取到登录uuid: {uuid}")
self.LOG.info(f"获取到登录二维码: {url}")
while True:
self.LOG.info(f"uuid: {uuid}, url: {url}")
stat, data = await self.ipad_bot.check_login_uuid(uuid, device_id=device_id)
if stat:
break
self.LOG.info(f"等待登录中,过期倒计时:{data}")
await asyncio.sleep(5)
# 保存登录信息
self.ipad_config["wxid"] = self.ipad_bot.wxid
self.ipad_config["device_name"] = device_name
self.ipad_config["device_id"] = device_id
with open("wechat_ipad/config.toml", "w", encoding="utf-8") as f:
toml.dump(self.ipad_config, f)
# 获取登录账号信息
self.ipad_bot.wxid = data.get("acctSectResp").get("userName")
self.ipad_bot.nickname = data.get("acctSectResp").get("nickName")
self.ipad_bot.alias = data.get("acctSectResp").get("alias")
self.ipad_bot.phone = data.get("acctSectResp").get("bindMobile")
# 更新Robot类的属性
self.wxid = self.ipad_bot.wxid
self.nickname = self.ipad_bot.nickname
self.alias = self.ipad_bot.alias
self.phone = self.ipad_bot.phone
self.LOG.info(
f"wechat_ipad登录账号信息: wxid: {self.wxid} 昵称: {self.nickname} 微信号: {self.alias} 手机号: {self.phone}")
break
async def _heartbeat_task(self):
"""wechat_ipad心跳任务"""
self.LOG.info("开启wechat_ipad心跳")
while self.ipad_running:
try:
success = await self.ipad_bot.heartbeat()
if success:
self.LOG.success("心跳进行中")
else:
self.LOG.warning("心跳失败")
except Exception as e:
self.LOG.error(f"wechat_ipad heartbeat: {e}")
await asyncio.sleep(5)
async def _process_ipad_message(self, message: WxMessage):
"""处理wechat_ipad消息"""
try:
self.LOG.debug(f"message: {message}")
# 消息已经是WxMessage对象直接使用其属性和方法
from_user = message.sender
to_user = message.to_user
content = message.content
msg_type = message.msg_type
# 判断是否为群消息
is_group = message.from_group()
group_id = message.roomid
# 检测群聊是否已加入机器人管理
if is_group and group_id not in GroupBotManager.local_cache["group_list"]:
self.LOG.info(f"检测到新群聊: {group_id},自动添加到机器人管理列表并启用机器人功能")
# 添加群组到列表
GroupBotManager.local_cache["group_list"].add(msg.roomid)
GroupBotManager.local_cache["group_list"].add(group_id)
# 保存到Redis
redis_conn = self.db_manager.get_redis_connection()
redis_conn.sadd("group:list", msg.roomid)
redis_conn.sadd("group:list", group_id)
# 设置ROBOT功能为启用状态
GroupBotManager.set_group_permission(msg.roomid, Feature.ROBOT, PermissionStatus.ENABLED)
# 更新联系人信息
# 群第一次加入机器人管理,自动添加并开启机器人功能,需要进行群成员信息初始化。请完成写入数据库,并更新联系人信息
GroupBotManager.set_group_permission(group_id, Feature.ROBOT, PermissionStatus.ENABLED)
# 获取群成员信息并更新数据库
try:
chatroom_info = self.client.get_chatroom_info(self.app_id, msg.roomid)
self.LOG.info(f"chatroom_info: {chatroom_info}")
self.contacts_db.save_chatroom_info(chatroom_info.get('data', {}))
self.LOG.info(f"添加新的群信息到数据库成功:{chatroom_info}")
# 添加 memberList 到群组信息表中
member_list = chatroom_info.get('data', {}).get('memberList', [])
self.contacts_db.save_chatroom_member_simple(msg.roomid, member_list)
for info in member_list:
wxid = info.get("wxid", "")
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
# 更新缓存
self.allContacts[wxid] = info.get("nickName", "nickName")
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
self.contact_manager.set_contacts(self.allContacts)
chatroom_info = await self.ipad_bot.get_chatroom_info(group_id)
if chatroom_info:
# 保存群信息到数据库
self.contacts_db.save_chatroom_info(chatroom_info)
# 保存群成员信息
if "NewChatroomData" in chatroom_info and "ChatRoomMember" in chatroom_info["NewChatroomData"]:
member_list = chatroom_info["NewChatroomData"]["ChatRoomMember"]
self.contacts_db.save_chatroom_member_simple(group_id, member_list)
# 更新联系人缓存
for member in member_list:
wxid = ""
if isinstance(member.get("UserName"), dict):
wxid = member["UserName"].get("string", "")
else:
wxid = member.get("UserName", "")
nick_name = ""
if isinstance(member.get("NickName"), dict):
nick_name = member["NickName"].get("string", "")
else:
nick_name = member.get("NickName", "")
if wxid:
self.allContacts[wxid] = nick_name
self.contact_manager.set_contacts(self.allContacts)
self.LOG.info(f"已更新群 {group_id} 的成员信息")
except Exception as e:
self.LOG.error(f"chatroom_info save error: {e}")
return
self.LOG.error(f"获取群成员信息失败: {e}")
# 发布消息接收事件
self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": message})
# 尝试使用插件处理消息
plugin_processed = await self.process_plugin_message(message)
self.LOG.debug(f"plugin_processed: {plugin_processed}")
# 处理群聊命令或私聊命令
if from_user == self.wxid: # 自己发送的消息
if is_group:
rsp = self.gbm.handle_command(group_id, content)
if rsp is not None:
await self.send_text(rsp, group_id)
else:
# 处理特殊命令
if content == "^更新$":
self.config.reload()
self.gbm.load_local_cache()
await self.send_text("已更新", "filehelper")
if is_group:
self.LOG.debug(f"入库和记录群消息: {message}")
# 调用统计逻辑进行聊天数据统计:
try:
self.message_storage.process_message(message)
except Exception as e:
self.LOG.error(f"process_message error: {e}")
# # 聊天记录入库动作:
try:
self.message_storage.archive_message(message)
# 单独处理图片消息 后续写定时任务自动完成下载。延时处理。
# if message.msg_type == MessageType.IMAGE: # 图片消息类型
# self.message_storage.process_image(message)
except Exception as e:
self.LOG.error(f"archive_message error: {e}")
except Exception as e:
self.LOG.error(f"加入新群,自动添加并开启机器人功能 error: {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
# 如果用户信息缓存里面没有这个用户昵称,则添加用户信息,并且维护该用户信息
# 以 wxid 作为唯一标识
try:
if msg.from_group():
wxid = msg.sender
if wxid and wxid not in self.allContacts:
# 添加到数据库
# 这里假设 contacts_db 有 save_contact_info 方法,参数为 dict
resp = self.client.get_chatroom_member_detail(self.app_id, msg.roomid, [wxid])
infos = resp.get('data', {})
for info in infos:
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
# 更新缓存
self.allContacts[wxid] = info.get("nickName", "nickName")
self.contact_manager.set_contacts(self.allContacts)
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
self.contacts_db.save_chatroom_member_detail(msg.roomid, infos)
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}")
#
# # 发布消息接收事件
# self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg})
#
# # 标记插件是否处理了消息
# plugin_processed = False
#
# # 尝试使用插件处理消息
# if self.process_plugin_message(msg):
# plugin_processed = True
#
# # 群聊消息处理 - 无论插件是否处理过,都执行数据存储
if msg.from_group():
# 调用统计逻辑进行聊天数据统计:
try:
self.message_storage.process_message(msg)
except Exception as e:
self.LOG.error(f"process_message error: {e}")
self.LOG.error(f"发送文本消息失败: {e}")
return False
# # 聊天记录入库动作:
try:
self.message_storage.archive_message(msg)
# 单独处理图片消息 后续写定时任务自动完成下载。延时处理。
if msg.msg_type == MessageType.IMAGE: # 图片消息类型
self.message_storage.process_image(msg)
except Exception as e:
self.LOG.error(f"archive_message error: {e}")
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
# # 如果插件已处理消息,则不再执行后续的业务逻辑
# if plugin_processed:
# return
#
# # 记录在群里发的最新消息,可以通过撤回指令撤回
# try:
# if msg.from_self():
# rsp = self.gbm.handle_command(msg.roomid, msg.content)
# # 不在群里发送,防止被骚扰
# if rsp is not None:
# self.message_util.send_text(rsp, msg.roomid, msg.sender)
# return
# except Exception as e:
# self.LOG.error(f"revoke_receive_message error: {e}")
#
# return # 处理完群聊信息,后面就不需要处理了
#
# # 如果插件已处理消息,则不再执行后续的业务逻辑
# if plugin_processed:
# return
#
# elif msg.msg_type == MessageType.TEXT: # 文本消息
# # 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
# if msg.from_self():
# if msg.content.clean_content == "^更新$":
# self.config.reload()
# self.gbm.load_local_cache()
# self.LOG.info("已更新")
# if msg.content.clean_content == "今日百度新闻":
# self.news_baidu_report()
# if msg.content.clean_content == "TO_DB":
# self.message_count_to_db()
# if msg.content.clean_content == "PDF":
# self.generate_sehuatang_pdf()
# if msg.content.raw_content.startswith("清除群-"):
# self.gbm.handle_command(msg.roomid, msg.content.clean_content)
# else:
# self.toChitchat(msg) # 闲聊
def onMsg(self, msg: WxMessage) -> int:
try:
self.LOG.info(msg) # 打印信息
self.processMsg(msg)
await self.ipad_bot.send_image_message(to_user, image_path)
return True
except Exception as e:
self.LOG.error(e)
self.LOG.error(f"发送图片消息失败: {e}")
return False
return 0
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
if self.ipad_loop:
self.ipad_loop.stop()
self.LOG.info("wechat_ipad客户端已停止")
def keep_running_and_block_process(self) -> None:
"""
@@ -295,32 +470,36 @@ class Robot(Job):
self.contact_manager.refresh_contacts(self.allContacts)
self.LOG.info("联系人信息已刷新")
def send_group_txt_message(self, msg: str, feature: Feature):
async def send_group_txt_message(self, msg: 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:
self.message_util.send_text(msg, r)
await self.send_text(msg, r)
except Exception as e:
self.LOG.error(f"send_group_txt_message:{feature.description} error{e}")
def send_group_file_message(self, path: str, feature: Feature):
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:
self.message_util.send_file(path, r)
await self.send_file(path, r)
except Exception as e:
self.LOG.error(f"send_group_file_message:{feature.description} error{e}")
def process_plugin_message(self, msg: WxMessage) -> bool:
async def process_plugin_message(self, msg) -> bool:
"""使用插件处理消息"""
# 获取所有消息处理插件
message_plugins = self.plugin_registry.get_plugins_by_type(MessagePluginInterface)
if not message_plugins:
return False
# 依次尝试处理消息
for plugin in message_plugins:
@@ -328,267 +507,41 @@ class Robot(Job):
continue
try:
# 转换WxMessage为插件可处理的格式
# 转换消息为插件可处理的格式
plugin_msg = {
"type": msg.msg_type,
"content": msg.content.clean_content,
"sender": msg.sender,
"roomid": msg.roomid if msg.from_group() else "",
"xml": msg.content.xml_content,
"is_at": msg.is_at(self.wxid), # 确保正确设置is_at标志
"is_at": msg.is_at(self.wxid),
"timestamp": time.time(),
"message_util": self.message_util, # 提供消息工具类
"gbm": self.gbm, # 每次从程序变量中取,保证最新
"all_contacts": self.allContacts,
"full_wx_msg": msg
"full_wx_msg": msg,
"gbm": self.gbm,
"bot": self.ipad_bot
}
# 检查插件是否可以处理该消息
if plugin.can_process(plugin_msg):
processed, _ = plugin.process_message(plugin_msg)
processed, _ = await plugin.process_message(plugin_msg)
if processed:
# 发布消息处理事件
self.event_system.publish(EventType.MESSAGE_PROCESSED, {
"message": msg,
"plugin": plugin.name
})
return True
except Exception as e:
self.LOG.error(f"插件 {plugin.name} 处理消息失败: {e}")
return False
# ============================================== 业务内容==========================================================
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_baidu_report(self, sender: str = None) -> None:
try:
news = News().get_baidu_news()
if news and isinstance(news, str):
self.message_util.send_text(news, sender)
else:
self.LOG.error("获取百度新闻返回值异常")
except Exception as e:
self.LOG.error(f"newsBaiduReport error{e}")
# 发送错误信息给用户,让用户知道发生了什么
def news_en_report(self, website, sender: str = None) -> None:
try:
news = News().get_eng_news(website)
self.message_util.send_text(news, sender)
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 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.message_util.send_text(output, r)
except Exception as e:
self.LOG.error(f"SendRanking error{e}")
#
# # 设置定时任务
# def game_auto_tasks(self):
# try:
# group_ids = get_group_ids()
# for gid in group_ids:
# if self.gbm.get_group_permission(gid, Feature.TASK_GAME) == PermissionStatus.ENABLED:
# rep = run_random_task_assignment(group_id=gid)
# message = rep["message"]
# player_id = rep["player_id"]
# print(f"消息: {message}")
# print(f"玩家ID: {player_id}")
# self.send_text_msg(message, gid, player_id)
# except Exception as e:
# self.LOG.error(f"message_summary_robot 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 xiu_ren_pdf_send(self):
try:
pub_path = generate_pdf_from_images("xiuren")
self.message_util.send_file(pub_path, "45317011307@chatroom")
except Exception as e:
self.LOG.error(f"xiu_ren_pdf_send error{e}")
# 本逻辑主要解决加载联系人信息的问题,只从数据库里面提取,不完成下载行为。
def get_all_contacts(self) -> dict:
"""获取所有联系人信息并返回字典格式 {wxid: nickname}"""
# 从数据库提取信息,如果数据库没内容,则完成第一次初始化。
try:
# 先尝试从数据库获取联系人信息
contacts_dict = self.contacts_db.get_all_contacts()
# 获取群成员列表
return contacts_dict
# 从数据库获取联系人信息
contacts = self.contacts_db.get_all_contacts()
return contacts
except Exception as e:
self.LOG.error(f"获取联系人信息失败: {e}")
return {}
def sync_all_contacts(self):
"""同步所有联系人信息"""
try:
# 数据库中没有联系人信息,需要初始化
self.LOG.info("数据库中没有联系人信息,开始初始化...")
contacts_dict = {}
# 获取所有联系人列表
response = self.client.fetch_contacts_list(self.app_id)
self.LOG.info(f"获取联系人列表响应: {response}")
if not response or response.get("ret") != 200:
self.LOG.warning(f"获取联系人列表失败: {response}")
return contacts_dict
# 从响应中提取联系人数据
contact_data = response.get("data", {})
# 处理好友列表
friends = contact_data.get("friends", [])
for wxid in friends:
contacts_dict[wxid] = wxid # 默认使用wxid作为昵称
# 处理群聊列表
chatrooms = contact_data.get("chatrooms", [])
for chatroom_id in chatrooms:
contacts_dict[chatroom_id] = chatroom_id
# 如果是群聊,则获取群成员信息
self.update_chatroom_member_details(chatroom_id)
# 处理公众号列表
ghs = contact_data.get("ghs", [])
for gh_id in ghs:
contacts_dict[gh_id] = gh_id
# 获取联系人详细信息(昵称等)
self.update_contact_details(contacts_dict)
self.LOG.info(f"成功获取并保存{len(contacts_dict)}个联系人信息")
return contacts_dict
except Exception as e:
self.LOG.error(f"获取联系人信息失败: {e}")
return {}
def update_contact_details(self, contacts_dict):
"""更新联系人详细信息(昵称等)"""
try:
# 将wxid列表分批处理每批50个
batch_size = 10
wxids = list(contacts_dict.keys())
for i in range(0, len(wxids), batch_size):
batch_wxids = wxids[i:i + batch_size]
# 批量获取联系人详细信息
contact_info = self.client.get_detail_info(self.app_id, batch_wxids)
self.LOG.info(f"获取联系人详细信息响应: {contact_info}")
# 处理返回的数据
if contact_info and contact_info.get("ret") == 200 and "data" in contact_info:
contact_data = contact_info.get("data", [])
if contact_data:
for contact in contact_data:
user_name = contact.get("userName")
if not user_name or user_name not in contacts_dict:
continue
# 更新昵称
contacts_dict[user_name] = contact.get("nickName") or user_name
try:
# 判断联系人类型
contact_type = "friends" # 默认为好友类型
if user_name.endswith("@chatroom"):
contact_type = "chatrooms"
elif user_name.startswith("gh_"):
contact_type = "ghs"
# 保存到数据库
self.contacts_db.save_contacts([contact], contact_type)
except Exception as e:
self.LOG.error(f"处理联系人 {user_name} 失败: {e}")
continue
else:
self.LOG.error(f"获取联系人详情失败: {contact_info}")
except Exception as e:
self.LOG.error(f"更新联系人详细信息失败: {e}")
def update_chatroom_member_details(self, chatroom_id):
"""更新群成员详细信息"""
try:
# 首先获取群成员列表
members_response = self.client.get_chatroom_member_list(self.app_id, chatroom_id)
if members_response and members_response.get('ret') == 200:
member_list = members_response.get('data', {}).get('memberList', [])
# 提取成员wxid列表
member_wxids = [member.get('wxid') for member in member_list if member.get('wxid')]
if member_wxids:
# 按照官方接口格式传递参数
details_response = self.client.get_chatroom_member_detail(
self.app_id,
chatroom_id,
member_wxids # 直接传递列表,不需要转换为集合
)
success = self.contacts_db.process_chatroom_member_detail_response(chatroom_id, details_response)
if success:
self.LOG.info(f"成功更新群聊{chatroom_id}的成员详细信息")
else:
self.LOG.error(f"更新群聊{chatroom_id}的成员详细信息失败")
return success
else:
self.LOG.warning(f"群聊{chatroom_id}没有成员")
return False
else:
self.LOG.error(f"获取群聊{chatroom_id}成员列表失败")
return False
except Exception as e:
self.LOG.error(f"更新群聊成员详细信息出错: {e}")