🚀
极简优先
5 行代码即可启动机器人,无需理解复杂的协议细节。SDK 内部封装 Intent 计算、Token 管理、消息去重、签名验证等所有底层逻辑。
环境要求
pip install easybot-qqgit clone https://github.com/SaucePlum/easybot.git
cd easybot
pip install -r requirements.txtfrom easybot import Bot, Model
bot = Bot(app_id="你的AppID", app_secret="你的AppSecret")
@bot.on_guild_message
async def on_message(msg: Model.GuildMessage) -> None:
await msg.reply("Hello World!")
bot.start()提示
公域机器人只会收到频道内 @它 的消息;请在频道中 @机器人进行测试。
from easybot import Bot, Model, APIError, NetworkError, RateLimitError
bot = Bot(app_id="你的AppID", app_secret="你的AppSecret")
@bot.on_guild_message
async def on_message(msg: Model.GuildMessage) -> None:
try:
await msg.reply(f"你说:{msg.treated_msg}")
except RateLimitError as e:
bot.logger.warning(f"触发频率限制:{e}")
except (APIError, NetworkError) as e:
bot.logger.error(f"回复失败:{e}")
bot.start()import asyncio
from easybot import Bot, Model
bot = Bot(app_id="你的AppID", app_secret="你的AppSecret")
@bot.on_guild_message
async def on_message(msg: Model.GuildMessage) -> None:
await msg.reply("Hello World!")
asyncio.run(bot.start_async())注意
运行前请确保已在 QQ 开放平台 创建机器人应用并获取 AppID 和 AppSecret。
支持 QQ 平台全场景消息交互,一套代码适配多种场景:
| 场景 | 说明 | 适用范围 |
|---|---|---|
| 频道消息 | 公域 @机器人消息、私域全量消息 | 频道社区 |
| 群聊消息 | 群内 @机器人消息 | QQ 群聊 |
| 单聊消息 | QQ 私聊消息 | 一对一聊天 |
| 频道私信 | 频道内私信对话 | 频道私信 |
全面覆盖 QQ 开放平台的 OpenAPI,提供类型安全的 Python 接口:
基于作用域的会话状态管理,轻松实现多轮对话:
USER GUILD CHANNEL GROUP GLOBALfrom easybot import Bot, Model, Scope, WaitTimeoutError
bot = Bot(app_id="你的AppID", app_secret="你的AppSecret")
@bot.on_guild_message
async def on_message(msg: Model.GuildMessage) -> None:
with bot.session.bind(msg) as s:
await msg.reply("请输入你的名字:")
try:
# 等待用户回复(任意输入)
name_msg = await s.wait_for(
scopes=Scope.USER,
command=None, # None 表示接受任意输入
timeout=60 # 60秒超时
)
name = name_msg.treated_msg or name_msg.content
await msg.reply(f"你好,{name}!")
except WaitTimeoutError:
await msg.reply("等待超时,请重新开始。")
bot.start()