Learn in three minutes, send wechat to her automatically and send Spring Festival blessings

Posted by SheepWoolie on Wed, 19 Jan 2022 07:37:10 +0100

It's half a month before the Spring Festival in 2022. According to custom, we all go to pay New Year's greetings during the new year. New year's call is a traditional custom among Chinese people. It is a way for people to leave the old and welcome the new year and express good wishes to each other. In ancient times, the original meaning of the word "New Year's greetings" was to pay homage to the elderly for the new year, including kowtowing to the elderly, congratulating the elderly on the happy new year, greeting life and so on. In case of relatives and friends of the same generation, we should also give gifts and congratulations. With the development of the times, the custom of paying New Year's greetings has been adding new contents and forms. In addition to following the previous New Year's greetings, telephone New Year's greetings, SMS New Year's greetings, Internet new year's greetings and so on have sprung up. And we young people usually send holiday greetings to relatives and friends through wechat to express our feelings.

There's a lot of nonsense. What's the main course today?

Xiaohei today takes you to use Java code to realize the function of automatically sending wechat and new year's greetings. How about it? Dare you be interested? Why don't you give me a compliment first?

Ha ha, all right, let's do it.

functional analysis

First, let's analyze. How do you think it should be implemented to automatically send wechat through Java programs?

At the beginning, I thought it might be necessary to call the official API interface of wechat, pass in the message to be sent and the user ID of the person receiving the message, and add some official authentication and other operations in the process, but I made a big circle in the official, and the result was.... Not found.

Is wechat for the sake of user information security? Or does it not provide such an interface for user experience?

This method doesn't work, so we can't give up. Let's think of other ways. Don't say, it really reminds me of it.

Rest assured, this method is absolutely safe, will not involve information disclosure, will not have a third-party agent log in to your wechat, and will not transfer money on your wechat.

There is no official interface, so we simulate the whole process of sending wechat to realize an automation. Note that this function can only be used on the PC side.

First, we should open wechat;

Then, find the person to send the message;

Then, input the message to be sent;

Press enter and the message is sent.

It's such a simple set of processes. We just need to automatically simulate the process through the program.

Function realization

Here I'd like to introduce you to a class Robot. My later code content is developed based on this class.

According to the description of official documents, Robot class is mainly used to do some automatic tests, which can automatically run some mouse and keyboard operations, control other applications, etc.

Next, we will realize our function of automatically sending wechat through Robot class.

Step 1: create a Robot object

Because Robot objects are used in all subsequent operations, we first create a Robot instance.

Robot robot = new Robot();
Copy code

Step 2: open wechat chat page

In this step, you need to start the wechat application and log in successfully.

Here we will use the shortcut key of wechat. The shortcut key Ctrl+Alt+w can call out the wechat chat page

private static void openWechat(Robot robot) {
    // Simulate the key Ctrl+Alt+w through robot
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_W);
    // Release Ctrl+Alt
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_ALT);
	// Delay the robot for 1 second to prevent long page response time
    robot.delay(1000);
}
Copy code

Step 3: find friends to send messages

Similarly, use the shortcut key to quickly search for friends on the wechat chat page by using Ctrl+F.

The system clipboard will also be used here. Set the nickname of the friend to be found in the clipboard, and then paste it through Ctrl+V to achieve the purpose of entering the content.

private static void findFriend(Robot robot, String userName) {
    // Simulation key Ctrl+F
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_F);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    // Add a friend's nickname to the system clipboard first
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable tText = new StringSelection(userName);
    clip.setContents(tText, null);
    // Simulate ctrl+V to complete the paste function
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    // Delay 1 second to prevent slow query
    robot.delay(1000);
    // Press enter to locate the friend chat input box
    robot.keyPress(KeyEvent.VK_ENTER);
}
Copy code

Step 4: send message

Similarly, set the contents to the clipboard, and then Ctrl+V paste.

private static void sendMessage(String message) throws AWTException {
    // Sets the message to be sent to the clipboard
    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    Robot robot = new Robot();
    StringSelection text = new StringSelection(message);
    clip.setContents(text, null);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.delay(500);
    // Carriage return send
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.delay(500);
}
Copy code

Step 5: close the window

Use the same shortcut as opening a window Ctrl+Alt+w to close the chat window.

private static void closeWechat(Robot robot) {
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_W);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_ALT);
}
Copy code

functional testing

After the above functions are developed, we can do a simple function test to see if it works.

Directly use a main method to simulate,

/**
 * @author Xiao Hei said Java
 * @ClassName WechatApp
 * @Description
 * @date 2022/1/17
 **/
public class WechatApp {

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            WechatRobot.sendMessage("Two dogs next door", "Happy new year, two dogs!");
        }
    }
}
Copy code

sendMessage() in the WechatRobot class is a static method that contains five steps to send a message. Because the system operation is directly simulated and the clipboard is used in this process, this function does not support concurrent processing. I added the synchronized flag to this method.

@Slf4j
public class WechatRobot {

    public static synchronized void sendMessage(String user, String message) {
        try {
            Robot robot = new Robot();

            openWechat(robot);

            findFriend(robot, user);

            sendMessage(message);

            closeWechat(robot);
        } catch (AWTException e) {
            log.error("Message sending exception");
        }
    }
}
Copy code

See the effect:

Good!!!

This function is still a very small prototype. In the future, we can continue to add functions upward, such as adding the configuration of friends' nicknames, the configuration of sending messages, the timing of sending messages, etc. if you have better ideas, you can also add them upward, such as sending sweet words to your girlfriend on Tanabata, if you have an object. (dog head)

last

The technology used in this small function is not powerful. It's right to add a little atmosphere for the new year and add a little fun to the boring CRUD. If you think it's interesting, you might as well praise me and make me happy. Hee hee.

Topics: Operation & Maintenance wechat