java实现QQ机器人

java实现QQ机器人

使用Java拦截QQ消息 + 回复消息

请确保coolq环境搭建完毕 部署coolq环境教程

安装coolq插件

解压文件夹,将 org.ruiko.lemoc.cpk 拷贝到linux服务器上coolq目录下的app目录。我的如下

[root@iz2ze6ezkrxf8bsldl6cn3z app]# cd /data/coolq-data/app/
[root@iz2ze6ezkrxf8bsldl6cn3z app]# ll
total 1560
-rw-r--r-- 1 admin admin 345637 Aug 11 10:43 com.coxxs.music.cpk
-rw-r--r-- 1 admin admin 378541 Aug 11 10:43 com.coxxs.start.cpk
-rw-r--r-- 1 admin admin  17797 Aug 11 10:43 com.coxxs.status.cpk
-rw-r--r-- 1 admin admin 401717 Aug 11 10:43 com.coxxs.tuling123.cpk
-rw-r--r-- 1 root  root  440445 Jan 27  2017 org.ruiko.lemoc.cpk
[root@iz2ze6ezkrxf8bsldl6cn3z app]# 

登录coolq的控制台

  • 右键悬浮窗,启动lemoc模块

  • 设置 菜单>设置>自动开启serverg勾选,本机连接不勾选

开启25303的外网端口访问

  • 删除之前运行的docker容器,启动时,新增一个映射端口(lemoc模块的25303端口)
docker rm $(docker ps -aq)
docker run --name=coolq -d -p 8001:9000 -p 25303:25303 -v /data/coolq-data:/home/user/coolq -e VNC_PASSWD=******* -e COOLQ_ACCOUNT=630389368 coolq/wine-coolq

验证是否开启成功

windows下cmd:telnet ip 25303

模块已经安装完毕,接下来开始编写Java代码拦截QQ消息

  • 导入下载的jar

  • KQClient.java
import org.apache.log4j.Logger;

import java.net.URI;

public class KQClient {

    private static Logger logger = Logger.getLogger(KQClient.class);

    private static RewriteKQWebClient kqWebClient;

    public static void runClient(String host){
        try {
            if(kqWebClient == null){
                //连接coolq服务器
                kqWebClient = new RewriteKQWebClient(new URI(host));
            }
            //消息监听适配器
            MyQQAdapter myQQAdapter = new MyQQAdapter(kqWebClient);
            //监听消息
            kqWebClient.addQQMSGListenner(myQQAdapter);
        }catch (Exception e){
            System.err.println("init KQ client fail e:"+e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        KQClient.runClient("ws://39.106.87.218:25303");
    }
}
  • MyQQAdapter.java
import com.mumu.listenner.KQMSGAdapter;
import com.mumu.msg.RE_MSG_Forum;
import com.mumu.msg.RE_MSG_Group;
import com.mumu.msg.RE_MSG_Private;

public class MyQQAdapter extends KQMSGAdapter {

    private RewriteKQWebClient kqWebClient;


    MyQQAdapter(RewriteKQWebClient kqWebClient) {
        this.kqWebClient = kqWebClient;
    }

    /**
     * 接收私聊消息
     */
    public void Re_MSG_Private(RE_MSG_Private msg) {
        System.out.println("接收到私聊信息 from:"+msg.getFromqq()+" \t msg:"+msg.getMsg());
        kqWebClient.sendPrivateMSG(msg.getFromqq(),"你好,接收到了你的消息:" + msg.getMsg());
    }

    public void RE_MSG_FORUM(RE_MSG_Forum msg) {
        System.out.println("接收到消息 groupName:"+msg.getFromQQ() + "qq:"+msg.getFromQQ() + "msg:"+msg.getMsg());
    }

    /**
     * 接收群消息
     */
    public void RE_MSG_Group(RE_MSG_Group msg) {
        System.out.println("接收到群聊消息 groupName:"+msg.getFromGroupName() + "\t qq:"+msg.getFromQQ() + "\t msg:"+msg.getMsg());
    }
}
  • RewriteKQWebClient.java
import com.alibaba.fastjson.JSONObject;
import com.mumu.webclient.KQWebClient;

import java.net.URI;

public class RewriteKQWebClient extends KQWebClient {
    public RewriteKQWebClient(URI serverURI) {
        super(serverURI);
    }

    @Override
    public void sendPrivateMSG(String qq, String msg) {
        try {
            Thread.sleep(10L);
        } catch (InterruptedException var4) {
            var4.printStackTrace();
        }
        JSONObject sendMsg = new JSONObject();

        sendMsg.put("act",106);
        sendMsg.put("QQID",qq);
        sendMsg.put("msg",msg);
        super.send(sendMsg.toJSONString());
    }

    @Override
    public void sendGroupMSG(String qq, String groupid, String msg, Boolean isAT) {
        try {
            Thread.sleep(10L);
        } catch (InterruptedException var4) {
            var4.printStackTrace();
        }
        JSONObject sendMsg = new JSONObject();
        sendMsg.put("act",101);
        sendMsg.put("groupid",groupid);
        sendMsg.put("msg",msg);
        super.send(sendMsg.toJSONString());
    }
}

测试是否成功

    public static void main(String[] args) {
        KQClient.runClient("ws://ip:25303");
    }

输出如下日志

插件api地址,想要更多的接口说明,请查阅

https://cqp.cc/t/29722

大功告成,接下来,你可以完全做你想做的事情

# java  酷Q 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×