Automating Bulk WeChat Messaging with Python and ItChat
This guide demonstrates how to use Python's ItChat library to programmatically send text, image, and video messages to multiple WeChat contacts, including code examples, setup instructions, and troubleshooting tips for Windows environments.
If you need to send the same content to many WeChat friends without manual effort, you can automate the process with Python, which greatly reduces repetitive work and improves efficiency.
Example 1 – Sending Text Messages
import itchat
# Please replace with your WeChat username and password
username = 'your_wechat_username'
password = 'your_wechat_password'
# Specify target users
target_users = ['User1', 'User2', 'User3'] # replace with actual usernames
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if msg['ToUserName'] in target_users:
return 'Hello, %s' % msg['Text']
itchat.auto_login(hotReload=True)
while True:
itchat.run()
itchat.logout()The script defines a list of target users, registers a message handler, and replies with a greeting when a message comes from one of the specified users.
Example 2 – Sending Image Messages
import itchat
# Please replace with your WeChat username and password
username = 'your_wechat_username'
password = 'your_wechat_password'
# Specify target users
target_users = ['User1', 'User2', 'User3'] # replace with actual usernames
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if msg['ToUserName'] in target_users:
return 'Hello, %s' % msg['Text']
itchat.auto_login(hotReload=True)
image_path = 'path_to_your_image.jpg' # replace with your image path
for user in target_users:
itchat.send_image(image_path, toUserName=user)
itchat.logout()This example adds the use of itchat.send_image to deliver an image file to each target user after logging in.
Example 3 – Sending Video Messages
import itchat
# Please replace with your WeChat username and password
username = 'your_wechat_username'
password = 'your_wechat_password'
# Specify target users
target_users = ['User1', 'User2', 'User3'] # replace with actual usernames
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if msg['ToUserName'] in target_users:
return 'Hello, %s' % msg['Text']
itchat.auto_login(hotReload=True)
video_path = 'path_to_your_video.mp4' # replace with your video path
for user in target_users:
itchat.send_video(video_path, toUserName=user)
itchat.logout()The script shows how to send a video file using itchat.send_video . It also notes that the example is basic; real‑world use may require handling different media types, batching, rate limits, and compliance with WeChat policies.
All code should be run on Windows; a QR code will appear for login. If login fails, verify that your WeChat client is up‑to‑date, that login protection is disabled, and that the account has been logged in on the phone. For other issues, consult the ItChat documentation or its GitHub issue tracker.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.