很早之前就想过开发一个qq机器人,不过之前一直在想怎么抓包,模拟,最近我朋友给我说了个github的开源项目,再次开工
推荐关闭QQ的设备锁,否则模拟QQ登录可能需要验证,虽然官方有解决方案,不过开发阶段还是太麻烦
项目地址
https://github.com/mamoe/mirai
依赖
https://docs.mirai.mamoe.net/ConfiguringProjects.html 这是我开发使用的版本
<dependency>
<groupId>net.mamoe</groupId>
<artifactId>mirai-core-jvm</artifactId>
<version>2.9.1</version>
</dependency>
核心API文档
https://docs.mirai.mamoe.net/CoreAPI.html
简单使用(几乎包含了常用操作)
public static void main(String[] args) {
BotConfiguration configuration=new BotConfiguration();
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
configuration.setWorkingDir(new File("E:\\IDEA\\MyTest6\\src\\main\\resources"));
configuration.fileBasedDeviceInfo("qq.json");
configuration.noBotLog();
configuration.noNetworkLog();
Bot bot = BotFactory.INSTANCE.newBot(qq, "password",configuration);
bot.login();
JavaMain.afterLogin(bot);
}
public static void afterLogin(Bot bot) {
long yourQQNumber = qq;
bot.getEventChannel().subscribeAlways(FriendMessageEvent.class, (event) -> {
if (event.getSender().getId() == yourQQNumber) {
event.getSubject().sendMessage(new MessageChainBuilder()
.append(new QuoteReply(event.getMessage()))
.append("Hi, you just said: '")
.append(event.getMessage())
.append("'")
.build()
);
}
});
ContactList<Friend> friends = bot.getFriends();
System.out.println(friends);
ContactList<Group> groups = bot.getGroups();
System.out.println(groups);
Friend friend = bot.getFriend(yourQQNumber);
Image image = ExternalResource.uploadAsImage(
new File("E:\\IDEA\\MyTest6\\src\\main\\resources\\bbs.png"), friend);
MessageChain chain = new MessageChainBuilder()
.append(new PlainText("string"))
.append("string")
.append(AtAll.INSTANCE)
.append(image)
.build();
friend.sendMessage(chain);
}
茉莉聊天机器人(可以用于辅助)
https://mly.app/profile/index.html
项目演示
https://www.bilibili.com/video/BV1694y1Z7pz/
项目地址
https://gitee.com/shaokang123/qq-cmd 注意测试需要修改对应的配置文件
总结
因为直接用的开源项目,整体没什么难度,不过注意,不要为了图省事让机器人对所有人都应答,否则很快茉莉云api调用次数就干完了
|