refactor: Remove key attribute from PushTarget and rename groups table name

This commit is contained in:
LWR
2023-02-18 00:29:02 +08:00
parent 063608a3ad
commit d85c18a62a
6 changed files with 99 additions and 96 deletions
+51 -15
View File
@@ -3,10 +3,11 @@ from typing import Optional
import aiohttp
from aiohttp import web
from aiohttp.web_routedef import RouteTableDef
from graia.ariadne.exception import UnknownTarget
from loguru import logger
from .datasource import DataSource
from .model import Message
from .model import Message, PushType
from ..exception import DataSourceException
from ..utils import config
@@ -14,21 +15,55 @@ routes = web.RouteTableDef()
datasource: Optional[DataSource] = None
@routes.get("/send/{key}/{message}")
async def send(request: aiohttp.web.Request) -> aiohttp.web.Response:
key = request.match_info['key']
message = request.match_info['message']
@routes.get("/send/{type}/{key}/{message}")
async def send(request: aiohttp.web.Request, qq: int = None) -> aiohttp.web.Response:
if len(datasource.bots) == 1:
bot = datasource.get_bot()
else:
if qq is None:
qq = config.get("HTTP_API_DEAFULT_BOT")
if qq is None:
logger.warning("HTTP API 推送失败, 多 Bot 推送时使用 HTTP API 需填写 HTTP_API_DEAFULT_BOT 配置项")
return web.Response(text="fail")
try:
bot = datasource.get_bot(qq)
except DataSourceException:
logger.warning("HTTP API 推送失败, 填写的 HTTP_API_DEAFULT_BOT 配置项不正确")
return web.Response(text="fail")
if not str(request.match_info['key']).isdigit():
logger.warning("HTTP API 推送失败, 传入的 QQ 或群号格式不正确")
return web.Response(text="fail")
type_map = {
"friend": PushType.Friend,
"group": PushType.Group
}
_type = type_map.get(str(request.match_info['type']), None)
if _type is None:
logger.warning("HTTP API 推送失败, 传入的推送类型格式不正确")
return web.Response(text="fail")
key = int(request.match_info['key'])
message = Message(id=key, content=str(request.match_info['message']), type=_type)
try:
target = datasource.get_target_by_key(key)
bot = datasource.get_bot_by_key(key)
msg = Message(id=target.id, content=message, type=target.type)
await bot.send_message(msg)
return web.Response(text="success")
except DataSourceException:
logger.warning(f"HTTP API 推送失败, 不存在的推送 key: {key}")
await bot.send_message(message)
except UnknownTarget:
pass
return web.Response(text="success")
@routes.get("/send/{bot}/{type}/{key}/{message}")
async def send_by_bot(request: aiohttp.web.Request) -> aiohttp.web.Response:
if not str(request.match_info['bot']).isdigit():
logger.warning("HTTP API 推送失败, 传入的 Bot QQ 格式不正确")
return web.Response(text="fail")
return await send(request, int(request.match_info['bot']))
def get_routes() -> RouteTableDef:
"""
@@ -43,6 +78,7 @@ def get_routes() -> RouteTableDef:
async def http_init(source: DataSource):
global datasource
datasource = source
port = config.get("HTTP_API_PORT")
logger.info("开始启动 HTTP API 推送服务")
@@ -50,10 +86,10 @@ async def http_init(source: DataSource):
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', config.get("HTTP_API_PORT"))
site = web.TCPSite(runner, 'localhost', port)
try:
await site.start()
except OSError:
logger.error(f"设定的 HTTP API 端口 {config.get('HTTP_API_PORT')} 已被占用, HTTP API 推送服务启动失败")
logger.error(f"设定的 HTTP API 端口 {port} 已被占用, HTTP API 推送服务启动失败")
return
logger.success("成功启动 HTTP API 推送服务")
logger.success(f"成功启动 HTTP API 推送服务: http://localhost:{port}")