调整入库逻辑,@指令通过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, "已签到"