zoukankan      html  css  js  c++  java
  • Python使用wxpy模块实现微信两两群组消息同步

    python使用wxpy模块提供的微信API接口实现两两群组的消息同步

    安装模块: pip install wxpy

    注意:需要同步的微信群需要保存到通讯录中

    以下是自己闲来无事写的代码,暂时还存在以下几个问题,有能优化的大佬可以讨论下:

    1.暂时同步不了大文件,测试发现超过40M的文件无法同步;

    2.频发发送消息时可能导致有的消息丢失;

    3.项目不稳定,有时会掉线,脚本需要重启后重新登录微信

    直接上代码

    import time
    from wxpy import *

    # 用同步的微信群,为双重列表,最里层列表为要同步的微信群,可以有多个
    need_group = [['客户1群', '技术1群'],
    ['客户2群', '技术2群'],
    ['客户3群', '技术3群'],
    ]

    # 需要屏蔽的人
    except_list = ["张三", "李四"]


    # 保存搜索结果的对象双重列表
    group_obj_list = [[] for _ in range(len(need_group))]

    # 初始化机器人,电脑弹出二维码,用手机微信扫码登陆
    bot = Bot()

    # 微信登陆后,更新微信群列表(包括未保存到通讯录的群)
    bot.groups(update=True, contact_only=False)

    # 往微信助手发消息
    bot.file_helper.send('wechat bot login success.')

    # 查找群
    try:
    for i in range(len(need_group)):
         
         # 注意: 暂时发现需要搜索的群需要保存到通讯录中
    my_groups_Q1 = bot.groups().search(need_group[i][0])[0]
    my_groups_Q2 = bot.groups().search(need_group[i][1])[0]

    # 更新群消息
    my_groups_Q1.update_group(members_details=True)
    my_groups_Q2.update_group(members_details=True)

    group_obj_list[i].append(my_groups_Q1)
    group_obj_list[i].append(my_groups_Q2)

    except:
    pass


    def send_message(msg, group_list):
    try:
    # 屏蔽某人
    if msg.member.name not in except_list:
           # 使用API提供的函数同步消息
    sync_message_in_groups(msg, group_list, prefix="")
    except:
    pass

    # 暂时发现绑定监听事件是阻塞事件,需要一个一个帮忙,用循环绑定的话只能绑定第一个

    # 同步1群
    @bot.register(group_obj_list[0], except_self=False)
    def sync_my_groups_00(msg):

    send_message(msg, group_obj_list[0])


    # 同步2群
    @bot.register(group_obj_list[1], except_self=False)
    def sync_my_groups_01(msg):

    send_message(msg, group_obj_list[1])


    # 同步3群
    @bot.register(group_obj_list[2], except_self=False)
    def sync_my_groups_02(msg):

    send_message(msg, group_obj_list[2])

    # 每过30min往微信助手发送消息,不发则说明程序崩溃
    while True:
    DATE = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    bot.file_helper.send('程序运行中', DATE)
    time.sleep(1800)
    embed()
    # 堵塞线程,让机器人保持运行
    bot.join()



    同步微信群时默认的前缀为表情加微信名,如果想去掉的话,点击进入sync_message_in_groups函数修改下源码,如下图

  • 相关阅读:
    取物料特性
    MATCHCODE OBJECT 属性是什么意思
    SAPABAP用户权限检查
    FOR ALL ENTRIES的原理:
    上传EXCEL和text到SAP内表 ,SAP内表导出EXCEL
    屏幕设计设置默认值
    使用JOIN关联多张数据库表
    WHERE spras = sylangu.这是什么意思
    DESCRIBE TABLE
    取批次特性值
  • 原文地址:https://www.cnblogs.com/skaarl/p/13109679.html
Copyright © 2011-2022 走看看