Skip to main content

qqbotsdk

CI PyPI Python Downloads License: MIT

QQ 官方机器人 Python SDK(Python 3.12+ / asyncio / aiohttp),基于 WebSocket 长连接接入。

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

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

安装

pip install qqbotsdk-py     # 或 uv add qqbotsdk-py

五分钟上手

.env 配好 APPID/APPSECRET,intents.toml 开启 GROUP_AND_C2C_EVENT = true:

from qqbotsdk import Bot
from qqbotsdk.api import BotApi
from qqbotsdk.payloads import GroupAtMessage

bot = Bot()


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


if __name__ == "__main__":
    bot.run()

特性

  • Bot 门面:bot.run() 一行启动,bot.call_api 通用 REST,bot.api 类型化 API 面
  • 断线自动重连(指数退避 + 心跳看门狗),重连时自动 RESUME 续传
  • 事件订阅由项目根目录的 intents.toml 按大类开关
  • Markdown 与内嵌按钮一等支持,respond_interaction() 回应按钮回调
  • 组件按类型注入 handler(自定义组件同样可注入),全 SDK 共享一个 HTTP 连接池

架构

flowchart TB
    subgraph 接入层
        WS["Connecter(长连接 / 重连 / 心跳看门狗)"]
    end
    subgraph 协议层
        WSP["Protocol(握手 / 会话 / 序列号)"]
    end
    subgraph 分发层
        Q[("EventQueue<br/>业务事件 op=0")]
        EE["EventEmitter(路由 / 参数注入 / 异常隔离)"]
    end
    subgraph 业务层
        API["BotApi(REST + ApiError)"]
        USER["用户 handler @bot.on(...)"]
    end
    QQ["QQ 开放平台"]

    WS -. "逐帧 on_frame" .-> WSP
    WS -->|"业务事件"| Q
    Q --> EE
    EE --> USER
    USER --> API
    API --> QQ
    WS --> QQ

组件在 Bot.start() 中装配,依赖关系与设计决策详见 docs/ARCHITECTURE.md。

开发

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

推送与 Pull Request 由 GitHub Actions 自动执行同样的检查(.github/workflows/ci.yml);打 v* 标签经 publish.yml 以 Trusted Publishing 发布到 PyPI。

文档站点基于 docsify,文档变更时自动部署到 GitHub Pages(.github/workflows/pages.yml,自定义域名 qqbotsdk.4i.hk);本地预览:

npx docsify-cli serve

Metadata

Release files for qqbotsdk-py 1.0.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 1.0.0
File Size Uploaded Provenance
qqbotsdk_py-1.0.0.tar.gz 41.3 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 93.4 kB

Release files / qqbotsdk_py-1.0.0.tar.gz

Download URL qqbotsdk_py-1.0.0.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bff7da24d853001b327fb583d22792854475ecc90679e3e843a667b1a1d090a6
BLAKE2b-256 checksum
How to use checksums
ecc29a5d68f5a653ff2c34fd04bfec05380a4afbc8ec410b0a6f26466c2049b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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

Download URL qqbotsdk_py-1.0.0-py3-none-any.whl
Size 52.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
971bd0b2c0209f3504142b6a1d308baf36fd890d5243f0263e5df1694a9e6d43
BLAKE2b-256 checksum
How to use checksums
a2c75639ecd7d1ba06a669e00725a152847795c4dc4002b640d15df201fe7117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for qqbotsdk-py 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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