Herta-villa / Herta-villa-SDK

大别野「黑塔」Python SDK
MIT License
8 stars 1 forks source link

:sparkles: 自定义后端支持 #11

Closed MingxuanGame closed 1 year ago

MingxuanGame commented 1 year ago

简介

此 PR 引入了后端(Backend)代替了 WebHook 服务器的概念。后端是对 WebHook 服务器的抽象包装。

目前 Herta SDK 内置了 AIOHTTP 和 FastAPI 后端,默认使用 AIOHTTP 后端.

FastAPI 后端需要安装扩展 fastapi

pip install herta-villa-sdk[fastapi]

设置后端

run 函数提供了一个参数 backend_class,用来传入后端类

例如: 替换后端为 FastAPI

from __future__ import annotations

from hertavilla import VillaBot, run
from hertavilla.server.fastapi import FastAPIBackend

bot = VillaBot(
    "bot_id",
    "bot_secret",
    "/",
)

-run(bot)
+run(bot, backend_class=FastAPIBackend)

运行时获取后端和运行前修改后端

运行时可以通过 get_backend() 获取后端,运行前(run 之前)通过 init_backend 设置后端

Warning

使用 init_backend 设置后端后,run 的参数 backend_class 就会失效

其他

详见 hertavilla.server