1.调用青云客的API进行自动聊天
2.输入关键词自动添加为好友
3.自动发送消息
非特殊情况保持一致即可!
1.SpringBoot-3.0.3
2.JDK-17
3.Mirai-3.0.0.0-beta.6
4.Simboot-3.0.0-RC.3
Simple Robot GitHub: https://github.com/simple-robotSimple
Robot 官方(文档): https://simbot.forte.love
https://gitee.com/mumangguo/qqrobot
love.forte.simbot.boot
simboot-core-spring-boot-starter
3.0.0-RC.3
love.forte.simbot.component
simbot-component-mirai-core
3.0.0.0-beta.6
org.apache.httpcomponents
httpclient
4.5.13
# 是否在bot注册完成后自动启动他们。
# 默认为true
simbot.auto-start-bots=true
# 自定义配置bot资源文件的扫描路径。
# 默认为 classpath:simbot-bots/*.bot*
simbot.bot-configuration-resources[0]=classpath:simbot-bots/*.bot*
#配置监听的QQ号,多个QQ用逗号分割
MonitorQQ=1805395628
{
"component": "simbot.mirai",
"code":QQ号,
"passwordInfo": {
"type": "text",
"text": "密码"
}
}
import love.forte.simbot.Identifies;
import love.forte.simbot.application.BotManagers;
import love.forte.simbot.bot.BotManager;
import love.forte.simbot.component.mirai.MiraiFriend;
import love.forte.simbot.component.mirai.bot.MiraiBot;
import love.forte.simbot.component.mirai.bot.MiraiBotManager;
import love.forte.simbot.component.mirai.message.MiraiSendOnlyImage;
import love.forte.simbot.resources.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.net.URL;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
@Component
public class AutoMessage {
private static final Logger log = LoggerFactory.getLogger(AutoMessage.class);
@Autowired
private BotManagers botManagers;
@Value("${MonitorQQ}")
private Set qqSet;
/**
* 定义语录
*/
static List content;
/**
* 定义图片
*/
static List images;
static {
content = new ArrayList<>();
images = new ArrayList<>();
log.info("开始加载定义语录~~~");
// 定义语录
content.add("遇见你之前,我没想过结婚,遇见你之后,结婚我没想过别人。");
content.add("你走向我,我觉得一日不见如隔三秋,你朝我笑,我又觉得三秋未见不过一日。");
content.add("如果可以和你在一起,我宁愿让天空所有的星光全部损落,因为你的眼睛,是我生命里最亮的光芒。");
content.add("我一直喜欢温暖的东西,而世界上最温暖的,无非阳光和你。");
content.add("我不要短暂的温存,只要你一世的陪伴。");
content.add("我没有特别喜欢的零食,没有特别喜欢的电影,没有特别喜欢听的歌,但我就是特别喜欢你。");
content.add("一年四季想陪你度过,世间琐事都想与你做,此生也只想同你尝尽烟火。");
content.add("我还是很喜欢你,像七月的风和八月的雨,毫无交集。");
content.add("你在我身边也好,在天边也罢,想到世界的角落有一个你,觉得整个世界也变得温柔安定了。");
content.add("我的人生理念是活十成,一成不变的是爱你,剩下九成是加倍爱你。");
log.info("开始加载表情图片~~~");
// 表情图片
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/112ab4dd77ba5919bd830a75cf8973c.png");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200623142757.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002220953.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221329.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221441.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221545.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201004212459.jpg");
images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201004212532.jpg");
}
/**
* 每一小时发送一次: 0 0 0/1 * * ?
* 每五分钟发送一次: 0 0/5 * * * ?
* 每天晚上8点: 0 0 20 * * ?
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void handler() {
Calendar calendar = Calendar.getInstance();
// 获取当前小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
// 只在早上8点到晚上22点发送消息
if (hour < 8 || hour > 22) {
return;
}
//发送QQ私信
qqSet.forEach(qq -> {
try {
final String msg = content.get(new SecureRandom().nextInt(content.size()));
//图片URL:https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201004212532.jpg
final String img = images.get(new SecureRandom().nextInt(images.size()));
// 获取所有的bot
for (BotManager> manager : botManagers) {
if (manager instanceof MiraiBotManager miraiBotManager) {
MiraiBot bot = miraiBotManager.get(Identifies.ID("2634246"));
// 拿到bot,怎么操作看你心情,比如往某个群发消息
assert bot != null;
MiraiFriend friend = bot.getFriend(Identifies.ID(qq));
assert friend != null;
friend.sendAsync(msg);
// 构建图片
MiraiSendOnlyImage offlineImage = MiraiSendOnlyImage.of(Resource.of(new URL(img)));
// 上传图片到friend,然后向其发送
// 得到发送回执的 Future
offlineImage.uploadAsync(friend).thenCompose(friend::sendAsync);
log.info("正在发送定义语录,当前qq={}, 语录={}, img={}", qq, msg, img);
break;
}
}
} catch (Exception e) {
log.error("发送定义语录异常, qq={}", qq, e);
}
});
}
}
import love.forte.simboot.annotation.Listener;
import love.forte.simbot.ID;
import love.forte.simbot.event.FriendAddRequestEvent;
import love.forte.simbot.event.FriendMessageEvent;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
/**
* 监听类
* Author:木芒果
*/
@Component
public class MessageListener {
private static final Logger log = LoggerFactory.getLogger(MessageListener.class);
/**
* 监听好友添加请求
*/
@Listener
public void onFriendAddRequest(FriendAddRequestEvent friendAddRequestEvent) {
String message = friendAddRequestEvent.getMessage();
ID id = friendAddRequestEvent.getFriend().getId();
log.info(id + ",添加我为好友");
//触发关键词即自动同意,否则拒绝
if (message.equals("木芒果")) {
log.info("同意添加" + id + "为好友");
friendAddRequestEvent.acceptAsync();
} else {
log.info("拒绝添加" + id + "为好友");
friendAddRequestEvent.rejectAsync();
}
}
/**
* 监听消息
*/
@Listener
public synchronized void onMessage(FriendMessageEvent friendMessage) {
String msg = friendMessage.getMessageContent().getPlainText();
//把空格自动转换为逗号
msg = msg.trim().replaceAll(" ", ",");
log.info(friendMessage.getFriend().getId() + "提问:" + msg);
//AI自动回复
String reply = AiOne(msg);
if (reply == null) {
reply = "宝,回复失败!重新试试把!";
}
//异步回复消息
friendMessage.replyAsync(reply);
}
public static String AiOne(String sendMsg) {
try {
HttpGet httpGet = new HttpGet("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + sendMsg);
String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42";
httpGet.addHeader("user-agent", user_agent);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
body = body.substring(body.indexOf("content") + 10, body.length() - 2);
log.info("AiOne={}", body);
return body;
} catch (Exception e) {
log.error(e.toString());
return null;
}
}
}
下载链接:https://github.com/MrXiaoM/Aoki/releases,下的慢的可以挂国外加速器,源码中也有
下载好以后,打开是这样的,使用账号和密码进行登录。
登录好以后将登录的数据打包发送到电脑上,注意:如果是通过qq发送的,别用当前登录的账号发送,会被挤下线,建议用小号做机器人的话,那么就用大号进行数据传输
发送到电脑端以后是一个压缩包,我们只要cache目录和device.json文件,将这两个文件复制粘贴到项目的根目录下,用于认证
@EnableSimbot //开启Simbot
@EnableScheduling //开启定时任务
@SpringBootApplication
public class qqRobotApplication {
private static final Logger log = LoggerFactory.getLogger(qqRobotApplication.class);
public static void main(String[] args) {
SpringApplication.run(qqRobotApplication.class, args);
log.info("木芒果机器人启动成功!");
}
}
如果还没学会可以观看官方教程:https://www.bilibili.com/video/BV1vA411o7A3,有问题留言评论,我会第一时间解答!