天天看點

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

看完本篇部落格,你将能做到

  • qq消息秒速99+
  • 搭建雲環境(或者虛拟機),部署qq機器人
  • 使用springboot為你的人工智障寫一個簡單的hello world

靈感來源

  • 偉哥的部落格
  • 基于酷Q的一個插件coolq-http-api

本地部署

步驟一:下載下傳酷Q和插件coolq-http-api

  • 酷Q
    【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署
  • coolq-http-api
    【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

步驟二:解壓酷Q添加coolq-http-api

解壓後的目錄

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

把插件複制到app裡

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

步驟三:打開酷Q,熟悉一下基本操作

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

步驟四:啟動插件,實作秒速999+

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

然後照着api寫一個url,循環通路,想要更猛烈就多開幾個線程

public static void main(String[] args) throws IOException {
        String senMsg = "http://127.0.0.1:5700/send_private_msg";
        sendGet(senMsg, "user_id=此處填寫對方的qq号&message=此處填寫要發送的内容", 20);//剩下這個填轟炸次數
    }

    public static String sendGet(String url, String param, int n) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接配接

            for (int i = 0; i < n; i++) {
                URLConnection connection = realUrl.openConnection();
                // 設定通用的請求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                // 建立實際的連接配接
                connection.connect();
                // 擷取所有響應頭字段
                Map<String, List<String>> map = connection.getHeaderFields();
                // 周遊所有的響應頭字段
                for (String key : map.keySet()) {
                    System.out.println(key + "--->" + map.get(key));
                }
                // 定義 BufferedReader輸入流來讀取URL的響應
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            }
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
           

雲端部署

步驟一:安裝docker插件,有興趣可以研究更多的API

跟着這個教程來CoolQ HTTP API 插件

步驟二:登入ip+9000端口

是一個wine界面

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

注意: 修改成自己項目的url

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署

步驟三:寫springboot項目

public class HelloWorldController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(String message){
        sendGet("http://xx.xx.xx.xx:5700/send_private_msg","user_id=xxxxx&message=HelloWorld", 1);

        return "success";
    }
    public static String sendGet(String url, String param, int n) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接配接

            for (int i = 0; i < n; i++) {
                URLConnection connection = realUrl.openConnection();
                // 設定通用的請求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                // 建立實際的連接配接
                connection.connect();
                System.out.println("已發送");
                // 擷取所有響應頭字段
                Map<String, List<String>> map = connection.getHeaderFields();
                // 周遊所有的響應頭字段
                for (String key : map.keySet()) {
                    System.out.println(key + "--->" + map.get(key));
                }
                // 定義 BufferedReader輸入流來讀取URL的響應
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            }
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}
           

步驟四:将該項目部署到tomcat上

步驟五:和人工智障聊兩句

【入門】QQ聊天機器人--HelloWorld篇看完本篇部落格,你将能做到靈感來源本地部署雲端部署