PagerMaid-Pyro/pagermaid/web/api/command_alias.py

48 lines
1.2 KiB
Python
Raw Normal View History

2023-01-31 16:24:56 +00:00
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from pagermaid.common.alias import AliasManager
from pagermaid.web.api.utils import authentication
route = APIRouter()
2023-03-12 03:56:01 +00:00
@route.get(
"/command_alias", response_class=JSONResponse, dependencies=[authentication()]
)
2023-01-31 16:24:56 +00:00
async def get_command_alias():
alias = AliasManager()
return {
2023-03-12 03:56:01 +00:00
"status": 0,
"msg": "ok",
"data": {
"items": alias.get_all_alias_dict(),
},
2023-01-31 16:24:56 +00:00
}
2023-03-12 03:56:01 +00:00
@route.post(
"/command_alias", response_class=JSONResponse, dependencies=[authentication()]
)
2023-01-31 16:24:56 +00:00
async def add_command_alias(data: dict):
2023-03-12 03:56:01 +00:00
data = data["items"]
2023-01-31 16:24:56 +00:00
try:
await AliasManager.save_from_web(data)
2023-03-12 03:56:01 +00:00
return {"status": 0, "msg": "命令别名保存成功"}
2023-01-31 16:24:56 +00:00
except Exception:
2023-03-12 03:56:01 +00:00
return {"status": 1, "msg": "命令别名保存失败"}
2023-01-31 16:24:56 +00:00
2023-03-12 03:56:01 +00:00
@route.get(
"/test_command_alias", response_class=JSONResponse, dependencies=[authentication()]
)
2023-01-31 16:24:56 +00:00
async def test_command_alias(message: str):
alias = AliasManager()
return {
2023-03-12 03:56:01 +00:00
"status": 0,
"msg": "测试成功",
"data": {
"new_msg": alias.test_alias(message),
},
2023-01-31 16:24:56 +00:00
}