起因 :加入一个新的公司 每周都要发感悟,这种死板重复的工作,作为一个程序员怎么能忍呀! 肯定程序定时发呀 !
准备工作 : 微信pc版, 2 开发环境(jdk ,eclipse/intelliJ IDEA 等)
直接上程序
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 定时器
1 package com.freemarker.reverse.weixinAuto; 2 3 import org.apache.commons.io.FileUtils; 4 import org.apache.commons.lang.StringUtils; 5 6 import java.io.File; 7 import java.io.IOException; 8 import java.text.ParseException; 9 import java.text.SimpleDateFormat; 10 import java.util.*; 11 12 /** 13 * @Author libin 14 * @create 2021/10/25 11:26 15 */ 16 public class WeChartTimer { 17 private WeChatRobot robot = new WeChatRobot(); 18 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 19 20 private static int oneDayTime= 1000 * 60 * 60 * 24; 21 22 public void execute(String friendName, String timeStr) throws Exception { 23 24 //获取执行时间 25 Date date = getDate(timeStr); 26 Timer timer = new Timer(); 27 TimerTask timerTask = new TimerTask() { 28 29 @Override 30 public void run() { 31 try { 32 Runtime rt = Runtime.getRuntime(); 33 File myfie = new File(WeChatApplication.weixinPath); 34 rt.exec(myfie.getAbsolutePath()); 35 String msg = getMsg(); 36 if (StringUtils.isNotEmpty(msg)) { 37 writeLog(friendName, msg); 38 robot.OpenWeChat(); 39 robot.ChooseFriends(friendName); 40 robot.SendMessage(StringUtils.trimToEmpty(msg)); 41 } 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 } 46 }; 47 timer.schedule(timerTask, date, oneDayTime); 48 49 50 } 51 52 private static String getMsg() { 53 List<String> content = new ArrayList<>(); 54 try { 55 File f = new File(WeChatApplication.contentPath); 56 content = FileUtils.readLines(f, "utf-8"); 57 int index = content.size(); 58 Random r = new Random(); 59 int recordIndex = r.nextInt(index); 60 return content.get(recordIndex); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 } 64 return ""; 65 } 66 67 68 private void writeLog(String friendName, String message) { 69 try { 70 File log = new File("C:\\software\\autoWX\\wxMsgLog.log"); 71 StringBuffer sb = new StringBuffer(); 72 sb.append("\n------------begin-------------"); 73 sb.append("\n当前时间: " + sdf.format(new Date())); 74 sb.append("\n发送对象: " + friendName); 75 sb.append("\n发送内容: " + message); 76 sb.append("\n------------end-------------"); 77 FileUtils.write(log, sb.toString(), "utf-8", true); 78 } catch (IOException e) { 79 System.out.println("获取log失败:" + e); 80 } 81 } 82 83 /** 84 * 获取执行时间 <code>如果执行时间小于当天时间加一天</code> 85 * 86 * @param timeStr 87 * @return 88 * @throws ParseException 89 */ 90 private Date getDate(String timeStr) throws ParseException { 91 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 92 String currentDate = sdf.format(new Date()); 93 String targetTime = currentDate + " " + timeStr; 94 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 95 long targetTimer = sdf.parse(targetTime).getTime(); 96 long currentTimer = new Date().getTime(); 97 if (targetTimer < currentTimer) { 98 targetTimer += oneDayTime; 99 } 100 return new Date(targetTimer); 101 } 102 103 }View Code
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/感悟.txtView 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+r 调出命令框 输入 compmgmt.msc
然后 配置定时器