Use python to automatically reply to QQ messages - less than 60 lines

Recently, I was looking at test-related content and found that automated testing is very interesting, so I decided to make a script that automatically replies to QQ messages (I am very good at it)


1. Modules that need to be installed

This automation script needs to use 3 modules. If you want to use this script, you may not have these modules installed in your python, so you can install it.

The first module: This module is mainly used to let the program automatically control a series of operations of the mouse and keyboard to achieve the purpose of automated testing. Enter the installation command under cmd: pip install pyautoguipyautogui


The second module: This module is mainly used to copy the contents of the clipboard and write the contents to the clipboard Enter the installation command under cmd: pip install pyperclippyperclip

The third module: psutil
psutil is a cross-platform library that can easily obtain information about the running process and system utilization (including CPU, memory, disk, network, etc.)
of the system. Enter the installation command under cmd: pip install psutil


2. Overall logic

  • First of all, we need to judge whether QQ is in the logged-in state. If it is not in the logged-in state, we need to start QQ and then log in.
  • Polling to detect whether someone sends a message, if someone sends a message, open the corresponding dialog box
  • Automatically enter the content of the reply and reply
  • Close the dialog box and test again

How to judge whether QQ is activated?

We know that if QQ needs to run, the operating system first allocates resources to it. The resources include a content called process pid. pid is the unique identifier of the process, and the relationship between pid and process is 1:1. You can know the name of the process through pid, and by judging whether the name is equal to "QQ.exe", you can know whether QQ is started

#获取全部进程的pid
pl = psutil.pids()
	for pid in pl:
    # 判断QQ.exe是否运行
    if psutil.Process(pid).name() == "QQ.exe":

If QQ does not start, you need to start QQ and log
in. When we usually start QQ, we first need to find the shortcut of QQ, then double-click, wait until the login interface of QQ appears, and then click to log in or press the Enter key (“enter”) .
Automation is the same.
There is a method os.startfile(dir) in the os module. The parameter is the storage location of "QQ.exe" in the disk. After the login interface appears, we log in by pressing "enter".

os.startfile(QQ_dir)
time.sleep(3)
gui.write(["enter"])
time.sleep(5)

After QQ is successfully logged in (my default is Do Not Disturb), a small QQ icon will appear in the lower right corner of the desktop
insert image description here

If someone sends a message at this time, there will be a prompt
insert image description here

Therefore, at the beginning, we need to take a screenshot of the QQ message prompt, and judge whether there is news by judging whether the desktop icon exists. If there is news, we only need to click the QQ icon to pop up a dialog box.

if gui.locateOnScreen("./image/receive_message1.png", confidence=0.8) is not None:
	gui.click(gui.center(gui.locateOnScreen("./image/receive_message1.png", confidence=0.8)))

After the dialog box pops up, it is in the input state by default. You need to use pyperclip.copy("content to send") to cut the content to the pasteboard, and then use pyautogui.hotkey("ctrl", "v") to paste, the content is already in the input box, press Next "enter" and "esc"(pyautogui.write(["enter", "esc"])) to send, then close the dialog

lip.copy("自动回复")
gui.hotkey("ctrl", "v")
gui.write(["enter", "esc"])

However, when I store the data I need to send in a text, I randomly select one to send each time.

3. Code implementation

import os
import random

import psutil
import pyautogui as gui
import pyperclip as lip
import time

#所有的文件路径都需要自己去修改
QQ_dir = r'D:\Bin\QQScLauncher.exe'

def Proc_exist():
    pl = psutil.pids()
    for pid in pl:
        # 判断QQ.exe是否运行
        if psutil.Process(pid).name() == "QQ.exe":
            return True
    return False


def QQ_login():
    # 启动指定路径下的QQ
    os.startfile(QQ_dir)
    time.sleep(3)
    gui.write(["enter"])
    time.sleep(5)


def Readfile():
    with open("./image/text.txt", 'r', encoding="UTF-8") as f:
        filetxt = f.readlines()
    return filetxt

def Sendmessage(filetxt):
    filetxtlen = len(filetxt)
    #随机数取0到len-1
    ran = random.randint(0, filetxtlen-1)
    #剪切内容
    lip.copy(filetxt[ran])
    #粘贴
    gui.hotkey("ctrl", "v")
    gui.write(["enter", "esc"])


def Polling():
    if Proc_exist() == False:
        QQ_login()

    while True:
        # confidence=0.8是匹配精确度,需要安装opencv   pip install opencv-python
        #判断是否来消息
        if gui.locateOnScreen("./image/receive_message1.png", confidence=0.8) is not None:
            time.sleep(1)
            if gui.locateOnScreen("./image/img.png", confidence=0.8) is not None:
                location = gui.center(gui.locateOnScreen("./image/img.png"))
                gui.click(location.x+200, location.y)
            time.sleep(2)
            Sendmessage(filetxt)

# 将text.txt的数据读到列表中
filetxt = Readfile()
Polling()

Guess you like

Origin blog.csdn.net/qq_56044032/article/details/127125748
Recommended