开发QQ官方机器人

QQ 频道机器人开发简明教程

1. 简介

QQ 频道机器人是一种可以在 QQ 频道中与用户进行互动的服务。这个教程旨在帮助新手学习如何使用 Python 的官方SDK,快速实现一些基本的机器人功能。

2. 准备工作

  • 机器人注册和添加:QQ 开放平台 注册开发者账号,并创建一个机器人应用。获取分配给机器人的 App ID 和 Token。

3. 环境搭建

  • 安装 Python3: 确保你的机器上已经安装了 Python3。

  • 安装机器人SDK: 打开终端并执行以下命令安装机器人的 Python SDK:

    bash 复制代码
    pip install qq-bot
    pip install pyyaml

4. 创建项目文件

  • 创建两个文件
  • config.yaml
  • robot.py

5. 配置文件填写

  • 打开 config.yaml 文件,填入机器人的 App ID 和 Token,并保存:

    yaml 复制代码
    token:
      appid: "123"
      token: "xxxx"

6. 编写机器人代码

  • 打开 robot.py 文件,导入依赖包并设置机器人自动回复消息的功能:

    python 复制代码
    import asyncio
    import os.path
    import qqbot
    from qqbot.core.util.yaml_util import YamlUtil
    
    # 从配置文件读取机器人的信息
    test_config = YamlUtil.read(os.path.join(os.path.dirname(__file__), "config.yaml"))
    
    async def _message_handler(event, message: qqbot.Message):
        msg_api = qqbot.AsyncMessageAPI(t_token, False)
        qqbot.logger.info("event %s" % event + ",receive message %s" % message.content)
    
        # 自动回复消息
        message_to_send = qqbot.MessageSendRequest(content="你好", msg_id=message.id)
        await msg_api.post_message(message.channel_id, message_to_send)
    
    if __name__ == "__main__":
        t_token = qqbot.Token(test_config["token"]["appid"], test_config["token"]["token"])
        qqbot_handler = qqbot.Handler(
            qqbot.HandlerType.AT_MESSAGE_EVENT_HANDLER, _message_handler
        )
        qqbot.async_listen_events(t_token, False, qqbot_handler)

7. 运行机器人

  • 在终端中执行以下命令,运行机器人:

    bash 复制代码
    python3 /home/demo/robot.py

8. 测试机器人

  • 在 QQ 频道中 @机器人 发送消息(例如,@机器人 hello),机器人将自动回复 "你好"。

9. 关键词功能

我们将robot.py里面的代码改为关键词回复,可以在 _message_handler 函数中使用条件语句检查收到的消息内容,然后根据关键词回复不同的内容。

python 复制代码
import asyncio
import os.path
import qqbot
from qqbot.core.util.yaml_util import YamlUtil

# 从配置文件读取机器人的信息
test_config = YamlUtil.read(os.path.join(os.path.dirname(__file__), "config.yaml"))

async def _message_handler(event, message: qqbot.Message):
    msg_api = qqbot.AsyncMessageAPI(t_token, False)
    qqbot.logger.info("event %s" % event + ", receive message %s" % message.content)

    # 检查消息内容是否包含关键词
    if "你好" in message.content:
        reply_content = "你也好!"
    elif "再见" in message.content:
        reply_content = "再见,期待下次见面!"
    else:
        reply_content = "我不太明白你说的是什么..."

    # 回复消息
    message_to_send = qqbot.MessageSendRequest(content=reply_content, msg_id=message.id)
    await msg_api.post_message(message.channel_id, message_to_send)

if __name__ == "__main__":
    t_token = qqbot.Token(test_config["token"]["appid"], test_config["token"]["token"])
    qqbot_handler = qqbot.Handler(
        qqbot.HandlerType.AT_MESSAGE_EVENT_HANDLER, _message_handler
    )
    qqbot.async_listen_events(t_token, False, qqbot_handler)

到自己的测试频道艾特自己的机器人发送关键词即可

这里我添加了一个简单的条件语句,检查消息内容中是否包含关键词 "你好" 或 "再见",然后根据关键词回复不同的内容。

如果消息内容中包含关键词,则回复相应的内容;否则,回复一个默认的消息。

相关推荐
databook12 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar13 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805113 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_13 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机20 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机21 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机21 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机21 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i21 小时前
drf初步梳理
python·django
每日AI新事件21 小时前
python的异步函数
python