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 的定時器設置每天也行

  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/感悟.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

 

然后 配置定時器  的時間一定要比 程序的時間早 不然要隔一天執行

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM