SoFunction
Updated on 2025-04-13

Detailed explanation of the use of Python WeChat automation library

In Python, there are several libraries that can be used for WeChat automation, including itchat, wxpy, wechatpy, and wxauto. The following is a detailed introduction to these libraries

1. itchat

Introduction

itchat is a Python library based on WeChat web version, which supports the automated operations of personal WeChat accounts, such as logging in, sending messages, receiving messages, etc.

Prerequisites

  • WeChat account: A personal WeChat account is required.
  • Python environment: Python needs to be installed.
  • Network connection: Need to be able to access the WeChat server.

Dependencies

  • itchatLibrary.
  • requestsLibrary(itchatThe dependent network request library).

Installation dependencies

pip install itchat

Things to note

  • WeChat restrictions: WeChat officials are increasingly restricting unofficial clients, which may lead to account bans.
  • Login method: You need to scan the code to log in, and the login status may expire.
  • Support personal WeChat only: WeChat official accounts or corporate WeChat are not supported.

Complete code example

import itchat

# Log in to WeChatitchat.auto_login(hotReload=True)  # hotReload=True You can log in again in a short time without rescanning the code
# Send a message("Hello, this is a test message!", toUserName='filehelper')  # Send to the file transfer assistant
# Get a friend listfriends = itchat.get_friends()
print("Friend List:")
for friend in friends:
    print(friend['NickName'])  # Print your friend's nickname
# Listen to [email protected]_register()
def text_reply(msg):
    print(f"Received a message: {msg['Text']}")
    return f"Automatic reply: {msg['Text']}"

# Stay running()

Code Comments

  • itchat.auto_login(hotReload=True): Log in to WeChat,hotReload=TrueIt means hot loading, and you can log in again in a short time without rescanning the code.
  • (): Send a message,toUserNameParameters specify the recipient,filehelperIndicates file transfer assistant.
  • itchat.get_friends(): Get the friend list and return a list containing friend information.
  • @itchat.msg_register(): Register a message handling function, which is called when a text message is received.
  • (): Keep WeChat online and listen to messages.

2. wxpy

Introduction

wxpyIt is based onitchatThe packaging library provides a more concise API, suitable for rapid development of WeChat robots.

Prerequisites

  • WeChat account: A personal WeChat account is required.
  • Python environment: Python needs to be installed.
  • Network connection: Need to be able to access the WeChat server.

Dependencies

  • wxpyLibrary.
  • itchatLibrary(wxpyIt is based onitchatpackage).
  • requestsLibrary.

Installation dependencies

pip install wxpy

Things to note

  • WeChat restrictions:anditchatSimilarly, there is a risk of being banned.
  • Login method: You need to scan the code to log in.
  • Support personal WeChat only: WeChat official accounts or corporate WeChat are not supported.

Complete code example

from wxpy import *

# Initialize the robot, scan the code to log inbot = Bot()

# Send a messagebot.file_helper.send("Hello, this is a test message!")  # Send to the file transfer assistant
# Get a friend listfriends = ()
print("Friend List:")
for friend in friends:
    print(friend.nick_name)  # Print your friend's nickname
# Listen to messages@()
def reply_my_friend(msg):
    print(f"Received a message: {}")
    return f"Automatic reply: {}"

# Stay runningembed()

Code Comments

  • Bot(): Initialize a WeChat robot and scan the code to log in.
  • bot.file_helper.send(): Send a message to the file transfer assistant.
  • (): Get the friend list and return a list containing friend information.
  • @(): Register a message processing function and call it when a message is received.
  • embed(): Keep WeChat online and listen to messages.

3. wechatpy

Introduction

wechatpyIt is a Python SDK for WeChat public platform and WeChat enterprise account, which supports the development of WeChat public account and enterprise account.

Prerequisites

  • WeChat official account or corporate WeChat account: You need to have registered and configured your WeChat official account or corporate WeChat.
  • Python environment: Python needs to be installed.
  • Network connection: Need to be able to access the WeChat server.
  • API permissions: You need to obtain a WeChat official account or corporate WeChatapp_idandapp_secret

