wxpy Also a python Module , We can do many interesting things with it , Today I will introduce to you through this article Python Use wxpy The module realizes the message synchronization function of two groups of wechat .
Install the module :
pip install wxpy
Be careful : Wechat groups that need to be synchronized need to be saved in the address book
The following is the code I wrote in my spare time , For the time being, the following problems still exist , There are leaders who can optimize to discuss :
1. Temporarily unable to synchronize large files , The test found more than 40M Your file could not be synchronized ;
2. Frequent sending of messages may cause some messages to be lost ;
3. The project is unstable , Sometimes the line drops , The script needs to restart and log in to wechat again
Go straight to the code
import timefrom wxpy import *# Use the synchronized wechat group , For a double list , The innermost layer list is the wechat group to be synchronized , There can be multiple need_group = [[' Customer 1 Group ', ' technology 1 Group '], [' Customer 2 Group ', ' technology 2 Group '], [' Customer 3 Group ', ' technology 3 Group '], ]# People who need shielding except_list = [" Zhang San ", " Li Si "]# Double list of objects to save search results group_obj_list = [[] for _ in range(len(need_group))]# Initialize the robot , The computer pops up the QR code , Log in with wechat scanning code bot = Bot()# After wechat login , Update wechat group list ( Include groups that have not been saved to the address book )bot.groups(update=True, contact_only=False)# Send a message to wechat assistant bot.file_helper.send('wechat bot login success.')# Find groups try: for i in range(len(need_group)): # Be careful : You need to save the group you want to search to the address book my_groups_Q1 = bot.groups().search(need_group[i][0])[0] my_groups_Q2 = bot.groups().search(need_group[i][1])[0] # Update group messages 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: passdef send_message(msg, group_list): try: # Shield sb if msg.member.name not in except_list: # Use API Provided function synchronization message sync_message_in_groups(msg, group_list, prefix="") except: pass# It is temporarily found that the binding listening event is a blocking event , Need help one by one , If loop binding is used, only the first one can be bound # Sync 1 Group @bot.register(group_obj_list[0], except_self=False)def sync_my_groups_00(msg): send_message(msg, group_obj_list[0])# Sync 2 Group @bot.register(group_obj_list[1], except_self=False)def sync_my_groups_01(msg): send_message(msg, group_obj_list[1])# Sync 3 Group @bot.register(group_obj_list[2], except_self=False)def sync_my_groups_02(msg): send_message(msg, group_obj_list[2])# every 30min Send a message to wechat assistant , If not, the program crashes while True: DATE = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) bot.file_helper.send(' The program is running ', DATE) time.sleep(1800)embed()# Blocking threads , Keep the robot running bot.join()
When synchronizing wechat groups, the default prefix is emoticon plus wechat name , If you want to remove it , Click to enter sync_message_in_groups Function to modify the source code , Here's the picture
This is about Python Use wxpy The module realizes the message synchronization function of two groups of wechat ( recommend ) This is the end of the article , More about Python Please search the previous articles of SDN or continue to browse the related articles below for the message synchronization content of wechat two groups. I hope you will support SDN more in the future !