0
点赞
收藏
分享

微信扫一扫

Java开发QQ机器人

天蓝Sea 2022-04-05 阅读 91
java

很早之前就想过开发一个qq机器人,不过之前一直在想怎么抓包,模拟,最近我朋友给我说了个github的开源项目,再次开工

项目地址

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可不填都有默认值
    configuration.setWorkingDir(new File("E:\\IDEA\\MyTest6\\src\\main\\resources"));
    //        configuration.setCacheDir();// 设置 工作与缓存目录
    configuration.fileBasedDeviceInfo("qq.json");//设置设备信息 防止重复验证设备信息
    // 日志相关  可以自己处理 或关闭日志
    configuration.noBotLog();// qq操作相关日志
    configuration.noNetworkLog();// 网络相关日志
    Bot bot = BotFactory.INSTANCE.newBot(qq, "password",configuration);
    bot.login();//登录
    JavaMain.afterLogin(bot);
}
public static void afterLogin(Bot bot) {
    long yourQQNumber = qq;
    // subscribe 自定义监听结束时间    subscribeAlways一直监听   subscribeOnce只处理一次
    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") // 会被构造成 PlainText 再添加, 相当于上一行
        .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调用次数就干完了

举报

相关推荐

0 条评论