Dependencies

  • wechatpyLibrary.
  • requestsLibrary.

Installation dependencies

pip install wechatpy

Things to note

  • Only support public accounts and corporate WeChat: Personal WeChat is not supported.
  • API Limitations: It is necessary to comply with the official WeChat API call frequency limit.
  • Complex configuration: The server URL, token, etc. need to be configured.

Complete code example

from wechatpy import WeChatClient

# Initialize the WeChat clientclient = WeChatClient('your-app-id', 'your-app-secret')

# Send template messagetemplate_id = 'your-template-id'
openid = 'user-openid'
data = {
    'first': {'value': 'Hello, this is a test message!'},
    'remark': {'value': 'This is a remark.'}
}
.send_template(openid, template_id, data)

# Get the user listusers = ()
print("User List:")
for user in users['data']['openid']:
    print(user)

# Obtain user informationuser_info = (openid)
print(f"User Information: {user_info}")

Code Comments

  • WeChatClient('your-app-id', 'your-app-secret'): Initialize the WeChat client and provide a WeChat official account.app_idandapp_secret
  • .send_template(): Send template message,openidis the unique identifier of the user.template_idIt is the ID of the template message.dataIt is the content of the template message.
  • (): Get the user list and return a user containing itopenidlist.
  • (openid): Get information about the specified user.

4. wxauto

Introduction

wxautoIt is a WeChat client automation library based on the Windows operating system. It realizes the automation of WeChat by simulating user operations (such as mouse clicks, keyboard input, etc.).

Prerequisites

  • Windows SystemwxautoOnly run on Windows.
  • WeChat client: You need to install the WeChat client and keep it logged in.
  • Python environment: Python needs to be installed.
  • Screen resolution: Scripts may be sensitive to screen resolution and need to be adapted.

Dependencies

  • wxautoLibrary.
  • pywin32orpywinauto(For Windows GUI automation).
  • Pillow(For image processing, if screenshots or image recognition is required).

Installation dependencies

pip install wxauto pywin32 Pillow

Things to note

  • Only supported on Windows: Cannot run on macOS or Linux.
  • WeChat client version: The script may be sensitive to the WeChat client version and needs to be adapted.
  • Stability issues: Because it is GUI-based automation, script failure may occur due to changes in window location or network delay.

Complete code example

import time
from wxauto import WeChat

# Initialize the WeChat clientwx = WeChat()

# Start WeChat()

# Wait for WeChat to start(10)

# Get the current WeChat windowwx.get_window()

# Find a friend or group chat('Friend or group chat name')

# Send a message('Hello, this is a test message!')

# Receive messagemessages = wx.get_messages()
for message in messages:
    print(f"Received a message: {message}")

# Close WeChat()

Code Comments

  • WeChat(): Initialize the WeChat client.
  • (): Start the WeChat client.
  • (10): Wait for the WeChat client to start, wait for 10 seconds here.
  • wx.get_window(): Get the current WeChat window.
  • ('Friend or group chat name'): Find a friend or group chat.
  • ('Hello, this is a test message!'): Send a message.
  • wx.get_messages(): Get the message.
  • (): Close the WeChat client.

Summarize

Library name Support platform WeChat Type Dependencies Things to note
itchat Cross-platform Personal WeChat itchatrequests May be blocked, you need to scan the code to log in
wxpy Cross-platform Personal WeChat wxpyitchatrequests based onitchat, more functions, but also has the risk of banning the account
wechatpy Cross-platform Official account/Company WeChat wechatpyrequests Requires API configuration, only support official accounts and enterprise WeChat
wxauto Windows Personal WeChat wxautopywin32Pillow Only support Windows, rely on WeChat client, poor stability

Select the appropriate library for development according to your needs. If you need to develop a personal WeChat robot,itchatandwxpyIt is a good choice; if you need to develop WeChat official account or enterprise account application,wechatpyIt is a better choice; if you need to operate the WeChat client on Windows, you can usewxauto

The above is the detailed explanation of the use of Python WeChat automation library. For more information about Python WeChat automation library, please follow my other related articles!