java 微信定时发消息给好友

起因 :加入一个新的公司 每周都要发感悟,这种死板重复的工作,作为一个程序员怎么能忍呀! 肯定程序定时发呀 !
准备工作 : 微信pc版, 2 开发环境(jdk ,eclipse/intelliJ IDEA 等)  如果 觉得这个不好 可以用 微信定时发消息给好友

 https://www.cnblogs.com/agnils/p/15560180.html  这个简单

直接上程序

1 主程序

 1 package com.freemarker.reverse.weixinAuto;
 2 
 3 
 4 import org.apache.commons.io.FileUtils;
 5 import org.apache.commons.lang.StringUtils;
 6 
 7 import java.io.File;
 8 import java.io.FileInputStream;
 9 import java.io.InputStreamReader;
10 import java.util.Properties;
11 
12 /**
13  * @Author agnils
14  * @create 2021/10/25 11:25
15  */
16 public class WeChatApplication {
17     //默认值
18     public static String weixinPath = "D:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe";
19     public static String contentPath = "C:\\Users\\Administrator\\Desktop\\感悟.txt";
20     public static String recipient = "";
21     public static String sendTime = "20:10:30";//HH:mm:ss
22 
23     public static void main(String[] args) throws Exception {
24         //读配置文件 获取参数 start
25         File file = new File("C:\\software\\autoWX\\config.properties");
26         Properties prop = new Properties();
27         prop.load(new InputStreamReader(new FileInputStream(file), "UTF-8"));
28 
29         recipient = prop.getProperty("recipient");
30         sendTime = prop.getProperty("sendTime");
31 
32         String tempwxPath = StringUtils.trimToEmpty(prop.getProperty("weixinPath"));
33         if (!"".equals(tempwxPath)) {
34             weixinPath = tempwxPath;
35         }
36 
37         String tempCPath = StringUtils.trimToEmpty(prop.getProperty("contentPath"));
38         if (!"".equals(tempCPath)) {
39             contentPath = tempCPath;
40         }
41         //读配置文件 获取参数 end
42 
43         System.out.println("启动程序-->s");
44         WeChartTimer timer = new WeChartTimer();
45 
46         String msgLog = "\nrecipient=" + recipient + "\nsendTime:" + sendTime;
47 
48         File log = new File("C:\\software\\autoWX\\wxLog.log");
49         FileUtils.write(log, msgLog, "utf-8", true);
50 
51         timer.execute(recipient, sendTime);
52         System.out.println("启动程序-->e");
53     }
54 }
View Code

 

2 定时器  现在文件是每天一次  也可以 程序里只执行一次 换成  timer.schedule(timerTask, date); 那么windos 的定时器设置每天也行

package com.freemarker.reverse.weixinAuto;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 
 * @create 2021/10/25 11:26
 */
public class WeChartTimer {
    private WeChatRobot robot = new WeChatRobot();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static int oneDayTime= 1000 * 60 * 60 * 24;

    public void execute(String friendName, String timeStr) throws Exception {

        //获取执行时间
        Date date = getDate(timeStr);
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                try {
                    Runtime rt = Runtime.getRuntime();
                    File myfie = new File(WeChatApplication.weixinPath);
                    rt.exec(myfie.getAbsolutePath());
                    String msg = getMsg();
                    if (StringUtils.isNotEmpty(msg)) {
                        writeLog(friendName, msg);
                        robot.openWeChat();
                        robot.chooseFriends(friendName);
                        robot.sendMessage(StringUtils.trimToEmpty(msg));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(timerTask, date, oneDayTime);


    }

    private static String getMsg() {
        List<String> content = new ArrayList<>();
        try {
            File f = new File(WeChatApplication.contentPath);
            content = FileUtils.readLines(f, "utf-8");
            int index = content.size();
            Random r = new Random();
            int recordIndex = r.nextInt(index);
            return content.get(recordIndex);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }


    private void writeLog(String friendName, String message) {
        try {
            File log = new File("C:\\software\\autoWX\\wxMsgLog.log");
            StringBuffer sb = new StringBuffer();
            sb.append("\n------------begin-------------");
            sb.append("\n当前时间: " + sdf.format(new Date()));
            sb.append("\n发送对象: " + friendName);
            sb.append("\n发送内容: " + message);
            sb.append("\n------------end-------------");
            FileUtils.write(log, sb.toString(), "utf-8", true);
        } catch (IOException e) {
            System.out.println("获取log失败:" + e);
        }
    }

    /**
     * 获取执行时间 <code>如果执行时间小于当天时间加一天</code>
     *
     * @param timeStr
     * @return
     * @throws ParseException
     */
    private Date getDate(String timeStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        String targetTime = currentDate + " " + timeStr;
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long targetTimer = sdf.parse(targetTime).getTime();
        long currentTimer = new Date().getTime();
        if (targetTimer < currentTimer) {
            targetTimer += oneDayTime;
        }
        return new Date(targetTimer);
    }

}

  

package com.freemarker.reverse.weixinAuto;

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;

/**
 * @Author agnils
 * @create 2021/10/25 11:26
 */
public class WeChatRobot {

    private Robot bot = null;
    private Clipboard clip = null;

    public WeChatRobot() {

        try {

            this.clip = Toolkit.getDefaultToolkit().getSystemClipboard();
            this.bot = new Robot();
        } catch (AWTException e) {

            e.printStackTrace();
        }
    }

    public void openWeChat() {

        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_W);

        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.keyRelease(KeyEvent.VK_ALT);

        bot.delay(1000);
    }

    public void chooseFriends(String name) {

        Transferable text = new StringSelection(name);
        clip.setContents(text, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_F);
        bot.keyRelease(KeyEvent.VK_CONTROL);

        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);

        bot.delay(2000);

        bot.keyPress(KeyEvent.VK_ENTER);

    }

    public void sendMessage(String message) {

        Transferable text = new StringSelection(message);
        clip.setContents(text, null);
        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_V);
        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.delay(1000);

        bot.keyPress(KeyEvent.VK_ENTER);

        bot.delay(1000);
        bot.keyPress(KeyEvent.VK_CONTROL);
        bot.keyPress(KeyEvent.VK_ALT);
        bot.keyPress(KeyEvent.VK_W);

        bot.keyRelease(KeyEvent.VK_CONTROL);
        bot.keyRelease(KeyEvent.VK_ALT);
    }
}

 

3 用IntelliJ IDEA 或者 Eclipse 打包可运行的jar

4配置执行参数  config.properties

a=a
recipient=前数据处理开发群
sendTime=09:35:20
weixinPath=D:/Program Files (x86)/Tencent/WeChat/WeChat.exe
contentPath=C:/Users/Administrator/Desktop/感悟.txt
View Code

 

5配置bat文件  wxAuto.bat

@echo off
if "%1" == "h" goto head
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:head
cd C:\software\autoWX
java -jar wxAuto.jar

 

 

6配置windows 定时器  程序里已经是每天发了  配置win的定时器设置成一次  如果每天开关机建议设置多次

win+r 调出命令框 输入 compmgmt.msc

 

然后 配置定时器  的时间一定要比 程序的时间早 不然要隔一天执行

 

 

 

 

posted @ 2021-10-29 10:18  agnils  阅读(615)  评论(0编辑  收藏  举报