go-cqhttp 机器人使用教程

API | go-cqhttp 帮助中心

参考 | go-cqhttp 帮助中心

机器人下载

发送消息

http://127.0.0.1:5700/send_msg?message_type=private&user_id=911412667&message=你好呀

检查端口是否打开

netstat -ano | findstr :5701

发送的请求

软件的dopost的解析

java 复制代码
  @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        System.out.println("你好呀我获取了数据");
        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/json; charset=utf-8"); //设置编码格式和数据类型
        response.setHeader("Content-Type", "application/json;charset=UTF-8");


        // 获取请求体中的数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),StandardCharsets.UTF_8)); //第一种方式
//        String requestBody = IOUtils.toString(request.getInputStream(), String.valueOf(StandardCharsets.UTF_8));//第二种方式
//        System.out.println("错误乱码检查:"+requestBody);


        String jsonStr = "";
        String line = null;
        while ((line = reader.readLine()) != null) {
            jsonStr += line;
        }
        System.out.println(jsonStr);

//        // 解析JSON格式的数据
//        Gson gson = new Gson();
//        Message message = gson.fromJson(jsonStr, Message.class);
//
//        // 输出解析结果
//        System.out.println(message.getMessage());


    }

需要的jar包

commons的

java 复制代码
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

json

java 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>

通过WebSocket通信

java 复制代码
package WebSocket.Server;

import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import org.glassfish.tyrus.client.ClientManager;

@ClientEndpoint
public class WebsocketClient {
    public static void main(String[] args) throws Exception {
        String serverEndpoint = "ws://127.0.0.1:8081"; // 这里设置 Websocket 服务器的地址

        ClientManager client = ClientManager.createClient();
        WebsocketClient socket = new WebsocketClient();
        client.connectToServer(socket, new URI(serverEndpoint));

        // 等待接收服务器发来的消息
        while (true) {
            Thread.sleep(1000);
        }
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("Received message from server: " + message);
    }

    @OnError
    public void onError(Throwable t) {
        t.printStackTrace(System.err);
    }
}

所有的jar包

XML 复制代码
<dependencies>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.15.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.76</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-booter</artifactId>
        <version>2.22.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-client-api</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus.bundles</groupId>
        <artifactId>tyrus-standalone-client</artifactId>
        <version>1.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>1.7.36</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
相关推荐
mqiqe14 分钟前
架构-亿级流量性能调优实践
java·架构
野犬寒鸦1 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
七夜zippoe1 小时前
AI+Java 守护你的钱袋子!金融领域的智能风控与极速交易
java·人工智能·金融
岁忧1 小时前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先
liliangcsdn2 小时前
结合prompt分析NodeRAG的build过程
java·服务器·人工智能·数据分析·知识图谱
黑色的山岗在沉睡2 小时前
LeetCode 189. 轮转数组
java·算法·leetcode
会飞的小蛮猪2 小时前
Jenkins运维之路(权限分配&忘记admin密码)
java·运维·经验分享·jenkins·prometheus
slim~3 小时前
Java基础第9天总结(可变参数、Collections、斗地主)
java·开发语言
豆沙沙包?3 小时前
2025年- H118-Lc86. 分隔链表(链表)--Java版
java·数据结构·链表