2020-09-29 23:10:29 +08:00
|
|
|
|
---
|
|
|
|
|
contentSidebar: true
|
|
|
|
|
sidebarDepth: 0
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# NoneBot.adapters 模块
|
|
|
|
|
|
2020-10-01 23:52:56 +08:00
|
|
|
|
## 协议适配基类
|
|
|
|
|
|
|
|
|
|
各协议请继承以下基类,并使用 `driver.register_adapter` 注册适配器
|
|
|
|
|
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2020-12-07 00:52:26 +08:00
|
|
|
|
## _class_ `Bot`
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
|
|
|
|
基类:`abc.ABC`
|
|
|
|
|
|
2020-10-01 23:52:56 +08:00
|
|
|
|
Bot 基类。用于处理上报消息,并提供 API 调用接口。
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `driver`
|
|
|
|
|
|
|
|
|
|
Driver 对象
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### `config`
|
|
|
|
|
|
|
|
|
|
Config 配置对象
|
|
|
|
|
|
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
### _abstract_ `__init__(connection_type, self_id, *, websocket=None)`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
* **参数**
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
|
|
|
|
|
* `connection_type: str`: http 或者 websocket
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
* `self_id: str`: 机器人 ID
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
* `websocket: Optional[WebSocket]`: Websocket 连接对象
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `connection_type`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
连接类型
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `self_id`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
机器人 ID
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `websocket`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
Websocket 连接对象
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract property_ `type`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
Adapter 类型
|
2021-01-21 07:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _classmethod_ `register(driver, config)`
|
2021-01-21 07:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
register 方法会在 driver.register_adapter 时被调用,用于初始化相关配置
|
|
|
|
|
|
2021-01-21 07:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
2020-11-13 01:46:26 +08:00
|
|
|
|
### _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`: 请求非法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-01 23:52:56 +08:00
|
|
|
|
### _abstract async_ `handle_message(message)`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
处理上报消息的函数,转换为 `Event` 事件后调用 `nonebot.message.handle_event` 进一步处理事件。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `message: dict`: 收到的上报消息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract async_ `call_api(api, **data)`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
调用机器人 API 接口,可以通过该函数或直接通过 bot 属性进行调用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `api: str`: API 名称
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `**data`: API 数据
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **示例**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
await bot.call_api("send_msg", message="hello world")
|
|
|
|
|
await bot.send_msg(message="hello world")
|
|
|
|
|
```
|
|
|
|
|
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
### _abstract async_ `send(event, message, **kwargs)`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
调用机器人基础发送消息接口
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* `event: Event`: 上报事件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `message: Union[str, Message, MessageSegment]`: 要发送的消息
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `**kwargs`
|
|
|
|
|
|
|
|
|
|
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
## _class_ `MessageSegment`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
基类:`abc.ABC`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
消息段基类
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `type`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* 类型: `str`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* 说明: 消息段类型
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `data`
|
2020-12-31 17:58:09 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* 类型: `Dict[str, Union[str, list]]`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* 说明: 消息段数据
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
## _class_ `Message`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
基类:`list`, `abc.ABC`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
消息数组
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `__init__(message=None, *args, **kwargs)`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **参数**
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `message: Union[str, list, dict, MessageSegment, Message, Any]`: 消息内容
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `append(obj)`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
添加一个消息段到消息数组末尾
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **参数**
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `obj: Union[str, MessageSegment]`: 要添加的消息段
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `extend(obj)`
|
2020-12-31 17:58:09 +08:00
|
|
|
|
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **说明**
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
拼接一个消息数组或多个消息段到消息数组末尾
|
2020-12-31 17:58:09 +08:00
|
|
|
|
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **参数**
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
|
|
|
|
|
* `obj: Union[Message, Iterable[MessageSegment]]`: 要添加的消息数组
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `reduce()`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **说明**
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
缩减消息数组,即按 MessageSegment 的实现拼接相邻消息段
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### `extract_plain_text()`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
提取消息内纯文本消息
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
## _class_ `Event`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
基类:`abc.ABC`, `pydantic.main.BaseModel`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
Event 基类。提供获取关键信息的方法,其余信息可直接获取。
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_type()`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件类型的方法,类型通常为 NoneBot 内置的四种类型。
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `Literal["message", "notice", "request", "meta_event"]`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_event_name()`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件名称的方法。
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `str`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_event_description()`
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件描述的方法,通常为事件具体内容。
|
2020-12-19 15:31:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
* `str`
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `get_log_string()`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
* **说明**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件日志信息的方法,通常你不需要修改这个方法,只有当希望 NoneBot 隐藏该事件日志时,可以抛出 `NoLogException` 异常。
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
* **返回**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `str`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **异常**
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
|
|
|
|
|
* `NoLogException`
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_user_id()`
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
获取事件主体 id 的方法,通常是用户 id 。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `str`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2020-10-01 23:52:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_session_id()`
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取会话 id 的方法,用于判断当前事件属于哪一个会话,通常是用户 id、群组 id 组合。
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **返回**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `str`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `get_message()`
|
2020-09-29 23:10:29 +08:00
|
|
|
|
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件消息内容的方法。
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **返回**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* `Message`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### `get_plaintext()`
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取消息纯文本的方法,通常不需要修改,默认通过 `get_message().extract_plain_text` 获取。
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **返回**
|
2020-10-03 18:18:43 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
|
|
|
|
|
* `str`
|
2021-02-17 23:23:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
### _abstract_ `is_tome()`
|
2021-02-17 23:23:23 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
* **说明**
|
2021-02-17 23:23:23 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
获取事件是否与机器人有关的方法。
|
2021-02-17 23:23:23 +08:00
|
|
|
|
|
2021-02-19 15:24:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `bool`
|