From 8f820761bae09059a427c22906ada2701d339712 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 17 Feb 2025 14:16:10 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=8A=A0=E5=85=A5=E4=BA=86=E6=92=A4=E5=9B=9E?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message_storage/message_to_db.py | 4 +- robot.py | 64 +++++++++++++++++++--- {group_auto => robot_cmd}/robot_command.py | 11 +++- 3 files changed, 67 insertions(+), 12 deletions(-) rename {group_auto => robot_cmd}/robot_command.py (96%) diff --git a/message_storage/message_to_db.py b/message_storage/message_to_db.py index efd7150..440f9bb 100644 --- a/message_storage/message_to_db.py +++ b/message_storage/message_to_db.py @@ -29,7 +29,7 @@ def archive_message(group_id, timestamp_str, sender, content, message_type, atta # 提交事务 connection.commit() - print("Message archived successfully.") + print(f"Message archived successfully: {content}") except Exception as e: print(f"Error archiving message: {e}") @@ -79,7 +79,7 @@ def get_messages(group_id, all_contacts: dict): # 将列表中的字符串连接成一个最终的结果 result_str = "\n".join(result) - print(result_str) # 输出带逗号的字符串 + # print(result_str) # 输出带逗号的字符串 return result_str finally: diff --git a/robot.py b/robot.py index b940c44..fc08ff6 100644 --- a/robot.py +++ b/robot.py @@ -23,9 +23,9 @@ from base.func_xinghuo_web import XinghuoWeb from base.func_claude import Claude from configuration import Config from constants import ChatType -from group_auto.robot_command import GroupBotManager +from robot_cmd.robot_command import GroupBotManager from job_mgmt import Job -from group_auto.robot_command import Feature +from robot_cmd.robot_command import Feature __version__ = "39.2.4.0" @@ -46,6 +46,7 @@ class Robot(Job): self.LOG = logging.getLogger("Robot") self.wxid = self.wcf.get_self_wxid() self.allContacts = self.getAllContacts() + self.groups = {} # 存储按group_id分组的消息列表,每个group_id最多保留10条消息 GroupBotManager.load_local_cache() self.gbm = GroupBotManager() if ChatType.is_in_chat_types(chat_type): @@ -130,16 +131,22 @@ class Robot(Job): def toChitchat(self, msg: WxMsg) -> bool: """闲聊,接入 ChatGPT """ + # 去除@的人和空格等字符 + q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "") + + # 所有人员都可以要求他撤回刚刚的信息 + if msg.from_group(): + if q == '撤回': + self.revoke_messages(msg.roomid) + return True # 如果聊天内容来自自己,则进行指令判断 if msg.from_self() and msg.from_group(): - command_str = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "") - rsp = GroupBotManager.handle_command(msg.roomid, command_str) + rsp = GroupBotManager.handle_command(msg.roomid, q) self.sendTextMsg(rsp, msg.roomid, msg.sender) return True if not self.chat: # 没接 ChatGPT,固定回复 rsp = "你@我干嘛?" else: # 接了 ChatGPT,智能回复 - q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "") if q == "今日百度新闻": self.newsBaiduReport((msg.roomid if msg.from_group() else msg.sender)) return True @@ -175,7 +182,6 @@ class Robot(Job): # 群聊消息 if msg.from_group(): - # 调用统计逻辑进行聊天数据统计: try: process_message(msg) @@ -189,6 +195,10 @@ class Robot(Job): except Exception as e: self.LOG.error(f"archive_message error: {e}") + # 记录在群里发的最新消息,可以通过撤回指令撤回 + if msg.from_self(): + self.revoke_receive_message(msg) + # 如果在群里被 @ if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略 return @@ -213,6 +223,7 @@ class Robot(Job): if msg.from_self(): if msg.content == "^更新$": self.config.reload() + self.gbm.load_local_cache() self.LOG.info("已更新") if msg.content == "今日百度新闻": self.newsBaiduReport() @@ -222,12 +233,15 @@ class Robot(Job): self.messageCountToDB() if msg.content == 'PDF': self.generateSehuatangPdf() + if msg.content == 'GROUP_LIST': + self.sendTextMsg(self.gbm.get_group_list(), msg.sender) + self.sendTextMsg(f"指令:{msg.content} 已执行", msg.sender) else: self.toChitchat(msg) # 闲聊 def onMsg(self, msg: WxMsg) -> int: try: - self.LOG.info(msg) # 打印信息 + self.LOG.debug(msg) # 打印信息 self.processMsg(msg) except Exception as e: self.LOG.error(e) @@ -242,7 +256,7 @@ class Robot(Job): while wcf.is_receiving_msg(): try: msg = wcf.get_msg() - self.LOG.info(msg) + self.LOG.debug(msg) self.processMsg(msg) except Empty: continue # Empty message @@ -333,6 +347,40 @@ class Robot(Job): except Exception as e: self.LOG.error(f"send_group_file_message:{feature.description} error:{e}") + # 自动撤回功能块 + def revoke_receive_message(self, msg: WxMsg): + try: + group_id = msg.roomid + # 如果该group_id没有记录过,初始化一个列表 + if group_id not in self.groups: + self.groups[group_id] = [] + + # 将消息ID添加到对应group_id的消息列表中,最多保留10条消息 + if len(self.groups[group_id]) >= 10: + self.groups[group_id].pop(0) # 超过10条时,移除最早的消息 + + self.groups[group_id].append(msg.id) + self.LOG.debug(f"Message revoke received for group {group_id}: {msg.id}") + except Exception as e: + self.LOG.error(f"Revoke_receive_message error:{e}") + + def revoke_messages(self, group_id): + try: + # 如果没有该group_id,直接返回 + if group_id not in self.groups: + self.LOG.debug(f"No messages found for group {group_id}.") + return + + # 按照逆序撤回该组的消息 + for msg_id in reversed(self.groups[group_id]): + self.wcf.revoke_msg(msg_id) # 假设调用撤回方法 + self.LOG.debug(f"Message {msg_id} recalled from group {group_id}.") + self.groups[group_id].remove(msg_id) # 撤回后移除该消息ID + self.LOG.debug(f"Message {msg_id} removed from group {group_id}.") + + except Exception as e: + self.LOG.error(f"revoke_messages error:{e}") + # ============================================== 业务内容========================================================== def newsBaiduReportAuto(self) -> None: diff --git a/group_auto/robot_command.py b/robot_cmd/robot_command.py similarity index 96% rename from group_auto/robot_command.py rename to robot_cmd/robot_command.py index 01edcd9..faa2ba5 100644 --- a/group_auto/robot_command.py +++ b/robot_cmd/robot_command.py @@ -28,7 +28,7 @@ class Feature(Enum): AI_CAPABILITY = 4, "群AI能力" SUMMARY_CAPABILITY = 5, "群总结能力" PDF_CAPABILITY = 6, "sehuatang PDF能力" - EPIC = 7, "EPIC自动播报" + EPIC = 7, "EPIC自动播报" # 新增的功能 def __new__(cls, value, description): obj = object.__new__(cls) @@ -43,7 +43,7 @@ class Feature(Enum): class GroupBotManager: """群机器人管理,支持本地缓存""" - # 本地缓存 + # 本地缓存作为类级别静态属性 local_cache = { "group_permissions": {}, # 用于缓存群组功能权限 "group_list": set() # 用于缓存 group:list @@ -212,12 +212,16 @@ def simulate_commands(): # 关闭Sehuatang PDF能力 print(GroupBotManager.handle_command(group_id, "6-关闭")) # 使用序号关闭Sehuatang PDF能力 + # 启用EPIC自动播报 + print(GroupBotManager.handle_command(group_id, "7-启用")) # 使用序号启用EPIC自动播报 + # 查看当前群组的功能权限 print(GroupBotManager.get_group_permission(group_id, Feature.ROBOT)) print(GroupBotManager.get_group_permission(group_id, Feature.DAILY_NEWS)) print(GroupBotManager.get_group_permission(group_id, Feature.AI_CAPABILITY)) print(GroupBotManager.get_group_permission(group_id, Feature.SUMMARY_CAPABILITY)) print(GroupBotManager.get_group_permission(group_id, Feature.PDF_CAPABILITY)) + print(GroupBotManager.get_group_permission(group_id, Feature.EPIC)) # 查看群组所有功能和状态 permissions = GroupBotManager.list_group_permissions(group_id) @@ -235,6 +239,9 @@ def simulate_commands(): print("群组清单:") print(GroupBotManager.handle_command(group_id, "GROUP_LIST")) + # 更新缓存 + print(GroupBotManager.handle_command(group_id, "UPDATE")) + # 保存到 Redis GroupBotManager.save_to_redis()