Java开发QQ机器人
生活随笔
收集整理的這篇文章主要介紹了
Java开发QQ机器人
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很早之前就想過開發一個qq機器人,不過之前一直在想怎么抓包,模擬,最近我朋友給我說了個github的開源項目,再次開工
推薦關閉QQ的設備鎖,否則模擬QQ登錄可能需要驗證,雖然官方有解決方案,不過開發階段還是太麻煩
項目地址
https://github.com/mamoe/mirai
依賴
https://docs.mirai.mamoe.net/ConfiguringProjects.html
這是我開發使用的版本
核心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調用次數就干完了
總結
以上是生活随笔為你收集整理的Java开发QQ机器人的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1252 :[蓝桥杯2015初赛]奇妙的
- 下一篇: java qq机器人_简单几步教你如何用