热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java微信定时发消息给好友

起因:加入一个新的公司每周都要发感悟,这种死板重复的工作,作为一个程序员怎么能忍呀!肯定程序定时发呀!准备工作:微信pc版,2开发环境(jdk,eclipseintelliJIDE

起因 :加入一个新的公司 每周都要发感悟,这种死板重复的工作,作为一个程序员怎么能忍呀! 肯定程序定时发呀 !
准备工作 : 微信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 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);
}
}
/**
* 获取执行时间 如果执行时间小于当天时间加一天
*
* @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 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

 

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

 

 

 

 

一天踩一坑,

坑坑不一样,

管它浅与深,

都得填平它。

万坑之主就是我…加油!MT agnils


原文链接:https://www.cnblogs.com/agnils/p/15479162.html



推荐阅读
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 数组的排序:数组本身有Arrays类中的sort()方法,这里写几种常见的排序方法。(1)冒泡排序法publicstaticvoidmain(String[]args ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有