Sometimes let me free my hands , Let the computer help us automatically send some messages we want to send , It's very labor-saving , For example, I wrote a speech during the day , Give a written speech in the group in the evening , Then we can use the footbook to realize automatic replication , Function of pasting and sending text , To liberate ourselves , You don't have to do this on the computer again and again Ctrl
C/Ctrl V This is a tiring job .
You can also set the timing how long to send the specified content , Now you don't have to sit in front of the computer. It's time to launch a barrage .
How often 1 Message , Or 1 How many messages per second , Can be set freely , If the time is set short , It's OK to send dozens of messages a second , It's just too fast, it will have the effect of brushing the screen …
Let's share this skill today , It's simple , Not much code .
<> one , effect
Let's look at the effect first , What I set up here is 4s Start sending after , interval 0.5s Send once .
use Python Realize the computer to send messages automatically , Content customization , Fast or slow
<> two , development environment
* system ：Windows10 64 position
* Python edition ：3.9
* Pycharm edition ：2021.1.3
* modular （ library ）：os,time,pyautogui,pyperclip
<> three , Analysis of key steps
There are two main implementation code files , The purposes are ： Get the chat window location and realize the function of automatically sending messages
, The library used has been mentioned above , Before you start writing code , Put the library you want to use first pip Download and install , I won't say this again .
<>1. Get chat window location （ Source code 1）
Before we send a message , You need to know where the chat window is , That is, where can the mouse stay to locate the input interface of the chat window , That is, the of the mouse x and y What are the coordinates .
Here I use os,time and pyautogui These three libraries , Get the real-time position of the mouse ：
try: while True: print("Press Ctrl-C to end") x, y = pag.position() # Returns the coordinates of the mouse
posStr= "Position:" + str(x).rjust(4) + ',' + str(y).rjust(4) print(posStr) #
Print coordinates time.sleep(0.2) os.system('cls') # Clear screen except KeyboardInterrupt: print(
As long as the program runs , Whenever we move the mouse , Mouse x and y The value will automatically sound, change and print out , We just need to pull out the chat window , By positioning the mouse to the input position of the chat window, you can get the information at this time x and y value , With this x and y After value , We can tell the following message sending program where to paste and push .
Yes, of course , There are many ways to get the mouse position , You can also try other ways to get it .
<>2. Realize the function of automatically sending messages
After getting x and y After the value of , Of course, what we have to do is write a program to implement it “ Copy text → Paste text → send message ”, You need it here pyautogui To control the keyboard and mouse , use
pyperclip To control the computer to copy and paste , And use time This library controls the time .
First, we prepare the content to be sent in advance , Put on content inside , Just use it then , The content can be customized and modified , Like this ：
content = """ Call uncle long ! Second time ! Third time ! Fourth time ! Fifth time ! """
After running the code, we need to switch to the chat interface , It takes time to do this manually , So before copying, pasting and sending the code , We need to set aside some time for ourselves , I set it here first 4s Time delay , Of course, it can also be set to start sending messages after a few hours .
The next step is how to copy, paste and send ：
for line in list(content.split("\n"))*10: if line: pyautogui.click(669,687)
# Click and navigate to the chat window pyperclip.copy(line) # Copy this row pyautogui.hotkey("ctrl","v")
# paste ,mac The computer puts ctrl change into command pyautogui.typewrite("\n") # send out time.sleep(5) # Interval after each delivery 5s
Here we are , Everything is finished , If you think 5s send out 1 This message is too fast , Can be modified time.sleep(5) Inside 5 This value , for instance 10s Send a message ; If you set it to 0.01 second , Then it will be a screen swiping effect of sending messages quickly …
for In loop “*10” Number of control cycles , That is, let it go 10 Look at the text , You can also set it not to cycle , hold list(content.split("\n"))*10 Change to
content.split("\n") that will do .
The general method is the above , If you need the source code, you can talk to me privately , You can also try other ways , To put it bluntly, it's automatic messaging , There are many ways to implement it , For example, take the more advanced one directly xookie transfer api Send, etc , And the button wizard can also realize this function , More wonderful , When you dig it yourself .
The essence of this script is to realize the computer to send messages automatically , Just because of the interval setting, it also has the function of sending messages quickly , not only QQ, Wechat can also be used .
That's the basic principle , You can also think about it , How on this basis , Let the program start in a few hours , Send it every tens of minutes , Completely liberate yourself .