Skip to content

配置说明

Client 构造函数

python
class Client(
    intents: Intents,
    timeout: int = 5,
    is_sandbox: bool = False,
    log_config: Union[str, dict] = None,
    log_format: str = None,
    log_level: int = None,
    bot_log: Union[bool, None] = True,
    ext_handlers: Union[dict, List[dict], bool] = True,
)

参数说明:

参数类型默认值说明
intentsIntents必填事件订阅位掩码,通过 Intents() 创建
timeoutint5HTTP 请求超时时间(秒)
is_sandboxboolFalse是否使用沙箱环境
log_configUnion[str, dict]None日志配置,详见日志系统
log_formatstrNone控制台输出格式
log_levelintNone控制台日志级别
bot_logUnion[bool, None]True是否启用机器人日志
ext_handlersUnion[dict, List[dict], bool]True额外日志处理器

源码位置: botpy/client.py 第 30-52 行

鉴权配置

SDK 使用 AppID + AppSecret 进行鉴权,通过 Token 类管理 access_token 的获取和自动刷新。

方式一:直接传入

python
client.run(appid="你的AppID", secret="你的AppSecret")

方式二:配置文件

推荐使用 YAML 配置文件管理:

yaml
# config.yaml
appid: "你的AppID"
secret: "你的AppSecret"

通过扩展模块读取:

python
from botpy.ext.cog_yaml import read

config = read("config.yaml")
client.run(appid=config["appid"], secret=config["secret"])

Token 类

python
class Token(app_id: str, secret: str)

源码位置: botpy/robot.py 第 22-81 行

参数类型说明
app_idstr机器人 AppID
secretstr机器人密钥

属性:

属性类型说明
app_idstr机器人 AppID
secretstr机器人密钥
access_tokenOptional[str]缓存的 access_token
expires_inintaccess_token 过期时间戳
Typestr授权类型("QQBot""Bearer"

方法:

方法返回说明
check_token()None检查 access_token 是否过期,过期则自动刷新
update_access_token()None向 QQ 平台请求新的 access_token
bot_token()Token返回自身(机器人身份的 token)
get_string()str获取授权头字符串,如 "QQBot xxx"
get_type()str获取授权类型

鉴权流程

1. Token(app_id, secret)  →  创建 Token 实例
2. check_token()          →  检查 access_token 是否过期
3. update_access_token()  →  POST https://bots.qq.com/app/getAppAccessToken
   ├── 请求体: { "appId": app_id, "clientSecret": secret }
   └── 响应:  { "access_token": "...", "expires_in": 7200 }
4. 请求头: Authorization: QQBot {access_token}
5. 过期前自动刷新(expires_in + 当前时间戳对比)

沙箱环境

SDK 支持沙箱环境,在沙箱中测试不会影响线上数据:

python
client = MyClient(intents=intents, is_sandbox=True)

沙箱环境使用不同的 API 域名:sandbox.api.sgroup.qq.com

生产环境 API 域名:api.sgroup.qq.com

botpy/http.pyRoute 类中通过 is_sandbox 标志位切换,相关代码位置:botpy/http.py 第 89-111 行

超时配置

可通过 timeout 参数设置 HTTP 请求超时时间(默认 5 秒):

python
client = MyClient(intents=intents, timeout=10)

超时作用于所有 HTTP 请求的 aiohttp.ClientTimeout,在 BotHttp.request() 方法中使用,源码位置:botpy/http.py 第 186 行

异步启动

SDK 支持异步上下文管理器方式启动,适用于需要与其他异步服务集成的场景:

python
async with MyClient(intents=intents) as client:
    await client.start(appid="123", secret="xxx")

也可以获取协程对象自行调控:

python
coro = await client.start(appid="123", secret="xxx", ret_coro=True)
# ... 自行管理协程执行

Client.start() 方法签名:

python
async def start(
    self,
    appid: str,
    secret: str,
    ret_coro: bool = False,
) -> Optional[Coroutine]

源码位置:botpy/client.py 第 139-159 行