mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-25 01:25:04 +08:00
271 lines
3.4 KiB
Markdown
271 lines
3.4 KiB
Markdown
---
|
||
contentSidebar: true
|
||
sidebarDepth: 0
|
||
---
|
||
|
||
# NoneBot.adapters 模块
|
||
|
||
## 协议适配基类
|
||
|
||
各协议请继承以下基类,并使用 `driver.register_adapter` 注册适配器
|
||
|
||
|
||
## _class_ `Bot`
|
||
|
||
基类:`abc.ABC`
|
||
|
||
Bot 基类。用于处理上报消息,并提供 API 调用接口。
|
||
|
||
|
||
### _abstract_ `__init__(driver, connection_type, config, self_id, *, websocket=None)`
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `driver: Driver`: Driver 对象
|
||
|
||
|
||
* `connection_type: str`: http 或者 websocket
|
||
|
||
|
||
* `config: Config`: Config 对象
|
||
|
||
|
||
* `self_id: str`: 机器人 ID
|
||
|
||
|
||
* `websocket: Optional[WebSocket]`: Websocket 连接对象
|
||
|
||
|
||
|
||
### `driver`
|
||
|
||
Driver 对象
|
||
|
||
|
||
### `connection_type`
|
||
|
||
连接类型
|
||
|
||
|
||
### `config`
|
||
|
||
Config 配置对象
|
||
|
||
|
||
### `self_id`
|
||
|
||
机器人 ID
|
||
|
||
|
||
### `websocket`
|
||
|
||
Websocket 连接对象
|
||
|
||
|
||
### _abstract property_ `type`
|
||
|
||
Adapter 类型
|
||
|
||
|
||
### _abstract async classmethod_ `check_permission(driver, connection_type, headers, body)`
|
||
|
||
|
||
* **说明**
|
||
|
||
检查连接请求是否合法的函数,如果合法则返回当前连接 `唯一标识符`,通常为机器人 ID;如果不合法则抛出 `RequestDenied` 异常。
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `driver: Driver`: Driver 对象
|
||
|
||
|
||
* `connection_type: str`: 连接类型
|
||
|
||
|
||
* `headers: dict`: 请求头
|
||
|
||
|
||
* `body: Optional[dict]`: 请求数据,WebSocket 连接该部分为空
|
||
|
||
|
||
|
||
* **返回**
|
||
|
||
|
||
* `str`: 连接唯一标识符
|
||
|
||
|
||
|
||
* **异常**
|
||
|
||
|
||
* `RequestDenied`: 请求非法
|
||
|
||
|
||
|
||
### _abstract async_ `handle_message(message)`
|
||
|
||
|
||
* **说明**
|
||
|
||
处理上报消息的函数,转换为 `Event` 事件后调用 `nonebot.message.handle_event` 进一步处理事件。
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `message: dict`: 收到的上报消息
|
||
|
||
|
||
|
||
### _abstract async_ `call_api(api, **data)`
|
||
|
||
|
||
* **说明**
|
||
|
||
调用机器人 API 接口,可以通过该函数或直接通过 bot 属性进行调用
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `api: str`: API 名称
|
||
|
||
|
||
* `**data`: API 数据
|
||
|
||
|
||
|
||
* **示例**
|
||
|
||
|
||
```python
|
||
await bot.call_api("send_msg", message="hello world"})
|
||
await bot.send_msg(message="hello world")
|
||
```
|
||
|
||
|
||
### _abstract async_ `send(event, message, **kwargs)`
|
||
|
||
|
||
* **说明**
|
||
|
||
调用机器人基础发送消息接口
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `event: Event`: 上报事件
|
||
|
||
|
||
* `message: Union[str, Message, MessageSegment]`: 要发送的消息
|
||
|
||
|
||
* `**kwargs`
|
||
|
||
|
||
|
||
## _class_ `Event`
|
||
|
||
基类:`abc.ABC`, `pydantic.main.BaseModel`
|
||
|
||
|
||
## _class_ `MessageSegment`
|
||
|
||
基类:`abc.ABC`
|
||
|
||
消息段基类
|
||
|
||
|
||
### `type`
|
||
|
||
|
||
* 类型: `str`
|
||
|
||
|
||
* 说明: 消息段类型
|
||
|
||
|
||
### `data`
|
||
|
||
|
||
* 类型: `Dict[str, Union[str, list]]`
|
||
|
||
|
||
* 说明: 消息段数据
|
||
|
||
|
||
## _class_ `Message`
|
||
|
||
基类:`list`, `abc.ABC`
|
||
|
||
消息数组
|
||
|
||
|
||
### `__init__(message=None, *args, **kwargs)`
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `message: Union[str, dict, list, BaseModel, MessageSegment, Message]`: 消息内容
|
||
|
||
|
||
|
||
### `append(obj)`
|
||
|
||
|
||
* **说明**
|
||
|
||
添加一个消息段到消息数组末尾
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `obj: Union[str, MessageSegment]`: 要添加的消息段
|
||
|
||
|
||
|
||
### `extend(obj)`
|
||
|
||
|
||
* **说明**
|
||
|
||
拼接一个消息数组或多个消息段到消息数组末尾
|
||
|
||
|
||
|
||
* **参数**
|
||
|
||
|
||
* `obj: Union[Message, Iterable[MessageSegment]]`: 要添加的消息数组
|
||
|
||
|
||
|
||
### `reduce()`
|
||
|
||
|
||
* **说明**
|
||
|
||
缩减消息数组,即按 MessageSegment 的实现拼接相邻消息段
|
||
|
||
|
||
|
||
### `extract_plain_text()`
|
||
|
||
|
||
* **说明**
|
||
|
||
提取消息内纯文本消息
|