2022-01-22 15:23:07 +08:00
|
|
|
|
"""
|
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 2
|
|
|
|
|
description: nonebot.adapters._bot 模块
|
|
|
|
|
"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
import abc
|
|
|
|
|
import asyncio
|
|
|
|
|
from functools import partial
|
|
|
|
|
from typing_extensions import Protocol
|
2021-12-25 17:44:53 +08:00
|
|
|
|
from typing import TYPE_CHECKING, Any, Set, Union, Optional
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
from nonebot.log import logger
|
|
|
|
|
from nonebot.config import Config
|
2021-11-28 02:29:23 +08:00
|
|
|
|
from nonebot.exception import MockApiException
|
2021-09-27 00:19:30 +08:00
|
|
|
|
from nonebot.typing import T_CalledAPIHook, T_CallingAPIHook
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from ._event import Event
|
2021-12-06 22:19:05 +08:00
|
|
|
|
from ._adapter import Adapter
|
2021-08-29 00:24:28 +08:00
|
|
|
|
from ._message import Message, MessageSegment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _ApiCall(Protocol):
|
|
|
|
|
async def __call__(self, **kwargs: Any) -> Any:
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Bot(abc.ABC):
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""Bot 基类。
|
|
|
|
|
|
|
|
|
|
用于处理上报消息,并提供 API 调用接口。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
adapter: 协议适配器实例
|
|
|
|
|
self_id: 机器人 ID
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_calling_api_hook: Set[T_CallingAPIHook] = set()
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""call_api 时执行的函数"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
_called_api_hook: Set[T_CalledAPIHook] = set()
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""call_api 后执行的函数"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2021-12-06 22:19:05 +08:00
|
|
|
|
def __init__(self, adapter: "Adapter", self_id: str):
|
2021-12-07 02:16:18 +08:00
|
|
|
|
self.adapter: "Adapter" = adapter
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""协议适配器实例"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
self.self_id: str = self_id
|
|
|
|
|
"""机器人 ID"""
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, name: str) -> _ApiCall:
|
|
|
|
|
return partial(self.call_api, name)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def type(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""协议适配器名称"""
|
2021-12-06 22:19:05 +08:00
|
|
|
|
return self.adapter.get_name()
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2021-12-06 22:19:05 +08:00
|
|
|
|
@property
|
|
|
|
|
def config(self) -> Config:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""全局 NoneBot 配置"""
|
2021-12-06 22:19:05 +08:00
|
|
|
|
return self.adapter.config
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
async def call_api(self, api: str, **data: Any) -> Any:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""调用机器人 API 接口,可以通过该函数或直接通过 bot 属性进行调用
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
api: API 名称
|
2022-01-20 14:49:46 +08:00
|
|
|
|
data: API 数据
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2021-08-29 00:24:28 +08:00
|
|
|
|
await bot.call_api("send_msg", message="hello world")
|
|
|
|
|
await bot.send_msg(message="hello world")
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
2021-11-28 02:29:23 +08:00
|
|
|
|
|
|
|
|
|
result: Any = None
|
|
|
|
|
skip_calling_api: bool = False
|
|
|
|
|
exception: Optional[Exception] = None
|
|
|
|
|
|
2021-08-29 00:24:28 +08:00
|
|
|
|
coros = list(map(lambda x: x(self, api, data), self._calling_api_hook))
|
|
|
|
|
if coros:
|
|
|
|
|
try:
|
|
|
|
|
logger.debug("Running CallingAPI hooks...")
|
|
|
|
|
await asyncio.gather(*coros)
|
2021-11-28 02:29:23 +08:00
|
|
|
|
except MockApiException as e:
|
|
|
|
|
skip_calling_api = True
|
|
|
|
|
result = e.result
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"Calling API {api} is cancelled. Return {result} instead."
|
|
|
|
|
)
|
2021-08-29 00:24:28 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running CallingAPI hook. "
|
2021-11-22 23:21:26 +08:00
|
|
|
|
"Running cancelled!</bg #f8bbd0></r>"
|
|
|
|
|
)
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2021-11-28 02:29:23 +08:00
|
|
|
|
if not skip_calling_api:
|
|
|
|
|
try:
|
2021-12-21 18:22:14 +08:00
|
|
|
|
result = await self.adapter._call_api(self, api, **data)
|
2021-11-28 02:29:23 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
exception = e
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
coros = list(
|
2021-11-22 23:21:26 +08:00
|
|
|
|
map(lambda x: x(self, exception, api, data, result), self._called_api_hook)
|
|
|
|
|
)
|
2021-08-29 00:24:28 +08:00
|
|
|
|
if coros:
|
|
|
|
|
try:
|
|
|
|
|
logger.debug("Running CalledAPI hooks...")
|
|
|
|
|
await asyncio.gather(*coros)
|
2021-11-28 02:29:23 +08:00
|
|
|
|
except MockApiException as e:
|
|
|
|
|
result = e.result
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"Calling API {api} result is mocked. Return {result} instead."
|
|
|
|
|
)
|
2021-08-29 00:24:28 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running CalledAPI hook. "
|
2021-11-22 23:21:26 +08:00
|
|
|
|
"Running cancelled!</bg #f8bbd0></r>"
|
|
|
|
|
)
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
if exception:
|
|
|
|
|
raise exception
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2021-11-22 23:21:26 +08:00
|
|
|
|
async def send(
|
2022-01-20 14:49:46 +08:00
|
|
|
|
self,
|
|
|
|
|
event: "Event",
|
|
|
|
|
message: Union[str, "Message", "MessageSegment"],
|
|
|
|
|
**kwargs: Any,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
) -> Any:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""调用机器人基础发送消息接口
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
event: 上报事件
|
|
|
|
|
message: 要发送的消息
|
2022-01-20 14:49:46 +08:00
|
|
|
|
kwargs: 任意额外参数
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def on_calling_api(cls, func: T_CallingAPIHook) -> T_CallingAPIHook:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""调用 api 预处理。
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-20 14:49:46 +08:00
|
|
|
|
插槽函数参数:
|
|
|
|
|
|
|
|
|
|
- bot: 当前 bot 对象
|
|
|
|
|
- api: 调用的 api 名称
|
|
|
|
|
- data: api 调用的参数字典
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
cls._calling_api_hook.add(func)
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def on_called_api(cls, func: T_CalledAPIHook) -> T_CalledAPIHook:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""调用 api 后处理。
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-20 14:49:46 +08:00
|
|
|
|
插槽函数参数:
|
|
|
|
|
|
|
|
|
|
- bot: 当前 bot 对象
|
|
|
|
|
- exception: 调用 api 时发生的错误
|
|
|
|
|
- api: 调用的 api 名称
|
|
|
|
|
- data: api 调用的参数字典
|
|
|
|
|
- result: api 调用的返回
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
cls._called_api_hook.add(func)
|
|
|
|
|
return func
|