Tencent QQ Channel Python SDK

1.2.1 · active · verified Thu Apr 16

qq-botpy is the official Python SDK for creating bots on Tencent's QQ Channels, providing an easy-to-use and efficient framework for developers. It abstracts the complexities of the QQ Open Platform API, enabling quick development of features like message handling and event listening. The library is actively maintained, with frequent updates; the current stable version is 1.2.1.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic QQ Channel bot that responds to @-mentions. It requires your QQ bot's AppID and AppSecret, which should be set as environment variables `QQ_BOT_APP_ID` and `QQ_BOT_APP_SECRET` for security, or replaced directly with your credentials. The bot will print a 'ready' message and reply to any public guild message where it is @-mentioned.

import os
import botpy
from botpy.types.message import Message

# Get credentials from environment variables or provide defaults
APP_ID = os.environ.get("QQ_BOT_APP_ID", "YOUR_APP_ID")
APP_SECRET = os.environ.get("QQ_BOT_APP_SECRET", "YOUR_APP_SECRET")

class MyClient(botpy.Client):
    async def on_ready(self):
        """Event triggered when the bot is ready."""
        print(f"robot 「{self.robot.name}」 on_ready!")

    async def on_at_message_create(self, message: Message):
        """Event triggered when the bot receives an @ message."""
        # Remove the bot's mention from the message content
        user_message_content = message.content.replace(f"<@{self.robot.id}>", "").strip()
        
        if user_message_content:
            response_content = f"Hello, {message.author.username}! You said: {user_message_content}"
        else:
            response_content = f"Hello, {message.author.username}! How can I help you?"

        await message.reply(content=response_content)

if __name__ == "__main__":
    # Define the intents (events the bot should listen to)
    intents = botpy.Intents(public_guild_messages=True)
    
    # Create and run the client
    client = MyClient(intents=intents)
    client.run(appid=APP_ID, secret=APP_SECRET)

view raw JSON →