Itchat is an open source wechat id interface, and using Python to call wechat has never been easier.

With less than 30 lines of code, you can build a wechat robot that can process all the information.

Of course, there’s more to the API than just one robot, and there’s more to discover, like these.

Now wechat has become a large part of personal social communication, I hope this project can help you expand your personal wechat and facilitate your life.

The installation

To install itchat, run this command:

pip install itchatCopy the code

Give it a try

This is a small open source robot based on this project, once seen is better than once heard, interested can try.

screenshots

Advanced applications

Command line QR code

The following command can be used to display the TWO-DIMENSIONAL code when logging in:

itchat.auto_login(enableCmdQR=True)Copy the code

Some systems may have different font widths, which can be adjusted by setting enableCmdQR to a specific multiple:

Itchat. Auto_login (enableCmdQR=2)Copy the code

The default console background color is dark (black). If the background color is light (white), we can set enableCmdQR to a negative value:

itchat.auto_login(enableCmdQR=-1)Copy the code

Users to search

You can use the search_friends method to search for users in the following four ways: 1. Get the user information of the specified UserName. 3. Users who obtain any item in remarks, wechat signal or nickname equal to the name key value 4. Get the user whose remarks, wechat signal and nickname are equal to the corresponding key value respectively

Three or four of them can be used together. Here is a sample program:

# Get your user information, Itchat.search_friends (UserName ='@abcdefg1234567') # return itchat.search_friends() # Itchat.search_friends (name='littlecodersh') # Getuser for each key Itchat.search_friends (wechatAccount=' Littlecodersh ') # wechatAccount='littlecodersh')Copy the code

Access and search for public accounts and group chats are described in more detail in the document.

The end user

To enable multiple operations, run the following command:

import itchat

newInstance = itchat.new_instance()
newInstance.auto_login(hotReload=True, statusStorageDir='newInstance.pkl')

@newInstance.msg_register(TEXT)
def reply(msg):
    return msg['Text']

newInstance.run()Copy the code

Frequently asked Questions and answers

Q: Why can’t Chinese files be uploaded?

A: This is due to encoding problems in Requests. To support Chinese file transfers, place the fields.py(py3 version here) file under packages/urllib3 of the Requests package

Q: Why can’t I display the QR code on the command line after SETTING enableCmdQR to True for itchat.auto_login()?

A: This is due to the absence of the optional package Pillow, which can be installed using the command on the right: PIP Install Pillow

Q: How to turn your wechat signal into a controller through this package?

A: There are two ways to send and receive A message from your UserName. Sends and receives a message from fileHelper

Q: Why did some of the messages I sent fail to be sent?

A: Some accounts cannot send messages to their own accounts by nature. It is recommended to use FileHelper instead.

The resources

Liuwons /wxBot: A similar Python-based wechat robot

Zixia /wechaty: NodeJS framework/library for wechat personal account robot based on Javascript(ES6)