2025-07-25 16:05:45 admin 中国男足进世界杯

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

下载地址: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"