本文记录用nonebot2搭建多功能QQ机器人的流程,包括go-cqhttp和nonebot2环境搭建,解决了Linux下的扫码问题,介绍了配置及自建模块方法,附chatgpt模块示例,已实现文生图等多项功能。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

现在网络上已经有很多的教程去教学QQ搭建,我这里再写一个完整的流程搭建及自建模块如何实现。
(主要是怕自己搭完一次就忘了,算是个记录博客)
采用目前主流的nonebot2完成QQ机器人搭建。
这是一个QQ机器人功能合集。
目前实现了如下功能:
下载go-cqhttp压缩包
https://docs.go-cqhttp.org/guide/quick_start.html#%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B
或者在github下载,都一样
https://github.com/Mrs4s/go-cqhttp/releases
按照官网流程进行操作即可
https://docs.go-cqhttp.org/guide/quick_start.html
我的config.yml已放在go-cqhttp文件夹中,可进行参考。
PS linux环境下踩坑记录
这里可能会存在一个情况,在配置好config.yml运行脚本之后
若配置文件没有写密码,会提示你进行扫码,扫码需要保持设备之间是局域网环境,我实操linux系统无法解决这个问题
后面解决的方法是:将Windows中生成的device.json文件复制到linux对应的位置,即可解决问题。
先放官网地址
https://v2.nonebot.dev/docs/start/installation
安装nonebot2
pip install nb-cli
配置qqaibot/config.json
{ "openai_key": "sk-xxxxxxxxxxxxxxxx", "app_id": "xxxxxxxxxxxxxxxxx", "app_secret": "xxxxxxxxxxx", "gaode_key": "xxxxxxxxxxxxxxxxx"}到这里在运行go-cqhttp的前提下,你已经可以执行
qqaibot/bot.py
完成机器人的运行
可以参考我的写法,在下面路径新建文件夹
qqaibot/src/plugins/
每一个文件夹都是一个模块,然后完成自己的模块编写即可。
举例chatgpt DEMO 如下:
chatbot.py
import configimport openaifrom nonebot import on_commandfrom nonebot.adapters.onebot.v11 import MessageEvent, MessageSegmentfrom nonebot.rule import to_mefrom revChatGPT.Official import Chatbot# 只有艾特机器人才会触发rule = to_me()
key, _, _, _ = config.getToken()
chat = on_command("chatgpt", aliases={ "对话", "聊天", "chat"}, priority=99, rule=rule)@chat.handle()async def gpt3chat(event: MessageEvent):
# 获取用户发送的消息
msg = str(event.get_message()).strip() if len(msg.split(" ")) == 1: await chat.send(MessageSegment.text("请输入对话提示(或检查格式)")) await chat.send(MessageSegment.text("格式:聊天 你吃饭了吗?")) if len(msg.split(" ")) > 1: await chat.send(MessageSegment.text("收到,本bot正在组织语言..."))
flag = msg.split(" ")[0]
prompt = msg.split(" ", 1)[1] if flag == "对话" or flag == "gpt" or flag == "聊天" or flag == "chat": try:
res = await getres(prompt) except: await chat.send(MessageSegment.text("请求超时,可能是网络问题")) await chat.finish(MessageSegment.text("请重试")) await chat.finish(MessageSegment.text(res))async def getres(prompt):
openai.api_key = key
chatbot = Chatbot(api_key=key)
response = chatbot.ask(prompt)
res = response["choices"][0]["text"] return res.strip()以上就是玩转多功能QQ机器人【含ChatGPT实现】的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号