1.加入了撤回指令

This commit is contained in:
liuwei
2025-02-17 14:16:10 +08:00
parent af2ef96c3e
commit 8f820761ba
3 changed files with 67 additions and 12 deletions

View File

@@ -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: