Java开发QQ机器人

很早之前就想过开发一个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可不填都有默认值
    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调用次数就干完了

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值