Skip to main content

qqbotsdk

QQ 官方机器人 Python SDK(Python 3.12+ / asyncio / aiohttp),支持 WebSocket 与 Webhook 两种接入方式。

📖 在线文档:https://qqbotsdk.4i.hk/

使用指南 · API 参考 · 架构 · 示例 · 更新日志

架构

flowchart TB
    subgraph 接入层
        WS["WebsocketConnecter(ws 长连接 / 重连 / 心跳看门狗)"]
        WH["WebhookConnecter(http 回调 / 验签 / 去重)"]
    end
    subgraph 协议层
        WSP["WebsocketProtocol(握手 / 会话 / 序列号)"]
        WHP["WebhookProtocol(op=13 验证应答)"]
    end
    subgraph 分发层
        Q[("EventQueue<br/>业务事件 op=0")]
        EE["EventEmitter(路由 / 参数注入 / 异常隔离)"]
    end
    subgraph 业务层
        API["BotApi(REST + ApiError)"]
        USER["用户 handler @ee.on(...)"]
    end
    QQ["QQ 开放平台"]

    WS -. "逐帧 on_frame" .-> WSP
    WH -. "op=13 逐帧 on_frame" .-> WHP
    WS -->|"业务事件"| Q
    WH -->|"业务事件"| Q
    Q --> EE
    EE --> USER
    USER --> API
    API --> QQ
    WS --> QQ

组件在 run_loop 中显式装配,依赖关系与设计决策详见 docs/ARCHITECTURE.md。

特性

  • 断线自动重连(指数退避 + 心跳看门狗),重连时自动 RESUME 续传
  • Webhook 接入自带 Ed25519 验签、op=13 验证应答与事件去重
  • 事件订阅由项目根目录的 intents.toml 按大类开关
  • Markdown 与内嵌按钮一等支持,respond_interaction() 回应按钮回调
  • 组件显式装配(无全局单例),全 SDK 共享一个 HTTP 连接池

配置

.env(APPID/APPSECRET 必填,缺失启动即报错):

APPID=你的AppID
APPSECRET=你的AppSecret
BASE_URL=https://api.bot.qq.com   # 可选
TIMEOUT=5000                      # 可选,毫秒
CONNECTER=websocket               # websocket 或 webhook

# 仅 webhook 接入时
WEBHOOK_HOST=0.0.0.0
WEBHOOK_PORT=8080
WEBHOOK_PATH=/qqbot/webhook

使用

from qqbotsdk import EventEmitter, main
from qqbotsdk.payloads import GroupAtMessage
from qqbotsdk.api import BotApi

ee = EventEmitter()

# handler 形参按标注注入 payload 与 BotApi 等组件
@ee.on("GROUP_AT_MESSAGE_CREATE")
async def on_group_message(msg: GroupAtMessage, api: BotApi):
    # 被动回复带 msg_id;多次回复递增 msg_seq;失败抛 ApiError
    await api.post_group_message(
        msg.group_openid,
        content=f"你说:{msg.content}",
        msg_id=msg.id,
    )

if __name__ == "__main__":
    main(ee)

运行:

uv run python main.py

Webhook 接入:.env 中 CONNECTER 改为 webhook,再把回调地址配置到 QQ 开放平台(仅支持 80/443/8080/8443 端口,需公网 HTTPS)。

完整示例见 examples/,更多写法见使用指南。

开发

uv run pytest    # 单元测试
uv run ruff check src tests examples && uv run pyright

推送与 Pull Request 由 GitHub Actions 自动执行同样的检查(.github/workflows/ci.yml)。

文档站点基于 docsify,文档变更时自动发布(.github/workflows/deploy-docs.yml);本地预览:

npx docsify-cli serve

待完善

  • 更多 OpenAPI 封装(按需在 api.py 的 BotApi 上加薄封装即可)

Metadata

Release files for qqbotsdk-py 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qqbotsdk-py 0.1.0
File Size Uploaded
qqbotsdk_py-0.1.0.tar.gz 38.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for qqbotsdk-py 0.1.0
File Interpreter ABI Platform
qqbotsdk_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 89.1 kB

Release files / qqbotsdk_py-0.1.0.tar.gz

Download URL qqbotsdk_py-0.1.0.tar.gz
Size 38.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3ce179910a85948f1334d120333f737eb36cbed22ef765924455049c06544f5d
BLAKE2b-256 checksum
How to use checksums
cc4cf4c44f394f2a1677a80853133f3ff24cecd6297ce047e6f220452677ed85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / qqbotsdk_py-0.1.0-py3-none-any.whl

Download URL qqbotsdk_py-0.1.0-py3-none-any.whl
Size 50.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aed99cee1ca58af96ec83e49cc2ed98cb8deaea99a2bb1e672111a1b0981985e
BLAKE2b-256 checksum
How to use checksums
e1863c18042b6ed97c67dc3f250ef5a0e79126cc526bfc105fc008405961c1e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page