English version
itchat is an open source WeChat personal account interface. It has never been easier to call WeChat using python.
With less than thirty lines of code, you can complete a WeChat bot that can handle all information.
Of course, the use of this API is far more than just a robot, and there are more functions waiting for you to discover, such as these.
This interface shares a similar operation method with the public account interface itchatmp, and you can learn to master two tools at one time.
Nowadays, WeChat has become a large part of personal social interaction. I hope this project can help you expand your personal WeChat account and facilitate your life.
You can install itchat with this command:
pip install itchat
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' )
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 ()
Some advanced applications can be seen in the source code and advanced applications of the open source robot below, or you can also read the documentation.
This is an open source small robot based on this project. It’s better to hear about it than to see it. If you are interested, you can try it.
Because the number of friends is growing too fast, the demonstration of the function of automatically passing friend verification is temporarily closed.
By printing the user of itchat and the parameters of the registration message, you can find that these values are all dictionaries.
But in fact, itchat has carefully constructed corresponding messages, users, group chats, and public accounts.
All its key values can be accessed this way:
@ itchat . msg_register ( TEXT )
def _ ( msg ):
# equals to print(msg['FromUserName'])
print ( msg . fromUserName )
The attribute name is the lowercase first letter of the key value.
author = itchat . search_friends ( nickName = 'LittleCoder' )[ 0 ]
author . send ( 'greeting, littlecoder!' )
Through the following code, WeChat can already obtain and reply to various daily information.
import itchat , time
from itchat . content import *
@ itchat . msg_register ([ TEXT , MAP , CARD , NOTE , SHARING ])
def text_reply ( msg ):
msg . user . send ( '%s: %s' % ( msg . type , msg . text ))
@ itchat . msg_register ([ PICTURE , RECORDING , ATTACHMENT , VIDEO ])
def download_files ( msg ):
msg . download ( msg . fileName )
typeSymbol = {
PICTURE : 'img' ,
VIDEO : 'vid' , }. get ( msg . type , 'fil' )
return '@%s@%s' % ( typeSymbol , msg . fileName )
@ itchat . msg_register ( FRIENDS )
def add_friend ( msg ):
msg . user . verify ()
msg . user . send ( 'Nice to meet you!' )
@ itchat . msg_register ( TEXT , isGroupChat = True )
def text_reply ( msg ):
if msg . isAt :
msg . user . send ( u'@%s u2005 I received: %s' % (
msg . actualNickName , msg . text ))
itchat . auto_login ( True )
itchat . run ( True )
You can use the command line to display the QR code when logging in with the following command:
itchat . auto_login ( enableCmdQR = True )
Some systems may have different character widths, which can be adjusted by assigning enableCmdQR to a specific multiple:
# 如部分的linux系统,块字符的宽度为一个字符(正常应为两字符),故赋值为2
itchat . auto_login ( enableCmdQR = 2 )
The default console background color is dark (black). If the background color is light (white), enableCmdQR can be assigned a negative value:
itchat . auto_login ( enableCmdQR = - 1 )
Log in through the following command. Even if the program is closed, you can reopen it within a certain period of time without re-scanning the code.
itchat . auto_login ( hotReload = True )
Use the search_friends
method to search for users. There are four search methods:
UserName
name
key valueThree or four of them can be used together. The following is a sample program:
# 获取自己的用户信息,返回自己的属性字典
itchat . search_friends ()
# 获取特定UserName的用户信息
itchat . search_friends ( userName = '@abcdefg1234567' )
# 获取任何一项等于name键值的用户
itchat . search_friends ( name = 'littlecodersh' )
# 获取分别对应相应键值的用户
itchat . search_friends ( wechatAccount = 'littlecodersh' )
# 三、四项功能可以一同使用
itchat . search_friends ( name = 'LittleCoder机器人' , wechatAccount = 'littlecodersh' )
The acquisition and search of official accounts and group chats are described in more detail in the documentation.
Itchat's attachment download method is stored in the Text key of msg.
The filename of the file sent (the default filename given by the picture) is stored in the FileName key of msg.
The download method accepts an available positional argument (including the file name) and stores the file accordingly.
@ itchat . msg_register ([ PICTURE , RECORDING , ATTACHMENT , VIDEO ])
def download_files ( msg ):
msg . download ( msg . fileName )
itchat . send ( '@%s@%s' % (
'img' if msg [ 'Type' ] == 'Picture' else 'fil' , msg [ 'FileName' ]),
msg [ 'FromUserName' ])
return '%s received' % msg [ 'Type' ]
If you don't need to download it locally and just want to read the binary string for further processing, you can pass in no parameters. 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 . download ())
Use the following command to complete multiple open operations:
import itchat
newInstance = itchat . new_instance ()
newInstance . auto_login ( hotReload = True , statusStorageDir = 'newInstance.pkl' )
@ newInstance . msg_register ( itchat . content . TEXT )
def reply ( msg ):
return msg . text
newInstance . run ()
The method after the login is completed needs to be assigned in loginCallback
.
The method after exit 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 ()
If the value of loginCallback is not set, the QR code image will be automatically deleted and the command line display will be cleared.
Q: How can I turn my WeChat ID into a controller through this package?
A: There are two ways: sending and receiving messages of your own UserName; sending and receiving messages of the file transfer assistant (filehelper)
Q: Why are some of the messages not sent successfully when I send them?
A: Some accounts are inherently unable to send messages to their own accounts. It is recommended to use filehelper
instead.
LittleCoder: Architecting and maintaining Python2 and Python3 versions.
tempdban: protocol, architecture and daily maintenance.
Chyroc: Completed the first version of the Python3 architecture.
youfou/wxpy: Excellent api packaging and supporting plug-ins, WeChat robot/elegant WeChat personal account API
liuwons/wxBot: Similar Python-based WeChat robot
zixia/wechaty: WeChat personal account robot NodeJS framework/library based on Javascript (ES6)
sjdy521/Mojo-Weixin: WeChat client framework written in Perl language, which can provide an API interface based on HTTP protocol through plug-ins for other languages to call
HanSon/vbot: WeChat personal account robot based on PHP7, which can easily implement various customized functions by implementing anonymous functions
yaphone/itchat4j: Using Java to expand personal WeChat ID capabilities
kanjielu/jeeves: WeChat robot developed using springboot
If you have any questions or suggestions, you can discuss it with me in this Issue
Or you can also communicate on gitter:
Of course, you can also join our new QQ group discussion: 549762872, 205872856