1、前言
正常人每天平均耗水量为2000-2500毫升,体内物质氧化可生水300毫升,故每日应补充水分2200毫升,包括饮食中的含水量。夏天每日补充水分在3000毫升左右,才能满足人体需要。 如果有个机器人能按时提醒我们喝水,那该多好啊~~
2、创建一个springboot项目
(这个步骤是为小白提供,大佬们直接跳到第三步)
2.1. 新建项目 2.2. 选择springboot项目 2.3. 创建完成的项目结构如下
3、引入simple-robot机器人依赖
3.1. 在pom.xml文件引入simple-robot依赖
<properties>
<java.version>1.8</java.version>
<simbot.version>2.0.3</simbot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>love.forte.simple-robot</groupId>
<artifactId>parent</artifactId>
<version>${simbot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>love.forte.simple-robot</groupId>
<artifactId>component-mirai-spring-boot-starter</artifactId>
</dependency>
3.2. 配置application.yml文件
simbot:
core:
bots: 6013505:yinfeng
component:
mirai:
heartbeat-period-millis: 30000
heartbeat-timeout-millis: 5000
first-reconnect-delay-millis: 5000
reconnect-period-millis: 5000
reconnection-retry-times: 2147483647
protocol: ANDROID_PHONE
no-bot-log: true
no-network-log: true
use-simbot-bot-log: true
use-simbot-network-log: true
device-info-seed: 1
cache-type: MEMORY
cache-directory:
login-solver-type: DEFAULT
dispatcher:
core-pool-size: 8
maximum-pool-size: 8
keep-alive-time: 1000
3.3 在springboot启动类加上@EnableSimbot注解
@EnableSimbot
@EnableScheduling
@SpringBootApplication
@Slf4j
public class RobotApplication {
public static void main(String[] args) {
SpringApplication.run(RobotApplication.class, args);
log.info("机器人启动成功~~~~");
}
}
3.4 simple-robot机器人官方文档
https://www.yuque.com/simpler-robot/simpler-robot-doc/gbqsz5
4、编写定时任务
4.1. 创建一个DrinkNotify.java类
@Component
@Slf4j
public class DrinkNotify {
@Resource
private BotManager botManager;
@Value("${bello.qq}")
private Set<String> qqSet;
static List<String> content;
static List<String> images;
static {
content = new ArrayList<>();
images = new ArrayList<>();
log.info("开始加载喝水语录~~~");
content.add("俗话说\"女人是水造的\",所以身为女生就要时刻喝水,这样就可以保持充足的水分,皮肤、头发就会更有光泽~");
content.add("喝多点水还可以保持身材哦,因为水促进了我们身体的循环~");
content.add("该喝水了哟,喝多点水整体上也会容光焕发~");
content.add("该喝水了哟,要多爱护自己,多喝水、多吃新鲜水果蔬菜、尽量保证充足睡眠。加油!");
content.add("多喝水很简单的话,多喝水对身体好!只有心中挂念着你们的人才会说你的家人也老说的话:你要多喝水呀!!~");
content.add("天气寒冷干燥。多喝水,注意保暖。少抽烟喝酒吃辣。多想念我~");
log.info("开始加载喝水图片~~~");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221637.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221739.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221758.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221815.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221834.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221913.jpeg");
images.add("https://gitee.com/liujian8/study-image/raw/master/image/20211224221925.jpeg");
}
@Scheduled(cron = "0 0/1 * * * ?")
public void handler() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour < 9 || hour > 20) {
return;
}
qqSet.forEach(qq -> {
try {
final String msg = content.get(new Random().nextInt(content.size()));
final String img = String.format("[CAT:image,url=%s,flash=false]", images.get(new Random().nextInt(content.size())));
botManager.getDefaultBot().getSender().SENDER.sendPrivateMsg(qq, msg);
botManager.getDefaultBot().getSender().SENDER.sendPrivateMsg(qq, img);
log.info("正在发送喝水提醒,当前qq={}, 语录={}, img={}", qq, msg, img);
} catch (Exception e) {
log.error("发送喝水提醒异常, qq={}", qq, e);
}
});
}
}
4.2. 在yml文件中配置女神们的QQ号
bello:
qq: 1332483344,52000012
5、加入智能聊天功能
5.1. 这里主要使用青云客的api进行聊天,官网
http://api.qingyunke.com/
5.2. 封装http工具类
public class HttpUtil {
public static String sendGet(String url, String param) {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try {
String urlNameString = url;
if (!StringUtils.isEmpty(param)) {
urlNameString += "?" + param;
}
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");
connection.connect();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result.toString();
}
}
5.3. 创建消息监听类,支持私聊消息和群消息的智能聊天
@Component
@Slf4j
public class MessageListener {
static final String URL = "http://api.qingyunke.com/api.php";
@OnPrivate
public void privateMsg(PrivateMsg privateMsg, MsgSender sender) {
sendMsg(privateMsg, sender);
}
@OnGroup
public void groupMsg(GroupMsg groupMsg, MsgSender sender) {
sendMsg(groupMsg, sender);
}
private void sendMsg(MessageGet commonMsg, MsgSender sender) {
log.info("智能聊天中~~~,接收消息:qq={}, msg={}", commonMsg.getAccountInfo().getAccountCode(),
commonMsg.getMsgContent().getMsg());
final String result = HttpUtil.sendGet(URL,
"key=free&appid=0&msg=".concat(commonMsg.getMsgContent().getMsg()));
if (!StringUtils.isEmpty(result)) {
final JSONObject json = JSONObject.parseObject(result);
if (json.getInteger("result") == 0 && !StringUtils.isEmpty(json.getString("content"))) {
final String msg = json.getString("content").replace("{br}", "\n");
sender.SENDER.sendPrivateMsg(commonMsg, msg);
log.info("智能聊天中~~~,发送消息:qq={}, msg={}", commonMsg.getAccountInfo().getAccountCode(), msg);
}
}
}
}
6、测试一下
6.1. 启动项目 6.2. 喝水提醒测试
可以看到两个qq都收到了提醒消息 ,后台日志也是没问题的6.3. 智能聊天测试
可以看到聊天功能也是正常的 ,后台日志也是正常的
7、源码地址
https://gitee.com/liujian8/java-robot
肝文不易,老铁们三连一波支持下吧,谢谢大家了~
|