java中给多个微信好友自动发信息

package weixin;

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.io.IOException;

import javax.imageio.ImageIO;
public class T6 {
    private static Toolkit kit;
    // 创建Robot对象
    private static Robot robot= null;
    public static void main(String[] args) throws InterruptedException, IOException {
        // 好友昵称
        String friendNickName = "文件传输助手,红尘沙漏";//多个好友昵称
        
      for(String name:friendNickName.split(",")) {
    	  searchMyFriendAndSend(name);
    	  
    	
      }
        
    }

    private static void searchMyFriendAndSend(String friendNickName) throws InterruptedException, IOException {
       
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        //打开微信 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);
        // 发送消息
        sendMsg();
        //打开微信 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);
    }
    private static void sendMsg() throws InterruptedException, IOException {
        String[] mottoes = {
               // "我只爱你四天,春天夏天秋天冬天!",
               // "我只爱你三天,昨天,今天,明天!",
               // "我只爱你两天,白天,黑天!",
               // "我只爱你一天,每一天!",
               // "[玫瑰]爱你么么哒!",
                "[呲牙][坏笑]",
                "[奸笑][爱心]"
        };
        for (String motto : mottoes) {
            sendOneMsg(motto);
        }
        Thread.sleep(100);
        sendOneMsg("[得意]就问你,腻不腻害!");
        sendImgMessage("\\img\\66.png");//发送图片
        
        
    }

    private static void sendOneMsg(String msg) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText;
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        //延迟十秒,主要是为了预留出打开窗口的时间,括号内的单位为毫秒
        assert robot != null;
        robot.delay(500);
        tText = new StringSelection(msg);
        clip.setContents(tText, null);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(500);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(500);
        robot.keyRelease(KeyEvent.VK_CONTROL);
       
    }
    public static void sendImgMessage(String imgPath) throws IOException {
    	System.out.println("imgPath===="+imgPath);
    	Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        if (robot == null) {
            return;
        }
        // 将字符串放到剪切板内,相当于做了一次复制操作
        Image image = ImageIO.read(ClassLoader.getSystemResource(imgPath));
        //Image image =GameUtil.getImage(imgPath);
        Transferable tImg = new ImageSelection(image);
        clip.setContents(tImg, null);
        // 以下两行按下了ctrl+v,完成粘贴功能
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);

        robot.keyRelease(KeyEvent.VK_CONTROL);// 释放ctrl按键,像ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放,不然会出问题。crtl如果按住没有释放,在按其他字母按键是,敲出来的回事ctrl的快捷键。
        robot.keyPress(KeyEvent.VK_ENTER); // 按下enter键进行消息发送
        robot.delay(1000);
    }
    
    
}
package weixin;

import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

/**
 * 自定义Transferable实现类实现图片复制到剪切板
 * 
 * @date 2020/05/14
 */
public class ImageSelection implements Transferable {
    private Image image;

    public ImageSelection(Image image) {
        this.image = image;
    }

    // Returns supported flavors
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] {DataFlavor.imageFlavor};
    }

    // Returns true if flavor is supported
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return DataFlavor.imageFlavor.equals(flavor);
    }

    // Returns image
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
        if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return image;
    }
}

  

 

//pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cn</groupId>
  <artifactId>weixinhaoyou</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
      <dependency>
			<groupId>net.java.dev.jna</groupId>
			<artifactId>jna</artifactId>
			<version>5.5.0</version>
		</dependency>
		<dependency>
			<groupId>net.java.dev.jna</groupId>
			<artifactId>jna-platform</artifactId>
			<version>5.5.0</version>
		</dependency>
  </dependencies>
</project>

  

 

  

 

posted @ 2020-10-29 14:03  红尘沙漏  阅读(202)  评论(0编辑  收藏  举报