Skip to main content

qq robot client with python3

Project description

fork from [tencent-connect/botpy](https://github.com/tencent-connect/botpy) 更新了一些接口字段,以适配最新版本的机器人开放平台API

botpy

botpy 是基于机器人开放平台API 实现的机器人框架,目的提供一个易使用、开发效率高的开发框架。

PyPI BK Pipelines Status

准备工作

安装

pip install qq-botpy

更新包的话需要添加 --upgrade 注:需要python3.7+

使用

需要使用的地方import botpy

import botpy

兼容提示

原机器人的老版本qq-bot仍然可以使用,但新接口的支持上会逐渐暂停,此次升级不会影响线上使用的机器人

使用方式

快速入门

步骤1

通过继承实现bot.Client, 实现自己的机器人Client

步骤2

实现机器人相关事件的处理方法,如 on_at_message_create, 详细的事件监听列表,请参考 事件监听.md

如下,是定义机器人被@的后自动回复:

import botpy
from botpy.types.message import Message

class MyClient(botpy.Client):
    async def on_ready(self):
        print(f"robot 「{self.robot.name}」 on_ready!")

    async def on_at_message_create(self, message: Message):
        await message.reply(content=f"机器人{self.robot.name}收到你的@消息了: {message.content}")

注意:每个事件会下发具体的数据对象,如`message`相关事件是`message.Message`的对象 (部分事件透传了后台数据,暂未实现对象缓存)

步骤3

设置机器人需要监听的事件通道,并启动client

import botpy
from botpy.types.message import Message

class MyClient(botpy.Client):
    async def on_at_message_create(self, message: Message):
        await self.api.post_message(channel_id=message.channel_id, content="content")

intents = botpy.Intents(public_guild_messages=True)
client = MyClient(intents=intents)
client.run(appid="12345", token="xxxx")

备注

也可以通过预设置的类型,设置需要监听的事件通道

import botpy

intents = botpy.Intents.none()
intents.public_guild_messages=True

使用API

如果要使用api方法,可以参考如下方式:

import botpy
from botpy.types.message import Message

class MyClient(botpy.Client):
    async def on_at_message_create(self, message: Message):
        await self.api.post_message(channel_id=message.channel_id, content="content")

示例机器人

``examples` <./examples/>`_ 目录下存放示例机器人,具体使用可参考``Readme.md` <./examples/README.md>`_

examples/
.
├── README.md
├── config.example.yaml          # 示例配置文件(需要修改为config.yaml)
├── demo_announce.py             # 机器人公告API使用示例
├── demo_api_permission.py       # 机器人授权查询API使用示例
├── demo_at_reply.py             # 机器人at被动回复async示例
├── demo_at_reply_ark.py         # 机器人at被动回复ark消息示例
├── demo_at_reply_embed.py       # 机器人at被动回复embed消息示例
├── demo_at_reply_command.py     # 机器人at被动使用Command指令装饰器回复消息示例
├── demo_at_reply_file_data.py   # 机器人at被动回复本地图片消息示例
├── demo_at_reply_keyboard.py    # 机器人at被动回复md带内嵌键盘的示例
├── demo_at_reply_markdown.py    # 机器人at被动回复md消息示例
├── demo_at_reply_reference.py   # 机器人at被动回复消息引用示例
├── demo_dms_reply.py            # 机器人私信被动回复示例
├── demo_get_reaction_users.py   # 机器人获取表情表态成员列表示例
├── demo_guild_member_event.py   # 机器人频道成员变化事件示例
├── demo_interaction.py          # 机器人互动事件示例(未启用)
├── demo_pins_message.py         # 机器人消息置顶示例
├── demo_recall.py               # 机器人消息撤回示例
├── demo_schedule.py             # 机器人日程相关示例

更多功能

更多功能请参考: [https://github.com/tencent-connect/botpy]

Project details


Download files

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

Source Distribution

pt_qq_botpy-0.0.2.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

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

pt_qq_botpy-0.0.2-py3-none-any.whl (53.0 kB view details)

Uploaded Python 3

File details

Details for the file pt_qq_botpy-0.0.2.tar.gz.

File metadata

  • Download URL: pt_qq_botpy-0.0.2.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pt_qq_botpy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 db9ae9557c8e19d5d0957f15bdc4c2748afe057ddbffd1187958aa7552df9030
MD5 72e299ec4706463b2ac74485835d92b5
BLAKE2b-256 8bd0815fe28cd39f255a0bebf5fd091ee80889c5e97103429693c99185cb1177

See more details on using hashes here.

File details

Details for the file pt_qq_botpy-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pt_qq_botpy-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 53.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pt_qq_botpy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d9c475609e1b7878474a6675a5530c4bc6ce00a18996ab48c51cf559fdde7d46
MD5 5730223a74c705b150c6e967d3de50e1
BLAKE2b-256 fd360169c14c72b1f548336114d7ea68b9d13d0a27eeb5d1750afe3beb299297

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