91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot如何搭建go-cqhttp機器人

發布時間:2021-12-23 10:52:49 來源:億速云 閱讀:469 作者:iii 欄目:開發技術

這篇文章主要介紹“SpringBoot如何搭建go-cqhttp機器人”,在日常操作中,相信很多人在SpringBoot如何搭建go-cqhttp機器人問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SpringBoot如何搭建go-cqhttp機器人”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

    一、搭建go-cqhttp機器人

    測試

    給自己好友發送一條私聊消息(user_id:好友的QQ號)

    # cmd
    crul '127.0.0.1:5700/send_private_msg?user_id=xxxxxx&message=你好~'
    
    #postMan
    GET http://127.0.0.1:5700/send_private_msg?user_id=xxxxx&message=你好~

    響應

    SpringBoot如何搭建go-cqhttp機器人

    二、搭建SpringBoot環境

    基本環境

    SpringBoot如何搭建go-cqhttp機器人

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>
        
        <!--httpUtils-->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>
        
        <!--websocket作為客戶端-->
        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.3.5</version>
        </dependency>
    
    </dependencies>

    1、HTTP通信

    修改go-cqhhtp 配置文件 config.yml

    post:
      # 這里一定要填成這樣的http://{host}:{ip}
      - url: 'http://127.0.0.1:8400'
       secret: ''

    SpringBoot如何搭建go-cqhttp機器人

    Java 代碼

    測試案例:https://docs.go-cqhttp.org/api/#%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF 發送私聊消息

    QqRobotController.java

    @RestController
    @Slf4j
    public class QqRobotController {
    
        @Resource
        private QqRobotService robotService;
    
        @PostMapping
        public void QqRobotEven(HttpServletRequest request){
            robotService.QqRobotEvenHandle(request);
        }
    }

    QqRobotService.java

    public interface QqRobotService {
        void QqRobotEvenHandle(HttpServletRequest request);
    }

    QqRobotServiceImpl.java

    @Service
    @Slf4j
    public class QqRobotServiceImpl implements QqRobotService {
    
        @Override
        public void QqRobotEvenHandle(HttpServletRequest request) {
            //JSONObject
            JSONObject jsonParam = this.getJSONParam(request);
            log.info("接收參數為:{}",jsonParam.toString() !=null ? "SUCCESS" : "FALSE");
            if("message".equals(jsonParam.getString("post_type"))){
                String message = jsonParam.getString("message");
                if("你好".equals(message)){
                    // user_id 為QQ好友QQ號
                    String url = "http://127.0.0.1:5700/send_private_msg?user_id=xxxxx&message=你好~";
                    String result = HttpRequestUtil.doGet(url);
                    log.info("發送成功:==>{}",result);
                }
            }
        }
    
        public JSONObject getJSONParam(HttpServletRequest request){
            JSONObject jsonParam = null;
            try {
                // 獲取輸入流
                BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
    
                // 數據寫入Stringbuilder
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = streamReader.readLine()) != null) {
                    sb.append(line);
                }
                jsonParam = JSONObject.parseObject(sb.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return jsonParam;
        }
    
    }

    HttpUtils 工具類

    public class HttpRequestUtil {
        /**
         * @Description: 發送get請求
         */
        public static String doGet(String url) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-type", "application/json");
            httpGet.setHeader("DataEncoding", "UTF-8");
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
            httpGet.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(httpGet);
                HttpEntity entity = httpResponse.getEntity();
                if(httpResponse.getStatusLine().getStatusCode() != 200){
                    return null;
                }
                return EntityUtils.toString(entity);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (httpResponse != null) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (null != httpClient) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    
        /**
         * @Description: 發送http post請求
         */
        public static String doPost(String url, String jsonStr) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setHeader("DataEncoding", "UTF-8");
            CloseableHttpResponse httpResponse = null;
            try {
                httpPost.setEntity(new StringEntity(jsonStr));
                httpResponse = httpClient.execute(httpPost);
                if(httpResponse.getStatusLine().getStatusCode() != 200){
                    return null;
                }
                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                return result;
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (httpResponse != null) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != httpClient) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    }

    響應:

    發送成功:==>{"data":{"message_id":2113266863},"retcode":0,"status":"ok"}

    SpringBoot如何搭建go-cqhttp機器人

    2、WebScoket 通信

    一般WebScoket的客戶端都是H5, 但是為了測試本篇博客使用Java作為客戶端

    修改go-cqhhtp 配置文件 config.yml

      - ws:
          # 正向WS服務器監聽地址
          host: 127.0.0.1
          # 正向WS服務器監聽端口
          port: 5701

    SpringBoot如何搭建go-cqhttp機器人

    Java 代碼

    需要導入pom包

    SpringBoot如何搭建go-cqhttp機器人

    WebsocketClient.java

    @Slf4j
    @Component
    public class WebSocketConfig {
      
        @Bean
        public WebSocketClient webSocketClient() {
            try {
                WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://127.0.0.1:5701"),new Draft_6455()) {
                    @Override
                    public void onOpen(ServerHandshake handshakedata) {
                        log.info("[websocket] 連接成功");
                    }
      
                    @Override
                    public void onMessage(String message) {
                        log.info("[websocket] 收到消息={}",message);
      
                    }
      
                    @Override
                    public void onClose(int code, String reason, boolean remote) {
                        log.info("[websocket] 退出連接");
                    }
      
                    @Override
                    public void onError(Exception ex) {
                        log.info("[websocket] 連接錯誤={}",ex.getMessage());
                    }
                };
                webSocketClient.connect();
                return webSocketClient;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
      
    }

    測試

    SpringBoot如何搭建go-cqhttp機器人

    [websocket] 收到消息={"interval":5000,"meta_event_type":"heartbeat","post_type":"meta_event","self_id":2878522414,"status":{"app_enabled":true,"app_good":true,"app_initialized":true,"good":true,"online":true,"plugins_good":null,"stat":{"packet_received":29,"packet_sent":21,"packet_lost":0,"message_received":0,"message_sent":0,"disconnect_times":0,"lost_times":0,"last_message_time":0}},"time":1639797397}

    到此,關于“SpringBoot如何搭建go-cqhttp機器人”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    兴文县| 仁寿县| 普安县| 长岭县| 阜平县| 肥乡县| 英山县| 辉县市| 娄烦县| 吴堡县| 汉川市| 肇东市| 余干县| 宁强县| 象山县| 隆安县| 藁城市| 新田县| 紫金县| 日喀则市| 汝阳县| 久治县| 岳西县| 台江县| 聂拉木县| 石嘴山市| 庄浪县| 蒲城县| 青海省| 西乌| 绵竹市| 泽库县| 五家渠市| 宜良县| 双鸭山市| 年辖:市辖区| 井研县| 安义县| 当涂县| 西青区| 高安市|