【入門】QQ聊天機器人--HelloWorld篇

看完本篇博客,你將能做到

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

靈感來源

本地部署

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

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

解壓後的目錄
在這裏插入圖片描述
把插件複製到app裏
在這裏插入圖片描述

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

在這裏插入圖片描述

步驟四:啓動插件,實現秒速999+

在這裏插入圖片描述
然後照着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界面
在這裏插入圖片描述
注意: 修改成自己項目的url
在這裏插入圖片描述

步驟三:寫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上

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

[圖片]

發佈了16 篇原創文章 · 獲贊 10 · 訪問量 2466
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章