java 微信定時發訊息給好友
阿新 • • 發佈:2021-10-29
起因 :加入一個新的公司 每週都要發感悟,這種死板重複的工作,作為一個程式設計師怎麼能忍呀! 肯定程式定時發呀 !
準備工作 : 微信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 importView Codejava.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 = newFile("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 }
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
然後 配置定時器