2022-06-19 09:15:12 +00:00
|
|
|
|
# plugins 目录
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
2022-06-19 09:15:12 +00:00
|
|
|
|
## 说明
|
|
|
|
|
|
|
|
|
|
该目录仅限处理交互层和业务层数据交换的任务
|
|
|
|
|
|
|
|
|
|
如有任何新业务接口,请转到 `service` 目录添加
|
|
|
|
|
|
|
|
|
|
如有任何API请求接口,请转到 `model` 目录添加
|
|
|
|
|
|
|
|
|
|
## 基础代码
|
|
|
|
|
|
|
|
|
|
``` python
|
|
|
|
|
from telegram import Update
|
|
|
|
|
|
2022-06-26 06:17:43 +00:00
|
|
|
|
from manager import listener_plugins_class
|
2022-06-22 13:33:07 +00:00
|
|
|
|
from plugins.base import BasePlugins, restricts
|
2022-06-19 09:15:12 +00:00
|
|
|
|
from plugins.errorhandler import conversation_error_handler
|
2022-07-07 01:36:34 +00:00
|
|
|
|
from utils.base import PaimonContext
|
2022-06-19 09:15:12 +00:00
|
|
|
|
|
2022-06-26 06:17:43 +00:00
|
|
|
|
@listener_plugins_class()
|
2022-06-19 09:15:12 +00:00
|
|
|
|
class Example(BasePlugins):
|
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def create_handlers(cls):
|
|
|
|
|
example = cls()
|
2022-06-26 06:17:43 +00:00
|
|
|
|
return [CommandHandler('example', example.command_start)]
|
2022-06-19 09:15:12 +00:00
|
|
|
|
|
|
|
|
|
@conversation_error_handler
|
2022-06-22 13:33:07 +00:00
|
|
|
|
@restricts()
|
2022-07-07 01:36:34 +00:00
|
|
|
|
async def command_start(self, update: Update, context: PaimonContext) -> None:
|
2022-06-19 09:15:12 +00:00
|
|
|
|
await message.reply_text("Example")
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 注意
|
|
|
|
|
|
|
|
|
|
plugins 模块下的类需要继承 `BasePlugins`
|
|
|
|
|
|
2022-07-11 08:56:46 +00:00
|
|
|
|
plugins 模块下的类必须提供 `create_handlers` 类方法作为构建相应处理程序给 `handle.py`
|
2022-06-19 09:15:12 +00:00
|
|
|
|
|
|
|
|
|
在函数注册为命令处理过程(如 `CommandHandler` )需要添加 `conversation_error_handler` 修饰器作为错误统一处理
|
|
|
|
|
|
2022-06-22 13:33:07 +00:00
|
|
|
|
必要的函数必须捕获异常后通知用户或者直接抛出异常
|
|
|
|
|
|
|
|
|
|
入口函数必须使用 `@restricts()` 修饰器 防止洪水攻击
|
|
|
|
|
|
2022-06-26 06:17:43 +00:00
|
|
|
|
我也不知道从那个版本开始 `plugins` 文件夹下的全部模块无需再次修改 `handler` 文件实现注册处理程序
|
|
|
|
|
|
|
|
|
|
只需在构建的类前加上 `@listener_plugins_class()` 修饰器即可
|
|
|
|
|
|
2022-06-22 13:33:07 +00:00
|
|
|
|
**注意:`@restricts()` 修饰器带参,必须带括号,否则会出现调用错误**
|
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
|
如果 `service` 需要全局共用,可以参考 `daily_note.py` 代码
|