微信设置

需要将我们电脑的微信设置为如下快捷方式,当然你也可以改其他的,不过需要该代码。

6j8RC4.png

开发

为了方便操作我就不使用申请公众号开发的方式进行操作,直接模拟键盘操作控制微信。

编写一个小小的微信机器人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;

/**
* @author: YI
* @description: 获取微信句柄操作微信
* @date: create in 2021/3/26 10:20
*/
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();
}
}

/**
* 打开微信,我这边设置了CTRL+ALT+W 快捷键打开微信
*/
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);
}

/**
* 查找微信中好友名称
*
* @param name 好友/群名称
*/
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);

}

/**
* 发送消息
*
* @param message 消息
*/
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);
}
}

编写一个发送的功能

我这里使用了两种发送方式,一种是定时延迟,一种是立即发送。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @author: YI
* @description: 发送消息
* @date: create in 2021/3/26 10:20
*/
public class StartSendMsgTask {
private final WeChatRobot robot = new WeChatRobot();
ScheduledExecutorService exe = Executors.newSingleThreadScheduledExecutor();

/**
* 立刻发送消息
*
* @param friendName 发送的朋友/群名称
* @param message 发送的内容
*/
public void sendMsgNow(String friendName, String message) {
printLog(friendName, message);
robot.openWeChat();
robot.chooseFriends(friendName);
robot.sendMessage(message);
}

/**
* 定时发送任务
*
* @param friendName 发送的朋友/群名称
* @param timeStr 定时时间
* @param message 发送的内容
*/
public void sendMsgSchedule(String friendName, String timeStr, String message) {
exe.schedule(() -> sendMsgNow(friendName, message), getDate(timeStr), TimeUnit.SECONDS);
}

/**
* 发送内容日志
*
* @param friendName 发送给谁
* @param message 发送的消息
*/
private void printLog(String friendName, String message) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("-----------------发送消息-----------------");
System.out.println("当前时间: " + sdf.format(new Date()));
System.out.println("发送对象: " + friendName);
System.out.println("发送内容: " + message);
}

/**
* 获取定时任务的时间
*
* @param timeStr 时间
* @return 任务延时发送时间(秒)
*/
private long getDate(String timeStr) {
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 = 0;
try {
targetTimer = sdf.parse(targetTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
//当前时间 时间戳
long currentTimer = System.currentTimeMillis();
//判断是否已过目标时间
if (targetTimer < currentTimer) {
//目标时间加一天
targetTimer += 1000 * 60 * 60 * 24;
}
//返回目标日期
Date date = new Date(targetTimer);

return (date.getTime() - System.currentTimeMillis()) / 1000;
}
}

最后我们就可以使用啦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @author: YI
* @description: 发送微信消息
* @date: create in 2021/3/26 10:30
*/
public class Main {
public static void main(String[] args) {
System.out.println("Start Send Message Task!");
// 测试数据,真实数据可以使用爬虫获取
String msg = "今天是2021年3月26日,星期五\n" +
"\n" +
"首先今天好也想你喔(づ ̄3 ̄)づ╭❤~,然后我就要来播送天气预报了!!\n" +
"\n" +
"今天最:高温 29.0℃,最低温 20.0℃\n" +
"\n" +
"多云,风力<3级,空气质量是良\n" +
"\n" +
"今天将在 18:39 太阳会缓缓落下,我会在家做好饭等你哟!\n" +
"\n" +
"双子座今日运势\n" +
"\n" +
"健康指数:92%\n" +
"商谈指数:88%\n" +
"幸运颜色:白色\n" +
"幸运数字:8\n" +
"\n" +
"综合运势: 今天整体财运有点背,不太适合投资或是买乐透,建议可以做个理财计划,调整一下策略。中午将会花一些时间整理自己内心的小抽屉,其实也是时候用感性的思维清理那些烦人的问题了。\n" +
"\n" +
"最后阴晴之间,谨防紫外线侵扰\n" +
"\n" +
"爱你٩(๑>◡<๑)۶傻宝宝!!!";

StartSendMsgTask startSendMsgTask = new StartSendMsgTask();
// 立刻发送
startSendMsgTask.sendMsgNow("德玛西亚群", msg);
// 定时发送
startSendMsgTask.sendMsgSchedule("德玛西亚群", "11:08:30", msg);
}
}

从代码注释可以看出我们这里可以使用立刻发送或者定时发送,其中定时发送的时间如果超过了当前时间就会自动在明天发送。

效果

6jJVYD.png

其他版本

以前写的邮件版本:https://github.com/HWYWL/love-mail