调整入库逻辑,@指令通过dify 触发,让AI逻辑在一起。

This commit is contained in:
liuwei
2025-04-17 13:36:09 +08:00
parent e56bcc16d1
commit 07f5c853e7
3 changed files with 73 additions and 20 deletions

View File

@@ -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 "")
# 获取查询内容

View File

@@ -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, "已签到"

View File

@@ -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, # 提供消息工具类