Files
PWF/CoreRouters/callback.py

109 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Callback路由处理"""
from ..Convention.Runtime.GlobalConfig import *
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from ..CoreModules.models import CallbackRequest
from ..CoreModules.plugin_interface import PluginInterface
config = ProjectConfig()
ALWAYS_RETURN_OK = config.FindItem("always_return_ok", True) # 返回ok可以避免重试
router = APIRouter()
@router.get("/callback")
async def callback_verify():
"""Callback可用性校验"""
config.Log("Info", "收到Callback验证请求")
return JSONResponse({"result": "ok"})
@router.post("/callback/construct")
async def callback_receive_construct(callback_data: CallbackRequest):
"""以构造好的Callback消息进行处理, 已知方式"""
try:
# 解析指令
content = callback_data.content
command = content.split(" ")[0]
message = content[len(command):].strip()
config.Log("Info", f"识别指令: command={command}")
# 处理指令
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:
config.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
return JSONResponse({"result": "error", "message": str(e)})
@router.post("/callback")
async def callback_receive(request: Request):
"""接受未知的Callback消息并进行处理, 默认方式"""
try:
# 解析请求数据
data = await request.json()
config.Log("Info", f"完整callback数据: {data}")
# 验证请求
try:
callback_data = CallbackRequest(**data)
except Exception as e:
config.Log("Error", f"请求数据验证失败: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
return JSONResponse({"result": "error", "message": str(e)})
# 解析指令
content = callback_data.content
command = content.split(" ")[0]
message = content[len(command):].strip()
config.Log("Info", f"识别指令: command={command}")
# 处理指令
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:
config.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
return JSONResponse({"result": "error", "message": str(e)})
async def handle_command(command: str, message: str,
chat_id: int, user_id: int) -> str|None:
"""处理指令
Args:
command: 指令
message: 消息内容
chat_id: 会话ID
user_id: 用户ID
Returns:
回复文本或None
"""
try:
plugin = PluginInterface.plugin_instances.get(command, None)
if plugin:
config.Log("Info", f"已找到插件注册指令: {command}, class: {plugin.__class__.__name__}")
return await plugin.callback(message, chat_id, user_id)
else:
return f"❌ 未识别指令: {command}"
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}处理指令异常: {e}{ConsoleFrontColor.RESET}")
return f"❌ 处理指令时出错: {str(e)}"