mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 09:05:04 +08:00
add fastapi routes, adapters
This commit is contained in:
parent
4d242875d2
commit
ea21d6ffcc
@ -6,9 +6,9 @@ import importlib
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
|
|
||||||
from .log import logger
|
from nonebot.log import logger
|
||||||
from .config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
from .drivers import BaseDriver
|
from nonebot.drivers import BaseDriver
|
||||||
|
|
||||||
_driver: Optional[BaseDriver] = None
|
_driver: Optional[BaseDriver] = None
|
||||||
|
|
||||||
@ -48,4 +48,4 @@ def run(host: Optional[IPv4Address] = None,
|
|||||||
get_driver().run(host, port, *args, **kwargs)
|
get_driver().run(host, port, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
from .plugin import load_plugins, get_loaded_plugins
|
from nonebot.plugin import load_plugins, get_loaded_plugins
|
||||||
|
8
nonebot/adapters/__init__.py
Normal file
8
nonebot/adapters/__init__.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
|
class BaseBot(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
raise NotImplementedError
|
10
nonebot/adapters/coolq.py
Normal file
10
nonebot/adapters/coolq.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from nonebot.adapters import BaseBot
|
||||||
|
|
||||||
|
|
||||||
|
class Bot(BaseBot):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
@ -7,6 +7,9 @@ from ipaddress import IPv4Address
|
|||||||
|
|
||||||
class BaseDriver(object):
|
class BaseDriver(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def server_app(self):
|
def server_app(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@ -25,3 +28,9 @@ class BaseDriver(object):
|
|||||||
*args,
|
*args,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
async def _handle_http(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
async def _handle_ws_reverse(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys
|
import json
|
||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import Body, FastAPI, WebSocket
|
||||||
|
|
||||||
from . import BaseDriver
|
from nonebot.log import logger
|
||||||
|
from nonebot.drivers import BaseDriver
|
||||||
|
|
||||||
|
|
||||||
class Driver(BaseDriver):
|
class Driver(BaseDriver):
|
||||||
@ -24,6 +25,9 @@ class Driver(BaseDriver):
|
|||||||
|
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
|
self._server_app.post("/coolq/")(self._handle_http)
|
||||||
|
self._server_app.websocket("/coolq/ws")(self._handle_ws_reverse)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def server_app(self):
|
def server_app(self):
|
||||||
return self._server_app
|
return self._server_app
|
||||||
@ -76,3 +80,18 @@ class Driver(BaseDriver):
|
|||||||
debug=self.config.debug,
|
debug=self.config.debug,
|
||||||
log_config=LOGGING_CONFIG,
|
log_config=LOGGING_CONFIG,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
|
async def _handle_http(self, data: dict = Body(...)):
|
||||||
|
logger.debug(f"Received message: {data}")
|
||||||
|
return {"status": 200, "message": "success"}
|
||||||
|
|
||||||
|
async def _handle_ws_reverse(self, websocket: WebSocket):
|
||||||
|
await websocket.accept()
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = await websocket.receive_json()
|
||||||
|
except json.decoder.JSONDecodeError as e:
|
||||||
|
logger.exception(e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.debug(f"Received message: {data}")
|
||||||
|
@ -5,10 +5,10 @@ from functools import wraps
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import Type, List, Dict, Optional, Callable
|
from typing import Type, List, Dict, Optional, Callable
|
||||||
|
|
||||||
from .event import Event
|
from nonebot.event import Event
|
||||||
from .typing import Handler
|
from nonebot.typing import Handler
|
||||||
from .rule import Rule, user
|
from nonebot.rule import Rule, user
|
||||||
from .exception import PausedException, RejectedException, FinishedException
|
from nonebot.exception import PausedException, RejectedException, FinishedException
|
||||||
|
|
||||||
matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
|
matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from .log import logger
|
from nonebot.log import logger
|
||||||
from .event import Event
|
from nonebot.event import Event
|
||||||
from .matcher import matchers
|
from nonebot.matcher import matchers
|
||||||
|
|
||||||
|
|
||||||
async def handle_message(bot, event: Event):
|
async def handle_message(bot, event: Event):
|
||||||
|
@ -7,9 +7,9 @@ import importlib
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import Set, Dict, Type, Optional
|
from typing import Set, Dict, Type, Optional
|
||||||
|
|
||||||
from .log import logger
|
from nonebot.rule import Rule
|
||||||
from .rule import Rule
|
from nonebot.log import logger
|
||||||
from .matcher import Matcher
|
from nonebot.matcher import Matcher
|
||||||
|
|
||||||
plugins: Dict[str, "Plugin"] = {}
|
plugins: Dict[str, "Plugin"] = {}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
import re
|
import re
|
||||||
from typing import Union, Callable, Optional
|
from typing import Union, Callable, Optional
|
||||||
|
|
||||||
from .event import Event
|
from nonebot.event import Event
|
||||||
|
|
||||||
|
|
||||||
class Rule:
|
class Rule:
|
||||||
|
Loading…
Reference in New Issue
Block a user