If you want to send the same content to WeChat friends in batches, it is time-consuming and laborious to operate manually. At this time, you can use Python to implement automated processing, which is more efficient and convenient. Mass can greatly reduce your repetitive work.
At present, many merchants need to advertise and promote in WeChat groups. The content introduced today can allow everyone to operate and promote conveniently and flexibly. The following are cases of sending text, pictures, and videos. Please be sure to use this function legally and compliantly and not abuse it.
Example 1, send a text message:
import itchat # Please replace it with your WeChat username and passwordusername = 'your_wechat_username' password = 'your_wechat_password' # Specify the target user to send the messagetarget_users = ['User1', 'User2', 'User3'] # Replace the username of the target user you want to send the [email protected]_register() def text_reply(msg): if msg['ToUserName'] in target_users: return 'Hello, %s' % msg['Text'] # Initialize the WeChat client and log initchat.auto_login(hotReload=True) while True: () # Close the WeChat client()
In this example, we first define a list target_users that contains the username of the target user we want to send the message. Then, in our message handler function, we check if the message currently received comes from one of these target users, and if so, we reply to a message.
Example 2: Send image message:
import itchat # Please replace it with your WeChat username and passwordusername = 'your_wechat_username' password = 'your_wechat_password' # Specify the target user to send the messagetarget_users = ['User1', 'User2', 'User3'] # Replace the username of the target user you want to send the [email protected]_register() def text_reply(msg): if msg['ToUserName'] in target_users: return 'Hello, %s' % msg['Text'] # Initialize the WeChat client and log initchat.auto_login(hotReload=True) # Send pictures in the albumimage_path = 'path_to_your_image.jpg' # Replace the path to the image you want to sendfor user in target_users: itchat.send_image(image_path, toUserName=user) # Close the WeChat client()
In this example, we first define a list target_users that contains the username of the target user we want to send the message. Then, in our message handler function, we check if the message currently received comes from one of these target users, and if so, we reply to a message.
Finally, we use the itchat.send_image method to send the image. You need to provide the path of the image (relative to the location where the script is currently running) as the parameter.
Example 3: Send video messages:
import itchat # Please replace it with your WeChat username and passwordusername = 'your_wechat_username' password = 'your_wechat_password' # Specify the target user to send the messagetarget_users = ['User1', 'User2', 'User3'] # Replace the username of the target user you want to send the [email protected]_register() def text_reply(msg): if msg['ToUserName'] in target_users: return 'Hello, %s' % msg['Text'] # Initialize the WeChat client and log initchat.auto_login(hotReload=True) # Send videos in the albumvideo_path = 'path_to_your_video.mp4' # Replace with the path of the video you want to sendfor user in target_users: itchat.send_video(video_path, toUserName=user) # Close the WeChat client()
In this example, we first define a list target_users that contains the username of the target user we want to send the message. Then, in our message handler function, we check if the message currently received comes from one of these target users, and if so, we reply to a message.
Finally, we use the itchat.send_video method to send the video. You need to provide the path to the video (relative to the location where the script is currently running) as the parameter.
Note: This example is just a basic example, and may need to be modified according to your specific needs when actually using it. For example, you may need to read the message content to be sent from a file or other source, or process different message types (such as pictures, voice, etc.). In addition, if you have a large number of contacts, you may encounter restrictions on WeChat and need to consider how to send messages in batches or regularly.
Things to note
Please run the above code in Windows environment. During the operation, the WeChat login QR code will pop up. Use WeChat to scan the QR code to log in. If you encounter a situation where you cannot log in during operation, please give priority to check whether the WeChat version is the latest version.
For other questions, please refer to:
When using the itchat library, if the problem of "need to log in again", it is usually because the WeChat client is automatically closed. This may be due to the following reasons:
The account has not been logged in on your mobile phone, or it is the first time I logged in using the WeChat client.
The "Login Protection" function is enabled in the WeChat client settings.
For these problems, you can try the following solutions:
Make sure the WeChat client is logged in and is not closed manually.
In addition, if you encounter other problems when using the itchat library, you can refer to the official documentation or the issue list on GitHub for help.
Method supplement
python------Customized mass WeChat messages
How to use the information in the form to send WeChat messages?
- How to read csv? → Use built-in module csv
- How to send the corresponding information to WeChat? → Use third-party library wxpy
import csv from wxpy import * import time # Before running the code, you need to change the name in the table to your friend’s WeChat name. def read_info(): f = open(r'F:\temp\Script_Day10/','r',encoding='utf-8') reader = (f) return [info for info in reader]#[{},{},{}] #'xx-Students please participate in the xx course on xx time, the course address is xxx. Please reply after receiving it, thank you'def make_msg(raw_info): t = '{n}-Students please{t}Time to participate{s}course,course地址是{a}。Please reply after receiving,Thanks!' return [(n=info['Name'], t=info['Class time'], s=info['course'], a=info['Class Address'] ) for info in raw_info] # -> list ['xxx','xxx'] def send(msg_list): bot = Bot() for msg in msg_list: fren_name = ('-')[0] f = ().search(fren_name) # list if len(f) == 1: f[0].send(msg) else: print(fren_name) print('Please check this name') (5) if __name__ == '__main__': raw_info = read_info() msg_list = make_msg(raw_info) send(msg_list)
This is the article about using python to implement mass WeChat messages. For more related content on python mass WeChat messages, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!