最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)

 更新時(shí)間:2021年12月23日 09:24:05   作者:OY..  
本文主要介紹了SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)

百度一下搭建go-cqhttp,千篇一律都是采用python搭建的,Java搭建根本沒(méi)有。導(dǎo)致自己在搭建的時(shí)候可折磨了,出現(xiàn)了許多的問(wèn)題。唯一能參考就只有官方文檔。文檔對(duì)小白也不是太友好,所以出這篇博客彌補(bǔ)一下Java 的搭建版本。

搭建環(huán)境: winndows 系統(tǒng) + Java + Idea 2020.2

注意:本博客寫(xiě)的比較簡(jiǎn)單,存在很多不完善的地方,如需符合自己需求請(qǐng)參考官方文檔

參考文檔:

官方文檔

一、搭建go-cqhttp機(jī)器人

請(qǐng) 參考go-cqhttp 視頻:https://www.bilibili.com/video/av247603841/

測(cè)試

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

# 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=你好~

響應(yīng)

image-20211218110027904

二、搭建SpringBoot環(huán)境

基本環(huán)境

image-20211217151109816

<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作為客戶(hù)端-->
    <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: ''

image-20211217153726655

Java 代碼

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

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("接收參數(shù)為:{}",jsonParam.toString() !=null ? "SUCCESS" : "FALSE");
        if("message".equals(jsonParam.getString("post_type"))){
            String message = jsonParam.getString("message");
            if("你好".equals(message)){
                // user_id 為QQ好友QQ號(hào)
                String url = "http://127.0.0.1:5700/send_private_msg?user_id=xxxxx&message=你好~";
                String result = HttpRequestUtil.doGet(url);
                log.info("發(fā)送成功:==>{}",result);
            }
        }
    }

    public JSONObject getJSONParam(HttpServletRequest request){
        JSONObject jsonParam = null;
        try {
            // 獲取輸入流
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

            // 數(shù)據(jù)寫(xiě)入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 工具類(lèi)

public class HttpRequestUtil {
    /**
     * @Description: 發(fā)送get請(qǐng)求
     */
    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: 發(fā)送http post請(qǐng)求
     */
    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;
    }
}

響應(yīng):

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

image-20211218110115235

2、WebScoket 通信

一般WebScoket的客戶(hù)端都是H5, 但是為了測(cè)試本篇博客使用Java作為客戶(hù)端

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

  - ws:
      # 正向WS服務(wù)器監(jiān)聽(tīng)地址
      host: 127.0.0.1
      # 正向WS服務(wù)器監(jiān)聽(tīng)端口
      port: 5701

image-20211218110927792

Java 代碼

需要導(dǎo)入pom包

image-20211218111122308

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] 連接錯(cuò)誤={}",ex.getMessage());
                }
            };
            webSocketClient.connect();
            return webSocketClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  
}

測(cè)試(具體需求實(shí)現(xiàn)就根據(jù)官方API實(shí)現(xiàn),參考本篇博客HTTP通信的邏輯)

image-20211218111918792

[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}

三、補(bǔ)充

具體詳細(xì)需求請(qǐng)參考實(shí)現(xiàn):https://docs.go-cqhttp.org/api/

到此這篇關(guān)于SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot搭建go-cqhttp機(jī)器人內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 將一個(gè)數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法

    將一個(gè)數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法

    下面小編就為大家?guī)?lái)一篇將一個(gè)數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • Java實(shí)現(xiàn)導(dǎo)入csv的示例代碼

    Java實(shí)現(xiàn)導(dǎo)入csv的示例代碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)導(dǎo)入csv的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 關(guān)于idea-web.xml版本過(guò)低怎么生成新的(web.xml報(bào)錯(cuò))問(wèn)題

    關(guān)于idea-web.xml版本過(guò)低怎么生成新的(web.xml報(bào)錯(cuò))問(wèn)題

    今天通過(guò)本文給大家分享idea-web.xml版本過(guò)低怎么生成新的(web.xml報(bào)錯(cuò))問(wèn)題,通過(guò)更換web.xml版本解決此問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • k8s解決java服務(wù)下載超時(shí)問(wèn)題小結(jié)

    k8s解決java服務(wù)下載超時(shí)問(wèn)題小結(jié)

    我們?cè)谧遡ngress的java程序的時(shí)候,往往會(huì)有導(dǎo)出數(shù)據(jù)的功能,這個(gè)時(shí)候就會(huì)有因網(wǎng)絡(luò)慢、后臺(tái)處理時(shí)間過(guò)長(zhǎng)導(dǎo)致下載超時(shí),也有因下載文件太大,導(dǎo)致下載失敗,下面給分享k8s解決java服務(wù)下載超時(shí)問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • spring中Mapstruct屬性映射的實(shí)現(xiàn)

    spring中Mapstruct屬性映射的實(shí)現(xiàn)

    本文主要介紹了spring中Mapstruct屬性映射的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • mybatisplus如何解決分頁(yè)最多500條數(shù)據(jù)

    mybatisplus如何解決分頁(yè)最多500條數(shù)據(jù)

    這篇文章主要介紹了mybatisplus如何解決分頁(yè)最多500條數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • zookeeper的watch機(jī)制原理解析

    zookeeper的watch機(jī)制原理解析

    Watcher,異步通知客戶(hù)端,并且刪除哈希表中對(duì)應(yīng)的 Key-Value,這篇文章主要介紹了zookeeper的watch機(jī)制詳細(xì)講解,需要的朋友可以參考下
    2022-06-06
  • 使用Spring事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的步驟詳解

    使用Spring事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的步驟詳解

    Spring 事件監(jiān)聽(tīng)機(jī)制是 Spring 框架中用于在應(yīng)用程序的不同組件之間進(jìn)行通信的一種機(jī)制,Spring 事件監(jiān)聽(tīng)機(jī)制基于觀察者設(shè)計(jì)模式,使得應(yīng)用程序的各個(gè)部分可以解耦,提高模塊化和可維護(hù)性,本文給大家介紹了使用Spring事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用,需要的朋友可以參考下
    2024-06-06
  • RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決

    RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決

    這篇文章主要介紹了RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解利用SpringMVC攔截器控制Controller返回值

    詳解利用SpringMVC攔截器控制Controller返回值

    這篇文章主要介紹了詳解利用SpringMVC攔截器控制Controller返回值,通過(guò)定義一個(gè)StringResult注解,在訪問(wèn)方法的時(shí)候返回StringResult中的內(nèi)容,有興趣的可以了解一下。
    2017-01-01

最新評(píng)論

若尔盖县| 临桂县| 昌黎县| 南召县| 中方县| 惠东县| 怀集县| 福鼎市| 临湘市| 南汇区| 庆元县| 嵊泗县| 新泰市| 慈利县| 铜山县| 南川市| 化州市| 瑞金市| 郸城县| 融水| 武定县| 泌阳县| 蒙自县| 岱山县| 旬邑县| 定南县| 乐昌市| 嘉黎县| 东海县| 崇阳县| 中江县| 清原| 巴林左旗| 泌阳县| 祁阳县| 东兰县| 澄迈县| 大方县| 红河县| 裕民县| 尖扎县|