Java开发QQ机器人
生活随笔
收集整理的這篇文章主要介紹了
Java开发QQ机器人
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
很早之前就想過開發(fā)一個qq機器人,不過之前一直在想怎么抓包,模擬,最近我朋友給我說了個github的開源項目,再次開工
推薦關(guān)閉QQ的設(shè)備鎖,否則模擬QQ登錄可能需要驗證,雖然官方有解決方案,不過開發(fā)階段還是太麻煩
項目地址
https://github.com/mamoe/mirai
依賴
https://docs.mirai.mamoe.net/ConfiguringProjects.html
這是我開發(fā)使用的版本
核心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();// 設(shè)置 工作與緩存目錄configuration.fileBasedDeviceInfo("qq.json");//設(shè)置設(shè)備信息 防止重復(fù)驗證設(shè)備信息// 日志相關(guān) 可以自己處理 或關(guān)閉日志configuration.noBotLog();// qq操作相關(guān)日志configuration.noNetworkLog();// 網(wǎng)絡(luò)相關(guān)日志Bot bot = BotFactory.INSTANCE.newBot(qq, "password",configuration);bot.login();//登錄JavaMain.afterLogin(bot); } public static void afterLogin(Bot bot) {long yourQQNumber = qq;// subscribe 自定義監(jiān)聽結(jié)束時間 subscribeAlways一直監(jiān)聽 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);// 構(gòu)建發(fā)送的消息MessageChain chain = new MessageChainBuilder().append(new PlainText("string")).append("string") // 會被構(gòu)造成 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
注意測試需要修改對應(yīng)的配置文件
總結(jié)
因為直接用的開源項目,整體沒什么難度,不過注意,不要為了圖省事讓機器人對所有人都應(yīng)答,否則很快茉莉云api調(diào)用次數(shù)就干完了
總結(jié)
以上是生活随笔為你收集整理的Java开发QQ机器人的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1252 :[蓝桥杯2015初赛]奇妙的
- 下一篇: 动态生成鼠标指针--可以利用已有的图片生