Method of sending messages to specified contacts by python itchat

  • 2021-06-28 09:24:39
  • OfStack

itchat module

Official Reference Document: https://itchat.readthedocs.io/zh/latest/

install


pip install itchat / pip3 install itchat

principle

Python logs on by mimicking the Web version of WeChat, and there is an existing set of APIs that operate the Web version of WeChat, which allows you to crawl down and process the data you generate using the WeChat.

operation

1. Import package itchat for this set of WeChat API


import itchat

2. Simulate the webpage version of WeChat landing


itchat.auto_login()

3. Use the related function to find the relevant WeChat contact information (returned here is an JOSN array)


users=itchat.search_friends(" Uncle Fei Brother")

4. Get the username of the contact (see what data is inside JOSN that you can print out by yourself)


userName= users[0]['UserName']

5. Send information to related contacts


itchat.send(' Hello Uncle Fly Brother',toUserName=userName)

At this point, it will be sent successfully.


import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')

This code means to send an hello to filehelper, who is the file assistant.

So we want to send a message to the specified person, not so easy as to change filehelper


users=itchat.search_friends(" King ")
userName= users[0]['UserName']
print(userName)
itchat.send(' Hello King ',toUserName=userName)

If we want to send a message to Lao Wang, first use the itchat.search method, we will find out all the contacts with the name Lao Wang in the note.

Then we pick the first one (if you only have one king in your contact list, only one will be found)

users [0] takes a contact object with an key called UserName, which is the real user's username

Then we can use the itchat.send method to send the message to the King successfully.


Related articles: