Skip to main content

easy-to-use SDK for Tencent QQ guild robot

Project description

https://socialify.git.ci/GLGDLY/qg_botsdk/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&logo=https%3A%2F%2Fgithub.com%2Ftencent-connect%2Fbot-docs%2Fblob%2Fmain%2Fdocs%2F.vuepress%2Fpublic%2Ffavicon-64px.png%3Fraw%3Dtrue&name=1&owner=1&pattern=Floating%20Cogs&pulls=1&stargazers=1&theme=Light

https://img.shields.io/badge/language-python-green.svg?style=plastic https://img.shields.io/badge/license-MIT-orange.svg?style=plastic https://img.shields.io/pypi/dw/qg-botsdk?style=plastic&color=blue https://app.codacy.com/project/badge/Grade/f015549b3dba4602be2fe0f5d8b0a8d5 https://readthedocs.org/projects/qg-botsdk/badge/?version=latest

引言

对于使用python进行频道官方机器人开发而言,市面上确实有不同的sdk可以选用,但其很多只提供异步asyncio+类继承的开发方式,对于不会相关技巧的朋友们,尤其新手,会有开发难度。

为此,qg_botsdk相应提供了另一个选择,这一款sdk虽然同样使用asyncio编写sdk底层,但其同时提供了threading和asyncio封装下的应用层调用,以抽象化封装的库编写方式,极大地降低应用层的开发难度。

亮点

  • 已支持Websocket、Webhook、Remote Webhook(wh转ws允许本地调试)三种连接方式

  • 已支持SDK层面的沙箱处理,允许沙箱过滤外部消息、外部过滤沙箱消息;已支持频道、频道私信、群、QQ私信四种沙箱过滤模式

  • 两种应用层开发方式(threading、asyncio),可根据自己的喜好选择,而底层均为asyncio实现,保持高并发能力

  • 灵活的构建方式,即使官方删除或新增字段,SDK也不会规范于原来的数据格式,而会把真实数据反馈给你

  • 轻量,简洁,统一的代码结构,通过录入回调函数处理不同事件,10行即可构建一个简单的程序

  • 容易入门,无需学会asyncio、类继承等编程技巧也可使用,同时保留较高并发能力

  • 保留官方http API中Json数据的结构字段,带你学习官方结构,日后可自行开发适合自己的SDK

  • 简单易用的plugins编写与加载,使用例子可参阅 example_13(装饰器).py

  • 方便多场景(频道+群等)构建机器人的抽象化封装,使用例子可参阅 example_16(Q群简单工作流).py

下载方式

  • pip安装(推荐):

pip install qg-botsdk

注意是qg-botsdk(中线),不是qg_botsdk(底线)

一个简单的工作流

- 注册BOT实例,录入机器人平台获取的ID(BotAppId开发者ID)和token(机器人令牌)
- 编写接收事件的函数->下方例子:def deliver(data),并可借助model库检查数据格式(data: Model.MESSAGE
- 绑定接收事件的函数(bind_msg、bind_dm、bind_msg_delete、bind_guild_event、bind_guild_member、bind_reaction、bind_interaction、bind_audit、bind_forum、bind_audio)
- 开始运行机器人:bot.start()
from qg_botsdk import BOT, Model   # 导入SDK核心类(BOT)、所有数据模型(Model)

bot = BOT(bot_id='xxx', bot_token='xxx', is_private=True, is_sandbox=True)   # 实例化SDK核心类


@bot.bind_msg()   # 绑定接收消息事件的函数
def deliver(data: Model.MESSAGE):   # 创建接收消息事件的函数
    if '你好' in data.treated_msg:   # 判断消息是否存在特定内容
        data.reply('你好,世界')   # 发送被动回复(带message_id直接reply回复)
        # 如需使用如 Embed 等消息模板,可传入相应结构体, 如:
        # data.reply(ApiModel.MessageEmbed(title="你好", content="世界"))


if __name__ == '__main__':
    bot.start()   # 开始运行机器人

指令装饰器

SDK同时已经在内部实现指令装饰器:

from qg_botsdk import BOT, Model

bot = BOT(bot_id='xxx', bot_token='xxx', is_private=True, is_sandbox=True)

# before_command代表预处理器,将在检查所有commands前执行(要求SDK版本>=2.5.2)
@bot.before_command()
def preprocessor(data: Model.MESSAGE):
    bot.logger.info(f"收到来自{data.author.username}的消息:{data.treated_msg}")


@bot.on_command(
    regex=r"你好(?:机器人)?",  # 正则表达式,匹配用户消息
    is_short_circuit=True,    # is_short_circuit代表短路机制,根据注册顺序,匹配到即停止匹配,但不影响bind_msg()
    is_require_at=True,       # is_require_at代表是否要求检测到用户@了机器人才可触发指令
    is_require_admin=True,    # is_require_admin代表是否要求检测到用户是频道主或频道管理才可触发指令
    admin_error_msg="抱歉,你的权限不足(非频道主或管理员),不能使用此指令",
)
def command(data: Model.MESSAGE):
    data.reply("你好,世界")


if __name__ == '__main__':
    bot.start()

相关链接

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qg_botsdk-4.3.9-py3-none-any.whl (107.5 kB view details)

Uploaded Python 3

File details

Details for the file qg_botsdk-4.3.9-py3-none-any.whl.

File metadata

  • Download URL: qg_botsdk-4.3.9-py3-none-any.whl
  • Upload date:
  • Size: 107.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for qg_botsdk-4.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 fd1e0dabb6332df27b893e99dd38261c46ac9e8119dda8f480e0dc97a1a57704
MD5 1eb8bc76a2d8d3d2b458b0843538fbe8
BLAKE2b-256 bd1f52ee3d3f5175048d51cd73005f8ac7964248f2c65202db6fa10b5b161abe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page