2025-11-05 16:21:05 +08:00
|
|
|
|
"""Callback路由处理"""
|
2025-11-07 15:52:06 +08:00
|
|
|
|
from ..Convention.Runtime.GlobalConfig import *
|
2025-11-07 23:47:33 +08:00
|
|
|
|
from ..Convention.Runtime.Architecture import Architecture
|
2025-11-05 17:27:15 +08:00
|
|
|
|
|
2025-11-05 16:21:05 +08:00
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
|
|
from fastapi.responses import JSONResponse
|
2025-11-10 09:55:26 +08:00
|
|
|
|
import re
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
2025-11-05 17:27:15 +08:00
|
|
|
|
from ..CoreModules.models import CallbackRequest
|
|
|
|
|
|
from ..CoreModules.plugin_interface import PluginInterface
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
2025-11-07 23:47:33 +08:00
|
|
|
|
|
|
|
|
|
|
logger: ProjectConfig = Architecture.Get(ProjectConfig)
|
|
|
|
|
|
ALWAYS_RETURN_OK = logger.FindItem("always_return_ok", True) # 返回ok,可以避免重试
|
|
|
|
|
|
logger.SaveProperties()
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/callback")
|
|
|
|
|
|
async def callback_verify():
|
2025-11-05 17:27:15 +08:00
|
|
|
|
"""Callback可用性校验"""
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Info", "收到Callback验证请求")
|
2025-11-05 16:21:05 +08:00
|
|
|
|
return JSONResponse({"result": "ok"})
|
|
|
|
|
|
|
2025-11-10 09:55:26 +08:00
|
|
|
|
# 机器人名称模式(用于从@消息中提取,兼容半角/全角空格)
|
|
|
|
|
|
AT_PATTERN = re.compile(r'@[^\s]+[\s\u3000]+(.+)', re.DOTALL)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_message_after_at(message: str) -> str:
|
|
|
|
|
|
# 去除首尾空格
|
|
|
|
|
|
message = message.strip()
|
|
|
|
|
|
|
|
|
|
|
|
# 尝试提取@后的内容
|
|
|
|
|
|
at_match = AT_PATTERN.search(message)
|
|
|
|
|
|
if at_match:
|
|
|
|
|
|
return at_match.group(1).strip()
|
|
|
|
|
|
return message
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
2025-11-06 16:16:11 +08:00
|
|
|
|
@router.post("/callback/construct")
|
|
|
|
|
|
async def callback_receive_construct(callback_data: CallbackRequest):
|
|
|
|
|
|
"""以构造好的Callback消息进行处理, 已知方式"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 解析指令
|
2025-11-10 09:55:26 +08:00
|
|
|
|
content = parse_message_after_at(callback_data.content)
|
2025-11-06 16:16:11 +08:00
|
|
|
|
command = content.split(" ")[0]
|
|
|
|
|
|
message = content[len(command):].strip()
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Info", f"识别指令: command={command}")
|
2025-11-06 16:16:11 +08:00
|
|
|
|
|
|
|
|
|
|
# 处理指令
|
|
|
|
|
|
result = await handle_command(command, message, callback_data.chatid, callback_data.creator)
|
|
|
|
|
|
if result:
|
|
|
|
|
|
return JSONResponse({"result": "ok", "message": result})
|
|
|
|
|
|
else:
|
|
|
|
|
|
return JSONResponse({"result": "ok"})
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Error", f"处理Callback异常: {e}")
|
2025-11-06 16:16:11 +08:00
|
|
|
|
if ALWAYS_RETURN_OK:
|
|
|
|
|
|
return JSONResponse({"result": "ok"})
|
|
|
|
|
|
else:
|
|
|
|
|
|
return JSONResponse({"result": "error", "message": str(e)})
|
|
|
|
|
|
|
2025-11-05 16:21:05 +08:00
|
|
|
|
@router.post("/callback")
|
|
|
|
|
|
async def callback_receive(request: Request):
|
2025-11-06 16:16:11 +08:00
|
|
|
|
"""接受未知的Callback消息并进行处理, 默认方式"""
|
2025-11-05 16:21:05 +08:00
|
|
|
|
try:
|
|
|
|
|
|
# 解析请求数据
|
|
|
|
|
|
data = await request.json()
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Info", f"完整callback数据: {data}")
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
# 验证请求
|
|
|
|
|
|
try:
|
|
|
|
|
|
callback_data = CallbackRequest(**data)
|
|
|
|
|
|
except Exception as e:
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Error", f"请求数据验证失败: {e}")
|
2025-11-05 17:27:15 +08:00
|
|
|
|
if ALWAYS_RETURN_OK:
|
|
|
|
|
|
return JSONResponse({"result": "ok"})
|
|
|
|
|
|
else:
|
|
|
|
|
|
return JSONResponse({"result": "error", "message": str(e)})
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
# 解析指令
|
2025-11-10 10:06:26 +08:00
|
|
|
|
content = parse_message_after_at(callback_data.content)
|
2025-11-05 17:27:15 +08:00
|
|
|
|
command = content.split(" ")[0]
|
2025-11-06 10:55:39 +08:00
|
|
|
|
message = content[len(command):].strip()
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Info", f"识别指令: command={command}")
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
2025-11-06 10:55:39 +08:00
|
|
|
|
# 处理指令
|
|
|
|
|
|
result = await handle_command(command, message, callback_data.chatid, callback_data.creator)
|
|
|
|
|
|
if result:
|
|
|
|
|
|
return JSONResponse({"result": "ok", "message": result})
|
|
|
|
|
|
else:
|
|
|
|
|
|
return JSONResponse({"result": "ok"})
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Error", f"处理Callback异常: {e}")
|
2025-11-05 17:27:15 +08:00
|
|
|
|
if ALWAYS_RETURN_OK:
|
|
|
|
|
|
return JSONResponse({"result": "ok"})
|
|
|
|
|
|
else:
|
|
|
|
|
|
return JSONResponse({"result": "error", "message": str(e)})
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-11-06 10:55:39 +08:00
|
|
|
|
async def handle_command(command: str, message: str,
|
|
|
|
|
|
chat_id: int, user_id: int) -> str|None:
|
|
|
|
|
|
"""处理指令
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|
|
|
|
|
|
Args:
|
2025-11-05 17:27:15 +08:00
|
|
|
|
command: 指令
|
2025-11-06 10:55:39 +08:00
|
|
|
|
message: 消息内容
|
2025-11-05 16:21:05 +08:00
|
|
|
|
chat_id: 会话ID
|
|
|
|
|
|
user_id: 用户ID
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
2025-11-06 10:55:39 +08:00
|
|
|
|
回复文本或None
|
2025-11-05 16:21:05 +08:00
|
|
|
|
"""
|
|
|
|
|
|
try:
|
2025-11-05 17:27:15 +08:00
|
|
|
|
plugin = PluginInterface.plugin_instances.get(command, None)
|
|
|
|
|
|
if plugin:
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Info", f"已找到插件注册指令: {command}, class: {plugin.__class__.__name__}")
|
2025-11-06 10:55:39 +08:00
|
|
|
|
return await plugin.callback(message, chat_id, user_id)
|
2025-11-05 16:21:05 +08:00
|
|
|
|
else:
|
2025-11-05 17:27:15 +08:00
|
|
|
|
return f"❌ 未识别指令: {command}"
|
2025-11-05 16:21:05 +08:00
|
|
|
|
except Exception as e:
|
2025-11-07 23:47:33 +08:00
|
|
|
|
logger.Log("Error", f"{ConsoleFrontColor.RED}处理指令异常: {e}{ConsoleFrontColor.RESET}")
|
2025-11-05 17:27:15 +08:00
|
|
|
|
return f"❌ 处理指令时出错: {str(e)}"
|
2025-11-05 16:21:05 +08:00
|
|
|
|
|