天天看點

【Java】指定【微信】好友自動發送消息

Backgroud

原理很簡單:robot類模拟鍵盤輸入,快捷鍵打開微信,搜尋好友,把發送内容發送到粘貼闆實作。

另外一篇部落格介紹了通過python實作Python指定wx好友自動發送消息

程式源碼

package com.cloudansys.test;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;

public class T5 {

    public static void main(String[] args) throws InterruptedException {
        // 好友昵稱
//        String friendNickName = "檔案傳輸助手";
        String friendNickName = "不器";

        searchMyFriendAndSend(friendNickName);
    }

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

    private static void sendMsg() throws InterruptedException {
        String[] mottoes = {
                "我隻愛你四天,春天夏天秋天冬天!",
                "我隻愛你三天,昨天,今天,明天!",
                "我隻愛你兩天,白天,黑天!",
                "我隻愛你一天,每一天!",
                "[玫瑰]愛你麼麼哒!",
                "[呲牙][壞笑]",
                "[奸笑]"
        };
        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;
    }

}
           

效果如下圖所示

【Java】指定【微信】好友自動發送消息