赞
踩
要实现给微信好友/微信群聊 定时发送消息
1,实现给微信好友发送消息的原理就是利用微信的快捷按键,Robot类就可以很好的控制按键。
Java中Robot类位于java.awt.Robot,该类用于为测试自动化,自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件,Robot类的主要目的是便于Java平台实现自动测试。
Robot可以模拟鼠标和键盘的输入,可以把他看作Java版的按键精灵。
重点提示:使用robot模拟按下像Ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放,一定要释放,一定要释放
- //释放Ctrl按键,
- robot.keyRelease(KeyEvent.VK_CONTROL);
-
- //释放Alt按键,
- robot.keyRelease(KeyEvent.VK_ALT);
下面先开始一个简单模拟操作:
可以直接在工具类中运行main方法进行手动触发操作
-
- 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.Scanner;
-
- public class WxMsgUtils {
- public static void main(String[] args) {
- try {
- Scanner in=new Scanner(System.in);
- System.out.println("请输入要发送的群名称或用户昵称:(如需退出请输入:退出)");
-
- String qunname = "";
- while (true){
- String selectNUm = in.nextLine();//要求输入整数
- System.out.println("-------------------");
- if(selectNUm.equals("退出")){
- in.close();
- return;
- }else {
-
-
- if("".equals(qunname)){
- qunname = selectNUm;
- System.out.println("消息接收方:"+selectNUm);
- System.out.println("请输入要发送的内容:(如需退出请输入:退出)");
- } else {
- System.out.println("发送内容:"+selectNUm+"(如需退出请输入:退出)");
- searchMyFriendAndSend(qunname,selectNUm);
- }
-
- }
- System.out.println("--------------------");
- }
-
-
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- public static void searchMyFriendAndSend(String friendNickName, String text) 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,text);
- }
-
- private static void sendMsg(Robot robot,String text) throws InterruptedException {
-
- sendOneMsg(robot,text);
- Thread.sleep(1000);
-
- //关闭微信 Ctrl+Alt+W
- assert robot != null;
- 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);
- }
-
- private static void sendOneMsg(Robot robot,String msg) {
- // 创建Robot对象
- 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();
- robot.delay(1000);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- return robot;
- }
- }
已经有该环境的不需要管
- <!--rabbitmq-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
-
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- spring cache -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
- </dependency>
- <!-- aop依赖 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
不修改运行的时候 会报错:java.awt.AWTException: headless environment
-
- public static void main(String[] args) {
- SpringApplicationBuilder builder = new SpringApplicationBuilder(启动类名.class);
- builder.headless(false).run(args);
- }
- import com.rabbitmq.client.Channel;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.amqp.core.ExchangeTypes;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.rabbit.annotation.*;
- import org.springframework.stereotype.Component;
- import java.io.IOException;
-
- /**
- * 消费者,用于消费队列信息
- */
- @Component
- @RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
- public class DirectConsumer {
- Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * @ClassName mq接收消息 STORE_ORDER_MSG:这是我自己编写的 你们自己换成你们的队列名就好
- * @Description
- * @author qying
- * @date 2023/4/9 12:59
- */
- @RabbitListener(bindings = @QueueBinding(
- value = @Queue(value = "STORE_ORDER_MSG",autoDelete = "false"),
- exchange = @Exchange(value = "AMQP_STORE_ORDER_MSG", type = ExchangeTypes.DIRECT)
- ))
- public void orderQueueWork(String rabbitmqMsg, Message message, Channel channel) throws IOException {
- String msg = new String(message.getBody());
- try {
- logger.info("MQ接收到数据:"+msg);
- // 为了方便演示 采用字符拼接的方式传递的数据格式如下:群名/用户昵称::要发送的内容
- String[] split = msg.split("::");
- WxMsgUtils.searchMyFriendAndSend(split[0],split[1]);
- logger.info("MQ消息处理成功"+message.getMessageProperties().getDeliveryTag(), msg);
- channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
- }catch (Exception e){
- Boolean redelivered = message.getMessageProperties().getRedelivered();
- try {
- if (redelivered) {
- channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息
- logger.error("MQ消息重新处理失败,删除消息", msg);
- }
- if (!redelivered) {
- // 消息重新放回队列
- channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
- logger.error("MQ消息处理失败放回队列", msg);
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- }
-
-
- }
- spring:
- #配置rabbitMq 服务器
- rabbitmq:
- host: 127.0.0.1
- port: 5672
- username: root
- password: 1234qwer
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。