preparation:
Install itchat: install command pip install itchat
1. Import the itchat module and send a message to the file transfer assistant
import itchat itchat.auto_login(hotReload=True) itchat.send("Hello, file transfer assistant",toUserName='filehelper')
Run the code, scan the QR code, that is, log in the wechat page version, and automatically transfer "Hello, file transfer assistant" to the mobile phone.
The purpose of itchat. Auto login (hotreload = true) is to keep you logged in. In the near future, you don't need to scan QR code.
itchat.send() is the sending message. In "" is the content to be sent, and toUserName is the object to be sent. In wechat, the file transfer assistant is filehelper.
2. If you want to send it to the designated person. It is not easy to replace the filehelper. First search for the comment name of the person you want to send.
import itchat itchat.auto_login(hotReload=True) users= itchat.search_friends("A starry night") userName = users[0]['UserName'] itchat.send("Starry night, how are you",toUserName=userName)
See https://blog.csdn.net/Lynn_coder/article/details/79436539 for details
3. Process the text information and let the machine repeat the words
from itchat.content import TEXT import itchat @itchat.msg_register(TEXT) def text_reply(msg): print(msg) return msg.text itchat.auto_login(hotReload=True) itchat.run()
4. Create a new folder named file to process image and video information, and let the machine return the same message
import itchat import os from itchat.content import PICTURE,VIDEO @itchat.msg_register([PICTURE , VIDEO]) def text_reply(msg): file_name = os.path.join('file',msg.fileName) msg.download(file_name) #download Is to write the file stream to the file corresponding to the file name msg_type = { PICTURE: 'img', VIDEO: 'vid' }.get(msg.type,'fil') return '@%s@%s' % (msg_type,file_name) itchat.auto_login(hotReload=True) itchat.run()
5. Apply for the Turing robot account, and use the Turing robot to automatically reply the text and picture messages to your friends
import itchat import os import requests from itchat.content import TEXT,PICTURE,VIDEO,SHARING KEY = '' Here's what Turing robots are talking about key def get_response(msg,uid): api_url = 'http://www.tuling123.com/openapi/api' data = {'key':KEY,'info':msg,'userid':uid} r = requests.post(api_url,data=data).json() return r.get('text','') @itchat.msg_register(TEXT) def reply_text(msg): msg_text = msg.text reply = get_response(msg_text, msg.FromUserName) return reply @itchat.msg_register([PICTURE,VIDEO,SHARING]) def download_file(msg): file_name = os.path.join('file',msg.fileName) msg.download(file_name) msg_type = msg.type msg_type = {PICTURE:'img',VIDEO:'vid'}.get(msg_type,'fil') return '@%s@%s' %(msg_type,file_name) itchat.auto_login(hotReload=True) itchat.run()
6. If you want to reply to a message in the group, you only need to add the content in quotation marks at the end of each decorator bracket content ", isGroupChat=True". The decorator is the line starting with @ and the code is marked in red.