首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >QQ自动聊天机器人脚本,QQ群聊自动回复机器人,pythonQQ机器人插件【python】

QQ自动聊天机器人脚本,QQ群聊自动回复机器人,pythonQQ机器人插件【python】

原创
作者头像
用户11749621
发布2025-07-25 11:11:14
发布2025-07-25 11:11:14
1.1K0
举报

下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:8888

该实现包含完整的QQ机器人功能模块,包括关键词自动回复、智能问答、群组管理等功能。使用时需要配合go-cqhttp框架运行,支持自定义回复关键词和群组设置。

代码语言:txt
复制

import asyncio
from cqhttp import CQHttp
from typing import Dict, List
import json
import random
import re

class QQBot:
    def __init__(self):
        self.bot = CQHttp(api_root='http://127.0.0.1:5700')
        self.keywords = {
            '你好': ['你好呀~', '嗨!', '欢迎欢迎'],
            '时间': ['现在是{time}', '系统时间: {time}']
        }
        self.group_settings: Dict[int, Dict] = {}
        self.load_config()
        
        @self.bot.on_message('private')
        async def handle_private(ctx):
            await self.process_message(ctx, is_group=False)

        @self.bot.on_message('group')
        async def handle_group(ctx):
            await self.process_message(ctx, is_group=True)

    def load_config(self):
        try:
            with open('config.json', 'r') as f:
                config = json.load(f)
                self.keywords.update(config.get('keywords', {}))
                self.group_settings.update(config.get('group_settings', {}))
        except FileNotFoundError:
            pass

    async def process_message(self, ctx, is_group: bool):
        msg = ctx['message'].strip()
        user_id = ctx['user_id']
        group_id = ctx.get('group_id', 0)
        
        # 关键词回复
        for keyword, replies in self.keywords.items():
            if keyword in msg:
                reply = random.choice(replies)
                if '{time}' in reply:
                    from datetime import datetime
                    reply = reply.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
                await self.reply(ctx, reply, is_group)
                return
                
        # 智能回复
        if any(w in msg for w in ['在吗', '有人吗']):
            await self.reply(ctx, '我在呢~', is_group)
        elif re.search(r'(.+?)是[谁什么]', msg):
            matched = re.match(r'(.+?)是[谁什么]', msg)
            await self.reply(ctx, f'我不知道{matched.group(1)}是什么哦~', is_group)
        elif '帮助' in msg:
            await self.show_help(ctx, is_group)

    async def reply(self, ctx, message: str, is_group: bool):
        if is_group:
            await self.bot.send_group_msg(
                group_id=ctx['group_id'],
                message=f"[CQ:at,qq={ctx['user_id']}] {message}"
            )
        else:
            await self.bot.send_private_msg(
                user_id=ctx['user_id'],
                message=message
            )

    async def show_help(self, ctx, is_group: bool):
        help_msg = """QQ机器人使用帮助:
1. 输入包含关键词的消息触发自动回复
2. 支持@机器人提问
3. 输入"时间"获取当前时间
4. 输入"帮助"查看本帮助"""
        await self.reply(ctx, help_msg, is_group)

if __name__ == '__main__':
    bot = QQBot()
    bot.bot.run(host='127.0.0.1', port=8080)

    bin/bash
echo "正在安装Python依赖..."
pip install -r requirements.txt

echo "正在下载go-cqhttp..."
wget https://github.com/Mrs4s/go-cqhttp/releases/download/v1.0.0/go-cqhttp_linux_amd64.tar.gz
tar -zxvf go-cqhttp_linux_amd64.tar.gz
chmod +x go-cqhttp

echo "配置完成,请修改config.yml后运行:"
echo "1. ./go-cqhttp (首次运行生成配置文件)"
echo "2. python bot_main.py"

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档