nonebot2/docs/api/drivers/README.md
2021-05-31 00:27:31 +08:00

5.5 KiB

contentSidebar sidebarDepth
true 0

NoneBot.drivers 模块

后端驱动适配基类

各驱动请继承以下基类

class Driver

基类:abc.ABC

Driver 基类。

_adapters

  • 类型

    Dict[str, Type[Bot]]

  • 说明

    已注册的适配器列表

_bot_connection_hook

  • 类型

    Set[T_BotConnectionHook]

  • 说明

    Bot 连接建立时执行的函数

_bot_disconnection_hook

  • 类型

    Set[T_BotDisconnectionHook]

  • 说明

    Bot 连接断开时执行的函数

abstract __init__(env, config)

  • 参数

    • env: Env: 包含环境信息的 Env 对象

    • config: Config: 包含配置信息的 Config 对象

env

  • 类型

    str

  • 说明

    环境名称

config

  • 类型

    Config

  • 说明

    配置对象

_clients

  • 类型

    Dict[str, Bot]

  • 说明

    已连接的 Bot

property bots

  • 类型

    Dict[str, Bot]

  • 说明

    获取当前所有已连接的 Bot

register_adapter(name, adapter, **kwargs)

  • 说明

    注册一个协议适配器

  • 参数

    • name: str: 适配器名称,用于在连接时进行识别

    • adapter: Type[Bot]: 适配器 Class

abstract property type

驱动类型名称

abstract property logger

驱动专属 logger 日志记录器

abstract run(host=None, port=None, *args, **kwargs)

  • 说明

    启动驱动框架

  • 参数

    • host: Optional[str]: 驱动绑定 IP

    • post: Optional[int]: 驱动绑定端口

    • *args

    • **kwargs

abstract on_startup(func)

注册一个在驱动启动时运行的函数

abstract on_shutdown(func)

注册一个在驱动停止时运行的函数

on_bot_connect(func)

  • 说明

    装饰一个函数使他在 bot 通过 WebSocket 连接成功时执行。

  • 函数参数

    • bot: Bot: 当前连接上的 Bot 对象

on_bot_disconnect(func)

  • 说明

    装饰一个函数使他在 bot 通过 WebSocket 连接断开时执行。

  • 函数参数

    • bot: Bot: 当前连接上的 Bot 对象

_bot_connect(bot)

在 WebSocket 连接成功后,调用该函数来注册 bot 对象

_bot_disconnect(bot)

在 WebSocket 连接断开后,调用该函数来注销 bot 对象

class ReverseDriver

基类:nonebot.drivers.Driver

Reverse Driver 基类。将后端框架封装,以满足适配器使用。

abstract property server_app

驱动 APP 对象

abstract property asgi

驱动 ASGI 对象

abstract async _handle_http(*args, **kwargs)

用于处理 HTTP 类型请求的函数

abstract async _handle_ws_reverse(*args, **kwargs)

用于处理 WebSocket 类型请求的函数

class HTTPRequest

基类:object

HTTP 请求封装。参考 asgi http scope

property type

Always http

property scope

Raw scope from asgi.

The connection scope information, a dictionary that contains at least a type key specifying the protocol that is incoming.

property http_version

One of "1.0", "1.1" or "2".

property method

The HTTP method name, uppercased.

property schema

URL scheme portion (likely "http" or "https"). Optional (but must not be empty); default is "http".

property path

HTTP request target excluding any query string, with percent-encoded sequences and UTF-8 byte sequences decoded into characters.

property query_string

URL portion after the ?, percent-encoded.

property headers

An iterable of [name, value] two-item iterables, where name is the header name, and value is the header value.

Order of header values must be preserved from the original HTTP request; order of header names is not important.

Duplicates are possible and must be preserved in the message as received.

Header names must be lowercased.

property body

Body of the request.

Optional; if missing defaults to b"".

If more_body is set, treat as start of body and concatenate on further chunks.

class HTTPResponse

基类:object

HTTP 响应封装。参考 asgi http scope

status

HTTP status code.

headers

An iterable of [name, value] two-item iterables, where name is the header name, and value is the header value.

Order must be preserved in the HTTP response.

Header names must be lowercased.

Optional; if missing defaults to an empty list.

body

HTTP body content.

Optional; if missing defaults to None.

property type

Always http

class WebSocket

基类:object

WebSocket 连接封装。参考 asgi websocket scope

abstract __init__(websocket)

  • 参数

    • websocket: Any: WebSocket 连接对象

property websocket

WebSocket 连接对象

abstract property closed

  • 类型

    bool

  • 说明

    连接是否已经关闭

abstract async accept()

接受 WebSocket 连接请求

abstract async close(code)

关闭 WebSocket 连接请求

abstract async receive()

接收一条 WebSocket 信息

abstract async send(data)

发送一条 WebSocket 信息