QQPyBot
QQ 开放平台的 Python Webhook SDK。用 FastAPI 接收 QQ 事件,使用装饰器注册命令,并通过异步客户端调用 QQ REST API。
快速开始
PyPI 首发完成后,使用 pip install QQPyBot uvicorn。也可以克隆本仓库后运行 python -m pip install -e . uvicorn。运行环境需要 Python 3.11 以上及提供 Ed25519 的 OpenSSL。Windows 版 CPython 可使用自带的 OpenSSL。
把下面的示例保存为 main.py。
import os
from qqpybot import Bot, Context
bot = Bot(os.environ["QQBOT_APP_ID"], os.environ["QQBOT_APP_SECRET"])
@bot.command("你好", aliases=("hi",), events={"GROUP_MESSAGE_CREATE", "GROUP_AT_MESSAGE_CREATE"})
async def hello(ctx: Context):
await ctx.reply(f"你好,{ctx.args or '朋友'}!")
app = bot.create_app()
执行 python -m uvicorn main:app --host 0.0.0.0 --port 8080。在 QQ 开放平台填写对外的 HTTPS 回调地址 https://你的域名/qq/callback。可在已有 FastAPI 中使用 app.include_router(bot.router)。
勾选 GROUP_MESSAGE_CREATE 并为机器人启用接收所有消息。群里直接发送 你好 小明 或 /hi 小明。只订阅 AT 事件的群,用 @机器人 你好 小明。QQ 平台管理的账号注册、事件订阅、权限和回调地址配置请参阅官方接入指南。
事件与 REST
@bot.on_event("GROUP_ADD_ROBOT")
async def joined(ctx: Context):
print("已加入群聊", ctx.event.group_openid)
@bot.on_event("*")
async def delivered(ctx: Context):
print(ctx.event.type, ctx.event.event_id)
全事件示例包含逐项订阅的调用方式。也可运行 uvicorn examples.all_events:app 观察官方回调。事件数据通过 ctx.event.data 取得。
await bot.api.send_group_message("群 OpenID", msg_type=0, content="晚上好")
await bot.api.call("v2_groups_group_openid_info_get", group_openid="群 OpenID")
await bot.api.request("GET", "/users/@me")
REST 目录列出官网自动生成的接口及参数示例。登录凭证由 SDK 缓存,API 失败时可从 QQAPIError 读取 HTTP 状态、业务码和 Trace ID。管理、主动消息及其他受限接口取决于应用权限。
回调会先验签、应答 QQ,随后在当前进程执行已注册的处理器。同一进程内会过滤近期重复事件。需要跨进程去重或可靠重放的业务,应在业务层持久化事件 ID。
开发与发布
在项目的虚拟环境运行 python -m pip install -e .,然后执行 python -m unittest discover -s tests -v。执行 python scripts/sync_catalog.py 可将官方接口目录同步到包内。
发布前在 PyPI 建立 Trusted Publisher,项目名填 QQPyBot,GitHub Owner 填 Murphy08,仓库填 QQPyBot,工作流填 publish.yml,环境填 pypi。更新 pyproject.toml 中的版本,推送对应 vX.Y.Z 标签即可构建并发布。
Metadata
Release files for QQPyBot 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| qqpybot-0.1.0.tar.gz | 12.9 kB | With provenance | Details for qqpybot-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| qqpybot-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for qqpybot-0.1.0-py3-none-any.whl |
Total release size: 26.3 kB
Release files / qqpybot-0.1.0.tar.gz
| Download URL | qqpybot-0.1.0.tar.gz |
|---|---|
| Size | 12.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d594d36695be01222737ca35e034007e29c2b15625ca560310b4d70583209dd
|
|
BLAKE2b-256 checksum How to use checksums |
0f7b00883d522d333bc405c13ed2ca9a797bc0901329b1f9336591273722bf3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / qqpybot-0.1.0-py3-none-any.whl
| Download URL | qqpybot-0.1.0-py3-none-any.whl |
|---|---|
| Size | 13.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cecbade44fb66217076ed831dd589df397038c0c126ea0d94d9b562748687ed0
|
|
BLAKE2b-256 checksum How to use checksums |
7ef82c38da920441af47c7d82816a537adb6f3d20154cb457b6e81875dd4a5ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for QQPyBot 0.1.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for qqpybot-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA 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.