Python and WeChat take a picture

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


Ever since WeChat launched the Take a Shot function, it has been broken by talented netizens.

 

There are more ways to beat without temperament, not shown here.

But taking a shot is a weak reminder and can only be felt in the chat interface. If you are not on the WeChat interface and you can’t respond in time if someone is photographed, here is how to use PyWeChatSpy ( https://github.com/veikai/PyWeChatSpy ) to respond with emoticons and take a picture .

1. First, we prepare some emoticons, here I chose the sand sculpture panda head


2. Install the PC WeChat client version 2.8.0.133 ( https://share.weiyun.com/5AwuXRG )

 

3. Students who install Python3.8.3_x64 32-bit system ( https://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exe ) can go to the official website to download the 32-bit installation package by themselves


4. Open cmd, enter pip install PyWeChatSpy and press Enter to execute


5. At this time we need to code, first create a new app.py file

 

  • First introduce the PyWeChatSpy module, the regular re module and the random random module re to match the content of the message, and random to randomly select the reply image
from PyWeChatSpy import WeChatSpy
import random
import re
  • Then define a reply processing function my_parser
def my_parser(data):
    pass
  • Then instantiate a WeChatSpy class
spy = WeChatSpy(parser=my_parser)
  • Finally write the my_parser function processing logic
if data["type"] == 5: # 判断是微信消息数据
    for msg in data["data"]: # 遍历微信消息
        if msg["msg_type"] == 10000:  # 判断是微信拍一拍系统提示
            # 因为微信系统消息很多 因此需要用正则匹配消息内容进一步过滤拍一拍提示
            # {'self': 0, 'msg_type': 10000, 'wxid1': '179xxxxxx72@chatroom', 'content': '"Mandy的小脑袋" 拍了拍你'}
            m = re.search('".*" 拍了拍你', msg["content"])
            if m:  # 搜索到了匹配的字符串 判断为拍一拍
                image_path = f"images/{random.randint(1, 7)}.jpg"  # 随机选一张回复用的图片
                spy.send_file(msg["wxid1"], image_path)  # 发送图片
  • Run code
if __name__ == '__main__':
    spy.run()  # 运行代码

You're done. At this time, if someone takes another picture of you, whether it is a group chat or a private chat, it will automatically reply to the set picture.

 

The overall code is as follows:


from PyWeChatSpy import WeChatSpy
import random
import re


def my_parser(data):
    if data["type"] == 5: # 判断是微信消息数据
        for msg in data["data"]:  # 遍历微信消息
            if msg["msg_type"] == 10000:  # 判断是微信拍一拍系统提示
                # 因为微信系统消息很多 因此需要用正则匹配消息内容进一步过滤拍一拍提示
                # {'self': 0, 'msg_type': 10000, 'wxid1': '179xxxxxx72@chatroom', 'content': '"Mandy的小脑袋" 拍了拍你'}
                m = re.search('".*" 拍了拍你', msg["content"])
                if m:  # 搜索到了匹配的字符串 判断为拍一拍
                    image_path = f"images/{random.randint(1, 7)}.jpg"  # 随机选一张回复用的图片
                    spy.send_file(msg["wxid1"], image_path)  # 发送图片


spy = WeChatSpy(parser=my_parser)  # 实例化WeChatSpy类


if __name__ == '__main__':
    spy.run()  # 运行代码

Note: The storage location of app.py must be the same as the image folder, otherwise it will not respond correctly. For example, the image path is D:\images, and the storage path of app.py is D:\app.py. Chinese paths are not currently supported

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112853972