1.调用青云客的API进行自动聊天
2.输入关键词自动添加为好友
非特殊情况保持一致即可!
1.SpringBoot-3.0.5
2.JDK-17
3.go-cqhttp1.0
Github:GitHub - Mrs4s/go-cqhttp: cqhttp的golang实现,轻量、原生跨平台.
gocq api文档地址:https://docs.go-cqhttp.org/api/
https://gitee.com/mumangguo/go-cqhttp
org.springframework.boot
spring-boot-starter-websocket
com.alibaba
fastjson
2.0.21
org.projectlombok
lombok
org.apache.httpcomponents
httpclient
4.5.13
Friend类
import lombok.Data;
@Data
public class Friend {
private String user_id;
private String comment;
private String flag;
}
Message类
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
@Data
public class Message implements Serializable {
@JSONField(name = "post_type")
private String postType;
@JSONField(name = "meta_event_type")
private String metaEventType;
@JSONField(name = "message_type")
private String messageType;
@JSONField(name = "notice_type")
private String noticeType;
// 操作人id 比如群管理员a踢了一个人,那么该值为a的qq号
@JSONField(name = "operator_id")
private String operatorId;
private Long time;
@JSONField(name = "self_id")
private String selfId;
@JSONField(name = "sub_type")
private String subType;
@JSONField(name = "user_id")
private String userId;
@JSONField(name = "sender_id")
private String senderId;
@JSONField(name = "group_id")
private String groupId;
@JSONField(name = "target_id")
private String targetId;
private String message;
@JSONField(name = "raw_message")
private String rawMessage;
private Integer font;
//private Sender sender;
@JSONField(name = "message_id")
private String messageId;
@JSONField(name = "message_seq")
private Integer messageSeq;
private String anonymous;
}
Request类
import lombok.Data;
@Data
public class Request {
private String action;
private T params;
private String echo;
}
/**
* 消息监听
*/
@ClientEndpoint
public class Client {
private static final Logger logger = LoggerFactory.getLogger(Client.class);
private Session session;
private static Client instance;
private Client(String url) {
try {
session = ContainerProvider.getWebSocketContainer().connectToServer(this, URI.create(url));
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static boolean connect(String url) {
instance = new Client(url);
return true;
}
@OnOpen
public void onOpen(Session session) {
logger.info("连接成功!");
}
@OnClose
public void onClose(Session session) {
logger.info("连接关闭!");
}
@OnError
public void onError(Session session, Throwable throwable) {
logger.info("连接错误!");
}
@OnMessage
public void onMessage(String message) {
if (message.contains("\"request_type\":\"friend\"")) {
sendFriend(message);
}
if (message.contains("\"post_type\":\"message\"") && message.contains("\"message_type\":\"private\"")) {
sendMsg(message);
}
}
/**
* 好友请求
*/
private synchronized void sendFriend(String msg) {
Friend parseObject = JSONObject.parseObject(msg, Friend.class);
logger.info("收到好友请求:" + parseObject.getUser_id()+",验证消息:"+parseObject.getComment());
Request
下载链接:Releases · Mrs4s/go-cqhttp · GitHub,根据自身实际情况下载对应的操作系统版本,当然我的源码中包含了Linux版本和Windows版本
官方推荐使用cmd的方式启动,我们就在文件目录上输入cmd启动即可
可以看到他在当前文件夹下生成了一个config.yml文件,我们打开看看,只要修改我箭头标注位置即可
修改完后,重新启动该程序,完成qq的登录验证,正常完成验证的情况下,就会出现登录成功!
登录出现45/235错误代表着当前QQ号处于腾讯的风控,就需要修改协议(不用配置密码,把密码保持为空即可)采用扫码的方式进行登录!
修改刚刚生成的device.json
把这个protocol修改为2或者0,保存后再继续启动,出现扫码登录即可
import com.mmg.gocqhttp.websocket.Client;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* https://docs.go-cqhttp.org/guide/quick_start.html
*/
@SpringBootApplication
public class GoCqhttpApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(GoCqhttpApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
//这里的地址是根据config.yml里面配置的IP和端口号保持一致
Client.connect("ws://127.0.0.1:9090");
}
}