1 Star 0 Fork 15

oceanyang / coolq

forked from wind / coolq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Spring-CQ

基于 酷Q、cqhttp、SpringBoot、反向websocket 的 QQ 机器人框架

使用了开源jar https://github.com/lz1998/Spring-CQ

编写插件

  1. 编写XXXPlugin,继承CQPlugin

    /**
    * 示例插件
    * 插件必须继承CQPlugin,上面要 @Component
    *
    * 添加事件:光标移动到类中,按 Ctrl+O 添加事件(讨论组消息、加群请求、加好友请求等)
    * 查看API参数类型:光标移动到方法括号中按Ctrl+P
    * 查看API说明:光标移动到方法括号中按Ctrl+Q
    */
    @Component
    public class DemoPlugin extends CQPlugin {
       /**
        * 收到私聊消息时会调用这个方法
        *
        * @param cq    机器人对象,用于调用API,例如发送私聊消息 sendPrivateMsg
        * @param event 事件对象,用于获取消息内容、群号、发送者QQ等
        * @return 是否继续调用下一个插件,IGNORE表示继续,BLOCK表示不继续
        */
       @Override
       public int onPrivateMessage(CoolQ cq, CQPrivateMessageEvent event) {
           // 获取 发送者QQ 和 消息内容
           long userId = event.getUserId();
           String msg = event.getMessage();
    
           if (msg.equals("hi")) {
               // 调用API发送hello
               cq.sendPrivateMsg(userId, "hello", false);
    
               // 不执行下一个插件
               return MESSAGE_BLOCK;
           }
           // 继续执行下一个插件
           return MESSAGE_IGNORE;
       }
    
    
       /**
        * 收到群消息时会调用这个方法
        *
        * @param cq    机器人对象,用于调用API,例如发送群消息 sendGroupMsg
        * @param event 事件对象,用于获取消息内容、群号、发送者QQ等
        * @return 是否继续调用下一个插件,IGNORE表示继续,BLOCK表示不继续
        */
       @Override
       public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
           // 获取 消息内容 群号 发送者QQ
           String msg = event.getMessage();
           long groupId = event.getGroupId();
           long userId = event.getUserId();
    
           if (msg.equals("hello")) {
               // 回复内容为 at发送者 + hi
               String result = CQCode.at(userId) + "hi";
    
               // 调用API发送消息
               cq.sendGroupMsg(groupId, result, false);
    
               // 不执行下一个插件
               return MESSAGE_BLOCK;
           }
    
           // 继续执行下一个插件
           return MESSAGE_IGNORE;
       }
    }

    截图

  2. 创建一个CQConfig类

    @EnableCQ
    public class Config {
        public static void init(){
            CQGlobal.pluginList.add(DemoPlugin.class);
            // CQGlobal.pluginList.add(XXXXXXXX); // 如果还有别的功能,在这里add,模仿上面一行
        }
    }
  3. 在主函数中,Spring启动前执行CQConfig.init()

    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

测试应用

  1. 修改application.properties的端口,默认8081
  2. 运行SpringCqApplication的main方法

打包应用

  1. 使用maven打包应用
    mvn clean package
  2. 在target目录下,spring-cq-0.0.1-SNAPSHOT.jar即为打包的jar

运行应用

  1. 输入指令
    java -jar spring-cq-0.0.1-SNAPSHOT.jar

如果是Windows,并且不需要查看运行情况,可以直接双击jar文件运行,右下角托盘会出现小图标

Windows运行酷Q和cqhttp

  1. 准备酷Q Air
    • 方案一:下载已经配置好cqhttp的酷Q Air
    • 方案二:自己配置
      1. 下载酷Q Air
      2. 下载CQHTTP插件
      3. 创建文件酷Q Air\data\app\io.github.richardchien.coolqhttpapi\config.ini
        [general]
        use_http=false
        use_ws_reverse=true
        ws_reverse_url=ws://127.0.0.1:8081/ws/cq/
        ws_reverse_use_universal_client=true
        enable_heartbeat=true
        heartbeat_interval=60000
  2. 解压后运行 CQA.exe 登录QQ账号

Docker运行酷Q和cqhttp

  1. 安装酷Q和CQHTTP插件
    docker run -d --name cq01 \
    -v $(pwd)/coolq:/home/user/coolq \
    -p 9000:9000 \
    -e VNC_PASSWD=你的VNC密码(不超过8位) \
    -e COOLQ_URL=http://dlsec.cqp.me/cqa-tuling \
    -e COOLQ_ACCOUNT=你的机器人QQ号 \
    -e CQHTTP_USE_HTTP=false \
    -e CQHTTP_USE_WS_REVERSE=true \
    -e CQHTTP_WS_REVERSE_URL=ws://宿主机地址:8081/ws/cq/ \
    -e CQHTTP_WS_REVERSE_UNIVERSAL_CLIENT=true \
    -e CQHTTP_ENABLE_HEARTBEAT=true \
    -e CQHTTP_HEARTBEAT_INTERVAL=60000 \
    richardchien/cqhttp
    如果不知道宿主机地址是什么,可以使用Docker的host模式,共享主机网络
    docker run -d --name cq01 \
    -v $(pwd)/coolq:/home/user/coolq \
    --net=host \
    -e VNC_PASSWD=你的VNC密码(不超过8位) \
    -e COOLQ_URL=http://dlsec.cqp.me/cqa-tuling \
    -e COOLQ_ACCOUNT=你的机器人QQ号 \
    -e CQHTTP_USE_HTTP=false \
    -e CQHTTP_USE_WS_REVERSE=true \
    -e CQHTTP_WS_REVERSE_URL=ws://127.0.0.1:8081/ws/cq/ \
    -e CQHTTP_WS_REVERSE_UNIVERSAL_CLIENT=true \
    -e CQHTTP_ENABLE_HEARTBEAT=true \
    -e CQHTTP_HEARTBEAT_INTERVAL=60000 \
    richardchien/cqhttp
  2. 访问 http://127.0.0.1:9000 登录QQ账号
MIT License Copyright (c) 2020 wind Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

酷Q机器人服务端 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/marineosc/coolq.git
git@gitee.com:marineosc/coolq.git
marineosc
coolq
coolq
master

搜索帮助