diff --git a/plugins/dify/main.py b/plugins/dify/main.py index b112e02..4f32444 100644 --- a/plugins/dify/main.py +++ b/plugins/dify/main.py @@ -3,6 +3,7 @@ import os import requests import json import time +import re # 添加re模块导入 from typing import Dict, Any, List, Optional, Tuple from wcferry import Wcf @@ -102,7 +103,16 @@ class DifyPlugin(MessagePluginInterface): content = str(message.get("content", "")).strip() command = content.split(" ")[0] - return command in self._commands + # 检查是否是命令触发 + if command in self._commands: + return True + + # 检查是否是被@的消息 + if message.get("is_at", False) and message.get("roomid", ""): + # 只处理群聊中被@的消息 + return True + + return False @plugin_stats_decorator(plugin_name="Dify聊天") @plugin_points_cost(2, "AI聊天消耗积分", Feature.AI_CAPABILITY) @@ -110,23 +120,65 @@ class DifyPlugin(MessagePluginInterface): """处理消息""" content = str(message.get("content", "")).strip() self.LOG.info(f"插件执行: {self.name}:{content}") - parts = content.split(" ", 1) - command = parts[0] sender = message.get("sender") roomid = message.get("roomid", "") wcf: Wcf = message.get("wcf") gbm: GroupBotManager = message.get("gbm") - + + # 处理被@的消息 + if message.get("is_at", False) and roomid: + # 检查权限 + if gbm.get_group_permission(roomid, Feature.AI_CAPABILITY) == PermissionStatus.DISABLED: + return False, "没有权限" + + # 去除@的人和空格等字符 + query = re.sub(r"@.*?[\u2005|\s]", "", content).strip() + + if not query: + wcf.send_text("请在@我的同时提供问题内容", roomid, sender) + return True, "没有提供问题内容" + + self.message_util.send_text_msg("⏳AI 正在加油,请稍候… 😊", roomid, sender) + + try: + # 调用Dify API获取回复 + response = self._chat_with_dify(roomid, sender, query) + # 去除广告内容 + response = remove_trailing_content(response) + + # 发送回复 + if response: + # 判断是否为本地文件路径 + if os.path.isfile(response): + # 如果是文件路径,使用发送文件方法 + wcf.send_file(response, roomid) + else: + # 如果是普通文本,使用发送文本方法 + wcf.send_text(response, roomid, sender) + return True, "发送成功" + else: + wcf.send_text("❌未能获取到回复,请稍后再试", roomid, sender) + return True, "未获取到回复" + + except Exception as e: + self.LOG.error(f"处理Dify聊天请求出错: {e}") + wcf.send_text(f"❌请求出错:{str(e)}", roomid, sender) + return True, f"处理出错: {e}" + + # 原有的命令处理逻辑 + parts = content.split(" ", 1) + command = parts[0] + # 检查命令格式 if len(parts) < 2 or not parts[1].strip(): wcf.send_text(f"{self.command_format}", (roomid if roomid else sender), sender) return True, "命令格式错误" - + # 检查权限 if roomid and gbm.get_group_permission(roomid, Feature.AI_CAPABILITY) == PermissionStatus.DISABLED: return False, "没有权限" - + self.message_util.send_text_msg("⏳AI 正在加油,请稍候… 😊", (roomid if roomid else sender), sender if roomid else "") # 获取查询内容 diff --git a/plugins/message_sign/main.py b/plugins/message_sign/main.py index 74a68b8..ab957a0 100644 --- a/plugins/message_sign/main.py +++ b/plugins/message_sign/main.py @@ -240,7 +240,7 @@ class MessageSignPlugin(MessagePluginInterface): # 如果 sign_stat 已经大于或等于今天的零点,则认为用户已经签到过了 if sign_stat >= today_start: - self.message_util.send_text_msg(f"您今天已经签到过了!共获得积分:{user_record['points']}", + self.message_util.send_text_msg(f"您今天已经签到过了!", (roomid if roomid else sender), sender) return False, "已签到" diff --git a/robot.py b/robot.py index 2b7a6f8..4e42b82 100644 --- a/robot.py +++ b/robot.py @@ -154,13 +154,6 @@ class Robot(Job): return all(value is not None for key, value in args.items() if key != 'proxy') return False - def toAt(self, msg: WxMsg) -> bool: - """处理被 @ 消息 - :param msg: 微信消息结构 - :return: 处理状态,`True` 成功,`False` 失败 - """ - return self.toChitchat(msg) - def toChitchat(self, msg: WxMsg) -> bool: """闲聊,接入 ChatGPT """ @@ -210,12 +203,14 @@ class Robot(Job): # 发布消息接收事件 self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg}) + # 标记插件是否处理了消息 + plugin_processed = False + # 尝试使用插件处理消息 if self.process_plugin_message(msg): - return + plugin_processed = True - # 如果没有插件处理,使用原有逻辑处理消息 - # 群聊消息 + # 群聊消息处理 - 无论插件是否处理过,都执行数据存储 if msg.from_group(): # 调用统计逻辑进行聊天数据统计: try: @@ -232,6 +227,10 @@ class Robot(Job): except Exception as e: self.LOG.error(f"archive_message error: {e}") + # 如果插件已处理消息,则不再执行后续的业务逻辑 + if plugin_processed: + return + # 记录在群里发的最新消息,可以通过撤回指令撤回 try: if msg.from_self(): @@ -243,10 +242,12 @@ class Robot(Job): except Exception as e: self.LOG.error(f"revoke_receive_message error: {e}") - if msg.is_at(self.wxid): # 被@ - self.toAt(msg) return # 处理完群聊信息,后面就不需要处理了 + # 如果插件已处理消息,则不再执行后续的业务逻辑 + if plugin_processed: + return + # 非群聊信息,按消息类型进行处理 if msg.type == 37: # 好友请求 self.LOG.info(f"收到好友请求:{msg}") @@ -421,7 +422,7 @@ class Robot(Job): "sender": msg.sender, "roomid": msg.roomid if msg.from_group() else "", "xml": msg.xml, - "is_at": msg.is_at(self.wxid), + "is_at": msg.is_at(self.wxid), # 确保正确设置is_at标志 "timestamp": time.time(), "wcf": self.wcf, # 提供wcf对象,让插件可以直接发送消息 "message_util": self.message_util, # 提供消息工具类