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 @   蓝火BlueFlameAshe  阅读(135)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示