Java实现微信每天定时发送消息,指定好友 -- 柚子真好吃
一、实现原理二、代码三、效果展示2.0版本已发布,详见 点击跳转
2.0版本已发布,详见 点击跳转
2.0版本已发布,详见 点击跳转
2.0版本已发布,详见 点击跳转
一、实现原理
需要登陆电脑版微信。使用快捷键Ctrl+Alt+W 呼出微信窗。(快捷键可自定义)根据名称搜索好友。打开对话框粘贴文字。发送。二、代码
WeChatApplication.javapublic class WeChatApplication {public static void main(String[] args) throws ParseException {System.out.println("Start Success !");Execute execute = new Execute();execute.everyday("Ryan","08:35:30","ZY:早间打卡");execute.everyday("Ryan","18:25:30","ZY:晚间打卡");}}
Execute.java
public class Execute {private WeChatRobot robot = new WeChatRobot();private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");public void everyday(String friendName,String timeStr,String message) throws ParseException {//获取目标时间Date date = getDate(timeStr);Timer timer = new Timer();TimerTask timerTask = new TimerTask() {@Overridepublic void run() {printLog(friendName,message);robot.OpenWeChat();robot.ChooseFriends(friendName);robot.SendMessage(message);}};timer.schedule(timerTask,date,1000*60*60*24);}private void printLog(String friendName, String message) {System.out.println("-----------------发送消息-----------------");System.out.println("当前时间: " + sdf.format(new Date()));System.out.println("发送对象: " + friendName);System.out.println("发送内容: " + message);}private Date getDate(String timeStr) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//获取当前日期 例如 -22-22String currentDate = sdf.format(new Date());//组拼 目标时间 -22-22 22:22:22String 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 += 1000*60*60*24;}//返回目标日期return new Date(targetTimer);}}
WeChatRobot.java
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);}}
三、效果展示
逻辑参考:连接