2022 Spring Festival blessing wechat automatic reply

As we all know, when we play games or watch the Spring Festival Gala all night, if your friends and relatives send you Spring Festival greetings at this time, and you happen to be in the group war or see the wonderful part, we can't take the time to reply to them. In case they call directly and interrupt our good time, it's easy to make us happy s.

When the fish died, I made this automatic Fu and Yan system in Python to help us automatically bless others and enjoy a happy holiday.

Look at the library first!

import time
import random
import tkinter as tk
from tkinter import ttk, messagebox as msg
from PIL import ImageGrab,ImageChops,Image
import pymouse, pykeyboard, pyperclip

A tkinter is used to create a window, which is convenient for everyone to operate on their own computer

PIL library provides screenshots and image comparison to facilitate response to message updates

As for pymouse, pykeyboard and pyperclip, they are all old faces. Keyboard input, mouse acquisition and message transmission

After blue, it is to create a window!

mainForm = tk.Tk()
mainForm.title('Automatic perfunctory system')
mainForm.resizable(0, 0)

msgText = tk.Text(mainForm, height=40, width=40)
msgText.insert(0.0, "Write down your favorite perfunctory words here")
msgText.grid(row=0, padx=5, pady=5)

Because everyone's New Year wishes are different, so I put a text input box here, so that people with good literary talent can play at will. emoji also supports it 😯!

mouseInputXY = (-1, -1)
mousePeopleXY = (-1,-1)
mouseUPXY = (-1,-1)
mouseDOWNXY = (-1,-1)

def onClickMouseButton():
    global mouseInputXY ,mouseUPXY ,mouseDOWNXY ,mousePeopleXY
    m = pymouse.PyMouse()
    msg.showinfo('success', 'Successful, please move the mouse to the input box of wechat within 2 seconds after clicking to confirm, and automatically obtain the mouse position of the input box after 2 seconds.')
    time.sleep(2)
    mouseInputXY = m.position()
    msg.showinfo('success', 'Successful, please move the mouse to the first person on wechat within 2 seconds after clicking to confirm, and automatically obtain the mouse position after 2 seconds.')
    time.sleep(2)
    mousePeopleXY = m.position()
    msg.showinfo('success', 'Successful, please move the mouse to the upper left end of wechat chat box within 2 seconds after clicking to confirm, and automatically obtain the mouse position after 2 seconds.')
    time.sleep(2)
    mouseUPXY = m.position()
    msg.showinfo('success', 'Successful, please move the mouse to the lower right of wechat chat box within 2 seconds after clicking confirm, and automatically obtain the mouse position after 2 seconds.')
    time.sleep(2)
    mouseDOWNXY = m.position()
    msg.showinfo('success', 'Task completed successfully')

Since everyone's computer is different, naturally the input position of wechat QQ is different, and this function is to make the perfunctory system more appropriate and understand you better.

Hailan's house, men's wardrobe (I don't know why I suddenly thought of this advertisement)

def compare_images(path_one, path_two):
    image_one = Image.open(path_one)
    image_two = Image.open(path_two)
    try:
        diff = ImageChops.difference(image_one, image_two)
        if diff.getbbox() is None:
            # If there is no difference between pictures, exit directly
            print("No message!")
            return False
        else:
            print('New!')
            return True
    except ValueError:
        pass

This is the function of comparing desktop screenshots! If there is a change, our program will know that there is a message (of course, it can't distinguish between group messages and personal messages, and will be perfunctory [crying])

mouseButton = ttk.Button(mainForm, text='Click to get mouse position', width=40, command=onClickMouseButton)
mouseButton.grid(row=2, padx=5, pady=5)

Oh, I almost forgot. Of course, there's a button for starting. You can't lose it yee

def onClickStartButton():
    # Determine whether the mouse position has been read
    if mouseInputXY == (-1, -1):
        msg.showerror('error', 'Please get the mouse position first')
        return
    msgContent = msgText.get(0.0, tk.END).rstrip()
    if msgContent == 'Write down your favorite perfunctory words here' or msgContent == '':
        msg.showerror('error', 'Please enter your perfunctory words first')
        return
    if not msg.askokcancel('Tips', 'Start perfunctory'):
        return
    m = pymouse.PyMouse()  # Get mouse object
    k = pykeyboard.PyKeyboard()  # Get keyboard object
    lst = msgContent.split()
    size = (mouseUPXY[0], mouseUPXY[1], mouseDOWNXY[0], mouseDOWNXY[1])
    print(size)
    path_one, path_two = r"cut1.jpg", r"cut2.jpg"
    while True:
        img = ImageGrab.grab(size)
        image = img.convert('RGB')
        image.save(path_one)
        time.sleep(5)
        img = ImageGrab.grab(size)
        image = img.convert('RGB')
        image.save(path_two)
        if compare_images(path_one,path_two):
            m.click(mousePeopleXY[0], mousePeopleXY[1], n=2)
            m.click(mouseInputXY[0], mouseInputXY[1], n=2)
            pyperclip.copy(random.choice(lst))
            k.press_keys(['Command', 'V'])
            time.sleep(1)
            k.press_keys(['Return'])
            time.sleep(1)
        m.click(mouseInputXY[0], mouseInputXY[1])

Well, the core part, the ultimate meaning is

namely

namely

White whoring, directly spell all the above code segments, and then change the path of image storage, you can use it

Go and use it

Go, go

What are you still looking at

Don't look at me. Look at the code

what?

You say incomplete?

My pleasure.

You see

startButton = ttk.Button(mainForm, text='start-up', width=40, command=onClickStartButton)
startButton.grid(row=3, padx=5, pady=5)

mainForm.mainloop()

It's not complete. Add this paragraph and you can use it

what? Not complete?!

If you still want to be perfunctory, are you so lazy (woo woo)

ok

Then I'll stick it here

# Happy New Year!!!

Finally, I wish you all the best, such as the East China Sea, longevity and better day by day!!!

Oh, yeah!

Keywords: Python wechat Simulation

Added by plutoplanet on Thu, 27 Jan 2022 10:47:09 +0200