基于 OneBot 11 标准实现的异步 Python 机器人框架
Note
你需要先学习 Python 的 asyncio 再来使用本库
在你的虚拟环境中安装
$ pip install git+https://github.com/Web-Art-Online/BotX
import asyncio
from botx import Bot
from botx.models import Message, PrivateMessage, FriendRequest, GroupRecall
bot = Bot("ws://localhost:3001")
@bot.on_msg()
async def echo(msg: PrivateMessage):
await msg.reply(msg.raw_message)
# 如果要接收 Message, 你可以使用 targets 来指定目标, 'p'代表私聊, 'g'代表群聊
@bot.on_cmd("hi", help_msg="你好", targets=["p10001", "g123456"])
async def hi(msg: Message):
await msg.reply("hi")
# 如果指定了 PrivateMessage / GroupMessage 则直接指定 int
@bot.on_cmd("hi", help_msg="你好", targets=[10001])
async def hi_2(msg: PrivateMessage):
await msg.reply("hi")
# 也可以不指定, 接收所有消息. 注意: targets 为 [] / None / False 时皆为不指定
@bot.on_cmd("hi", help_msg="你好")
async def hi_2(msg: PrivateMessage):
await msg.reply("hi")
@bot.on_request()
async def friend(req: FriendRequest):
await req.result(True)
@bot.on_notice()
async def recall(notice: GroupRecall):
pass
@bot.on_error()
""" 第一个参数为 Eventloop 的 Context, 第二个参数为 Onebot 发送的数据 """
async def error(context: dict, data: dict):
pass
asyncio.run(bot.start())你需要使用类型注解来告诉 BotX 你想要干什么
比如,你想要接收私聊消息那就把形参声明为PrivateMessage类型
相信聪明的你一定能快速上手的~✨
目前 BotX 仅支持 send_private, send_group, get_msg 三个方法, 其余的 API 请使用 call_api 方法调用
本项目的 Qzone 部分借鉴了 Campux
自己看代码吧~
async def publish(text: str, image: bytes)
qzone = await bot.get_qzone()
return await qzone.publish(text,
await qzone.upload_image(image))- Recall (PrivateRecall, GroupRecall)
- FriendAdd
- GroupIncrease
- GroupDecrease
- FriendRequest
- GroupRequest
本项目为开源软件,遵循 LGPL v2 许可证发布。使用者可以自由地下载、修改和分发本项目的代码,但必须遵守以下条款:
-
禁止非法用途 本项目仅可用于合法用途。禁止将本项目用于任何违反法律法规的行为,包括但不限于:
- 侵犯他人知识产权;
- 传播恶意软件或病毒;
- 进行网络攻击或其他非法活动。
-
作者免责
本项目的作者和贡献者不对使用者的行为负责。使用者因使用、修改或分发本项目而产生的任何后果(包括但不限于法律纠纷、经济损失等),均由使用者自行承担,与作者和贡献者无关。 -
无担保声明
本项目按“原样”提供,不提供任何形式的明示或暗示担保,包括但不限于对适用性、特定用途适用性、无病毒或无错误的担保。使用者需自行承担使用本项目的一切风险。 -
遵守当地法律
使用者在下载、使用或分发本项目时,必须遵守所在国家或地区的法律法规。如果本项目的任何部分违反当地法律,使用者应立即停止使用。