I made an automatic message sending program in Python, so it doesn’t delay things even if people leave, it’s so easy to use

I. Introduction

Sometimes it is very labor-saving to let the hands free and let the computer automatically send some messages we want to send.

For example, after writing a speech during the day and giving a text speech in a group at night, we can use scripts to automatically copy, paste and send text, thereby liberating ourselves and not having to do CV repeatedly on the computer in person. Tiring work.
.For
example, if you are running a live broadcast and need someone to post the barrage, you can use the program that automatically sends messages to replace manual work. You can also set the time to send the specified content, so you don’t have to sit in front of the computer and send bullet chatting when the time comes.
.For
another example, there are always some people who come in to advertise in your fan group, and they refuse to change after repeated admonitions. Then you can use this automatic message sending program to bombard their group. By the time he notices, you have sent thousands of messages. The news, withdrawing the news can make him withdraw to collapse...

insert image description here

How long to send a message, or how many messages to send per second, can be freely set. If the time is set short, it is no problem to send dozens of messages per second, but too fast will cause the effect of swiping the screen...

Today I will share this technique with you. It is very simple and does not require much code.


Two, the effect

Let's take a look at the effect first. What I set here is to start sending after a 2s delay, and send it every 0.5s.

insert image description here


3. Development environment

  • System: Windows 10 64-bit
  • Python version: 3.9.6
  • Pycharm version: 2023
  • Modules (libraries): os, time, pyautogui, pyperclip

4. Analysis of key steps

There are two main code files to implement. The purpose is: to obtain the location of the chat window and realize the function of automatically sending messages . The library used has been mentioned above. Before starting to write code, first download the library to be used by pip Install it, and I won't talk about this anymore.

1. Get the chat window position (source code 1)

Before we send a message, we need to know where the chat window is, that is, where the mouse stays to locate the input interface of the chat window, that is, what are the x and y coordinates of the mouse.

Here I use the three libraries of os, time and pyautogui to get the real-time position of the mouse:

try:
    while True:
        print("Press Ctrl-C to end")
        x, y = pag.position()  # 返回鼠标的坐标
        posStr = "Position:" + str(x).rjust(4) + ',' + str(y).rjust(4)
        print(posStr)  # 打印坐标
        time.sleep(0.2)
        os.system('cls')  # 清楚屏幕
except KeyboardInterrupt:
    print('end....')

As long as the program is running, whenever we move the mouse, the x and y values ​​of the mouse will automatically change and print out. We only need to bring up the chat window and position the mouse to the input position of the chat window to get it. The x and y values ​​at this time, with the x and y values, we can tell the following messaging program where to paste and push.

insert image description here
Of course, there are many ways to get the mouse position, and you can also try other ways to get it.

2. Realize the function of sending messages automatically

After obtaining the values ​​of x and y, what we need to do is of course write a program to realize "copy text→paste text→send message". Here we need to use pyautogui to control the keyboard and mouse, and use pyperclip to control the computer to copy and Paste, and use the time library to control the time.

First of all, we prepare the content to be sent in advance, put it in the content, and then use it directly. The content can be customized and modified, such as this:

content = """   
呼叫龙叔!
第二遍!
第三遍!
第四遍!
第五遍!
"""

We need to switch to the chat interface after running the code, and we need time to manually do this operation in the middle, so before copying and pasting and sending the code, we need to set aside some time for ourselves. I set a time delay of 4s here first. Of course, you can also set it to start sending messages after a few hours.

time.sleep(4)

Next is how to implement copy, paste and send:

for line in list(content.split("\n"))*10:
    if line:
        pyautogui.click(669,687)  #鼠标点击并定位到聊天窗口
        pyperclip.copy(line)    #复制该行
        pyautogui.hotkey("ctrl","v") #粘贴,mac电脑则把ctrl换成command
        pyautogui.typewrite("\n")   #发送
        time.sleep(5) #每次发完间隔5s

At this point, everything has been completed. If you feel that sending a message in 5s is too fast, you can modify the value of 5 in time.sleep(5), for example, send a message in 10s; if you set it to 0.01 seconds, then It will be a screen swiping effect of sending a message quickly...

"*10" in the for loop controls the number of loops, that is, let it send the text 10 times, or you can set it not to loop, and change list(content.split(“\n”))*10 to content. split("\n") will do.

The general method is the above. If you need the source code, you can chat with me privately. You can also try other ways to realize it. To put it bluntly, it is to automatically send messages. There are many ways to realize it. Wait, and the button wizard can also realize this function, more exciting, waiting for you to dig by yourself.


V. Summary

The essence of this script is to realize the computer to automatically send messages, but the setting of the interval time makes it also have the function of sending messages quickly, not only QQ, but also WeChat.

The basic principle is like this. You can also think about how to start the program after a few hours on this basis, and send it every few minutes to completely liberate yourself.

Thank you for your reading and liking. I have collected a lot of technical dry goods, which can be shared with friends who like my articles. If you are willing to take the time to settle down and learn, they will definitely help you. The dry goods include:

insert image description here

Click on the business card at the end of the article to take it away
insert image description here

Guess you like

Origin blog.csdn.net/zhiguigu/article/details/130395313