python
主页 > 脚本 > python >

Python实现自动回复QQ消息功能的代码

2022-10-05 | 佚名 | 点击:

最近在看测试相关的内容,发现自动化测试很好玩,就决定做一个自动回复QQ消息的脚本(我很菜)

1、需要安装的模块

这个自动化脚本需要用到3个模块,如果要使用这个脚本的朋友,自己的python中可能没有安装这些模块,所以就可以安装一下

第1个模块:pyautogui

这个模块主要是用来让程序自动控制鼠标和键盘的一系列操作来达到自动化测试的目的。

在cmd下输入安装命令:pip install pyautogui

第2个模块:pyperclip

这个模块主要用于复制剪贴板里的内容,向剪贴板写入内容

在cmd下输入安装命令:pip install pyperclip

第3个模块:psutil

psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息

在cmd下输入安装命令:pip install psutil

2、整体逻辑

如何判断QQ是否处启动?

我们知道QQ如果需要运行,首先操作系统给它分配资源,资源中就包括一个叫进程pid的内容,pid是进程的唯一标识,pid和进程是1:1的关系。通过pid就可以知道进程的名字,判断这个名字是否与"QQ.exe"相等就可以知道QQ是否启动

1

2

3

4

5

#获取全部进程的pid

pl = psutil.pids()

    for pid in pl:

    # 判断QQ.exe是否运行

    if psutil.Process(pid).name() == "QQ.exe":

如果QQ没有启动,需要启动QQ,并登录

我们平时启动QQ的时候,首先需要找到QQ的快捷方式,然后双击,等到QQ的登录界面出现后,再点击登录或者按回车键(“enter”)。

自动化也是一样的。

os模块中有一个方法os.startfile(dir),参数就是"QQ.exe"在磁盘中的存放位置,登录界面出现后,我们这里采用按回车(“enter”)的方式进行登录

1

2

3

4

os.startfile(QQ_dir)

time.sleep(3)

gui.write(["enter"])

time.sleep(5)

QQ成功登录后(我的默认是免打扰),在桌面的右下角会出现QQ的小图标

如果此时有人发来消息,则会有提示符

因此最开始,就需要对这个QQ消息提示符进行截图,通过判断桌面这个图标是否存在,来判断是否有消息,如果有消息,我们只需要点击这个QQ这个图标,就能弹出对话框

1

2

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)))

弹出对话框后,默认是处于输入状态的。需要用到pyperclip.copy(“需要发送的内容”)剪切内容到粘贴板上,再使用pyautogui.hotkey(“ctrl”, “v”)进行粘贴,此时内容就已经在输入框中,按下"enter"和"esc"(pyautogui.write([“enter”, “esc”]))进行发送,然后关闭对话框

1

2

3

lip.copy("自动回复")

gui.hotkey("ctrl", "v")

gui.write(["enter", "esc"])

不过在我是将我需要发送的数据存放在了一个文本里,每次都随机选择一个发送

3、代码实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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()

原文链接:https://blog.csdn.net/qq_56044032/article/details/127125748
相关文章
最新更新