Python wxauto 微信 机器人复读机

from wxauto import *
import time
import random

repeat_rate = .3  # 回复概率
wait_seconds = 5  # 查询时间
start_num = 20  # 语料库少于这个数,就先攒一攒
file_name = 'db.txt'  # 语料库路径,可用数据库替代
split_tag = '6lol6'  # 分隔符,可改,如果用数据库,就不需要这个
chat_name = '群名称'  # 群名称或人名称

def save_content(content):
    f = open(file_name, mode='a', encoding='utf-8')
    f.write(content + split_tag)
    f.close()

def send_content():
    if random.random() < repeat_rate:
        f = open(file_name, mode='r', encoding='utf-8')
        line = f.readline()
        repeats = line.split(split_tag)
        if len(repeats) - 2 > start_num:
            index = random.randint(0, len(repeats) - 2)
            wx.SendMsg(repeats[index], chat_name)
            f.close()
    else:
        print('对[' + msg.content + ']不回复')

if __name__ == '__main__':
    wx = WeChat()
    wx.AddListenChat(chat_name)

    try:
        while True:
            new_msgs = wx.GetListenMessage(chat_name)
            if len(new_msgs) == 0:
                continue

            for msg in new_msgs:
                if (msg.content.startswith('[图片]')
                        or msg.content.startswith('[文件]')
                        or msg.content.startswith('[语音]')):
                    print('不处理')
                    continue
                if msg.type == 'friend':
                    save_content(msg.content)
                    send_content()

            time.sleep(wait_seconds)

    except KeyboardInterrupt:  # Ctrl + C 退出
        print('\n程序结束')
        quit()

posted @ 2024-08-24 21:54  蓝火BlueFlameAshe  阅读(91)  评论(0编辑  收藏  举报