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
|
|
|
|
|
from telegram.ext import CallbackContext
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
class Example(BasePlugins):
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def create_conversation_handler(service: BaseService):
|
|
|
|
|
example = Example(service)
|
|
|
|
|
return CommandHandler('example', example.command_start)
|
|
|
|
|
|
|
|
|
|
@conversation_error_handler
|
2022-06-22 13:33:07 +00:00
|
|
|
|
@restricts()
|
2022-06-19 09:15:12 +00:00
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
|
await message.reply_text("Example")
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 注意
|
|
|
|
|
|
|
|
|
|
plugins 模块下的类需要继承 `BasePlugins`
|
|
|
|
|
|
|
|
|
|
plugins 模块下的类必须提供 `create_conversation_handler` 静态函数作为构建相应会话过程给 `handle.py`
|
|
|
|
|
|
|
|
|
|
在函数注册为命令处理过程(如 `CommandHandler` )需要添加 `conversation_error_handler` 修饰器作为错误统一处理
|
|
|
|
|
|
2022-06-22 13:33:07 +00:00
|
|
|
|
必要的函数必须捕获异常后通知用户或者直接抛出异常
|
|
|
|
|
|
|
|
|
|
入口函数必须使用 `@restricts()` 修饰器 防止洪水攻击
|
|
|
|
|
|
|
|
|
|
**注意:`@restricts()` 修饰器带参,必须带括号,否则会出现调用错误**
|
|
|
|
|
|