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
With ItChat, if you want to send a message to the File Transfer Assistant, just do this:
import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')Copy the code
If you want to reply to a text message sent to yourself, just do this:
import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
return msg['Text']
itchat.auto_login()
itchat.run()Copy the code
Some of the advanced applications can be found in the open source robot source code and advanced applications below, or you can read the documentation.
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
Through the following code, wechat has been able to obtain and reply to all kinds of daily information.
#coding=utf8 import itchat, time from itchat.content import * @itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName']) @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) def download_files(msg): msg['Text'](msg['FileName']) return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName']) @itchat.msg_register(FRIENDS) def add_friend(msg): Itchat. add_friend(** MSG ['Text']) # itchat.send_msg('Nice to meet you! ', msg['RecommendInfo']['UserName']) @itchat.msg_register(TEXT, isGroupChat=True) def text_reply(msg): if msg['isAt']: itchat.send(u'@%s\u2005I received: %s' % (msg['ActualNickName'], msg['Content']), msg['FromUserName']) itchat.auto_login(True) itchat.run()Copy the code
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
Log in through the following command, even if the program is closed, restart within a certain period of time can also not scan code.
itchat.auto_login(hotReload=True)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.
Itchat’s attachment download method is stored in the Text key of MSG.
The FileName of the sent file (the default FileName given by the image) is stored in the MSG FileName key.
The download method takes an available location parameter (including the filename) and stores the file accordingly.
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def download_files(msg):
msg['Text'](msg['FileName'])
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
return '%s received'%msg['Type']Copy the code
If you don’t need to download it locally and just want to read the binary string for further processing without passing in the argument, the method will return the binary string of the image.
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def download_files(msg):
with open(msg['FileName'], 'wb') as f:
f.write(msg['Text']())Copy the code
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
The method that completes the login needs to be assigned in the loginCallback.
The exit method needs to be assigned in exitCallback.
import time
import itchat
def lc():
print('finish login')
def ec():
print('exit')
itchat.auto_login(loginCallback=lc, exitCallback=ec)
time.sleep(3)
itchat.logout()Copy the code
If the value of loginCallback is not set, the QR code image will be automatically deleted and the command line display will be cleared.
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.
LittleCoder: Architecture and maintenance Python2 Python3.
Tempdban: Protocol, architecture and routine maintenance.
Chyroc: Complete the first version of Python3 architecture.
The resources
Liuwons /wxBot: A similar Python-based wechat robot
Zixia /wechaty: NodeJS framework/library for wechat personal account robot based on Javascript(ES6)
Can you discuss with me on this Issue if you have any questions or suggestions
Or you can communicate in Gitter:
Of course, you can also join our new QQ group discussion: 549762872