Itchat is an open source wechat personal number interface, you can use this library for all operations in wechat web version, such as: all friends, add friends, pull friends group chat, wechat robot and so on. For details, please refer to the documentation, here.

This paper mainly uses this library to complete a Turing machine that can process wechat messages, including friend chat and group chat.

This post is a compilation of Python materials that beginners may need, including books/videos/online documentation and editors/sources

Installation of Python Qun: 850973621

1. Installation of itchat library

pip install itchat

Copy the code

After installation, run the following code, and a TWO-DIMENSIONAL code will appear. After scanning the code, you will log in to the wechat web page.

2,

Import itchat # login itchat.auto_login() # set hotReload = True # Run and stay online itchat.run()Copy the code

In addition, ItChat also provides the function of disconnecting and reconnecting within a short period of time. You only need to add the parameter hotReload = True. When you log in next time, you don’t need to scan the QR code.

3. Message sending

Itchat library can send text, pictures, videos, attachments and other contents, such as sending messages to wechat file Transfer Assistant, as follows:

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

Copy the code

ToUserName is the wechat signal of the person who wants to send the message. You can click on the wechat mobile terminal to query, or use the search_friends function in itChat library to search, return the wechat signal, and find the official document for detailed usage.

4. Message reception

# # # # # # # # # # # # # # # # # # # # # # complete code # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # to load the library from itchat. Content import * import requests the import json Import itchat itchat.auto_login(hotReload = True) # def tuling(info): import itchat itchat.auto_login(hotReload = True) # def tuling(info): appkey = "e5ccc9c7c8834ec3b08940e290ff1559" url = "http://www.tuling123.com/openapi/api?key=%s&info=%s"%(appkey,info) Req = requests. Get (URL) content = req.text data = json.loads(content) answer = data['text'] return answer # Def group_id(name) def group_id(name): Df = itchat.search_chatrooms(name=name) return df[0]['UserName'] Bound to the text_reply handler # text_reply msg_files can handle chat between friends reply. @ itchat msg_register ([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): itchat.send('%s' % tuling(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(TEXT, isGroupChat=True) def group_text_reply(MSG): @itchat.msg_register(TEXT, isGroupChat=True) def group_text_reply(MSG) Item = group_id(u' ToUserName') # set MSG ['ToUserName'] == item: itchat.send(u'%s' % tuling(msg['Text']), item) itchat.run()Copy the code

So, below your friends, wechat groups can happily chat with the Turing robot ~~~

Author: DT0203 links: www.jianshu.com/p/5d4de51f5… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.