Send messages to WeChat friends at regular intervals based on Java

1. Background knowledge

In order to send messages to WeChat friends at regular intervals, it needs to be divided into two parts:

1. Realize control screen buttons

1. The principle of sending messages to WeChat friends is to use the shortcut buttons of WeChat, and the Robot class can control the buttons very well.

The Robot class in Java is located at java.awt.Robot. This class is used to generate native system input events for test automation, self-running demo programs, and other applications that need to control the mouse and keyboard. The main purpose of the Robot class is to facilitate the implementation of the Java platform. automatic testing.

Robot can simulate mouse and keyboard input, and can be regarded as a Java version of the button wizard.

insert image description here

2. To send a message to a WeChat friend, you need to copy the message to the clipboard and then paste it into the dialog box. Here you need to use Toolkitit to copy the message to the clipboard. You can also use Toolkit to get the local system parameters of the screen. The specific code is:

//获取系统粘贴板   
//Toolkit类:Abstract Window Toolkit的所有实际实现的抽象超类。 Toolkit类的子类用于将各种组件绑定到特定的本机Toolkit实现。
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 将字符串复制到剪切板
Transferable tText = new StringSelection(msg);
clip.setContents(tText, null);

2. Realize sending messages at regular and fixed points

Sending messages at regular and fixed points can be achieved with the help of the Timer class in Java


import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
 
public class Test {
    
    
    public static void main(String[] args) {
    
    
        //timer1();
        timer2();
        //timer3();
        //timer4();
    }
 
    // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
    public static void timer1() {
    
    
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
    
    
            public void run() {
    
    
                System.out.println("-------设定要指定任务--------");
            }
        }, 2000);// 设定指定的时间time,此处为2000毫秒
    }
 
    // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
    // schedule(TimerTask task, long delay, long period)
    public static void timer2() {
    
    
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
    
    
            public void run() {
    
    
                System.out.println("-------设定要指定任务--------");
            }
        }, 1000, 1000);
    }
 
    // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
    // scheduleAtFixedRate(TimerTask task, long delay, long period)
    public static void timer3() {
    
    
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
    
    
            public void run() {
    
    
                System.out.println("-------设定要指定任务--------");
            }
        }, 1000, 2000);
    }
   
    // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
    // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
    public static void timer4() {
    
    
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
        calendar.set(Calendar.MINUTE, 0);       // 控制分
        calendar.set(Calendar.SECOND, 0);       // 控制秒
 
        Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00
 
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
    
    
            public void run() {
    
    
                System.out.println("-------设定要指定任务--------");
            }
        }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }
}

The above code provides four ways to practice timing tasks, this article uses the fourth way!

3. WeChat related shortcut keys

insert image description here

extension:

button Function
ALT+F4 Close WeChat window
Ctrl+Alt+W Open WeChat window
Ctrl+F Search for friends in WeChat
Enter Send a message

Second, the specific implementation steps

Regularly send messages to WeChat friends:

insert image description here

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Super_T5  {
    
    
    public static void main(String[] args) {
    
    
        timer4();
    }
    public static void timer4() {
    
    
        Calendar calendar = Calendar.getInstance();
/*        calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时文件传输助手
        calendar.set(Calendar.MINUTE, 0);       // 控制分
        calendar.set(Calendar.SECOND, 0);       // 控制秒*/

        Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
    
    
            public void run() {
    
    
                System.out.println("-------提醒日常签到--------");
                String friendNickName = "日常签到";
                try {
    
    
                    searchMyFriendAndSend(friendNickName);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }, time, 1000*60);// 这里设定将延时每天固定执行
    }

    private static void searchMyFriendAndSend(String friendNickName) throws InterruptedException {
    
    
        // 创建Robot对象
        Robot robot = getRobot();
        //打开微信 Ctrl+Alt+W
        assert robot != null;
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_W);
        //释放Ctrl按键,像Ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ALT);

        // 该延迟不能少,否则无法搜索
        robot.delay(1000);

        // Ctrl + F 搜索指定好友
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_F);
        robot.keyRelease(KeyEvent.VK_CONTROL);

        // 将好友昵称发送到剪切板
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(friendNickName);
        clip.setContents(tText, null);
        // 以下两行按下了ctrl+v,完成粘贴功能
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);

        // 发送消息
        sendMsg();
        
        //关闭微信对话窗口
        robot.delay(2000);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_F4);
        robot.keyRelease(KeyEvent.VK_ALT);
    }

    private static void sendMsg() throws InterruptedException {
    
    
        String[] mottoes = {
    
    
                "xdm,记得每天签到",
        };
        for (String motto : mottoes) {
    
    
            sendOneMsg(motto);
        }
        Thread.sleep(2000);
        sendOneMsg("学院要登记的哦!");
    }

    private static void sendOneMsg(String msg) {
    
    
        // 创建Robot对象
        Robot robot = getRobot();
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 将字符串复制到剪切板
        Transferable tText = new StringSelection(msg);
        clip.setContents(tText, null);
        // 以下两行按下了ctrl+v,完成粘贴功能
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        // 回车发送
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);
    }

    private static Robot getRobot(){
    
    
        // 创建Robot对象
        Robot robot = null;
        try {
    
    
            robot = new Robot();
        } catch (AWTException e) {
    
    
            e.printStackTrace();
        }
        return robot;
    }
}

The above is the complete code of the implementation, and there are comments in the code for the points that need attention! ! !

Three, application scenarios

The application scenarios can be said to be quite extensive, for example:

  • Good morning and good night to my girlfriend! ps: The premise is to have a girlfriend!

  • Wechat work that needs to be done repeatedly every day, such as signing in, punching in, etc.

  • For another example, WeChat harassed friends crazily, scolded each other on the Internet, etc.! ps: This is not recommended!

    insert image description here

Guess you like

Origin blog.csdn.net/qq_44085437/article/details/127888757