# autoQQ
**Repository Path**: airgwl/auto-qq
## Basic Information
- **Project Name**: autoQQ
- **Description**: 使用go-cqhttp创建qq机器人,可以定时发送消息,自动回复消息
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-01-15
- **Last Updated**: 2024-01-15
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# QQ机器人
# 环境搭建
### 机器人框架的下载和配置
[go-cqhttp]: https://docs.go-cqhttp.org/ "go-cqhttp"
下载后解压得到三个文件
**双击exe文件**,出现如下窗口,**一直点击确定**就行了(3次)
输入0后回车,然后关闭,此时已经在原来的文件目录下生成了配置文件config.yml。
可以不输入账号密码,直接运行那个bat文件会出现二维码扫码登录,这样更加安全。
再转到最后两行,把前面的#去掉使其生效。
再次双击bat文件扫码登陆。
如果出现`当前协议不支持二维码登录, 请配置账号密码登录`
- 修改device.json,将protocol修改成2(意思是修改连接协议,2是使用手表登录,6是电脑登录)
扫码登录后终端应该打印两个日志信息
这个警告可以不用理会,是正常现象
# 发送消息
发送消息的代码演示
不限制语言,这里使用java展示
**这包括发送私聊信息和发送群聊信息。下面的函数已经写好了,直接调用函数传入参数就行了。**
```java
package com.gwl.autoqq;
import com.gwl.autoqq.pojo.RespDict;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
@SpringBootTest
class AutoQqApplicationTests {
@Test
void contextLoads() {
RespDict respDict = new RespDict("private", "2423414358", "你好我是机器人");
sendMsg(respDict);
System.out.println("123");
}
public void sendMsg(RespDict respDict) {
try {
Socket client = new Socket(ip, 5700);
String msgType = respDict.getMsgType(); // 回复类型(群聊/私聊)
String number = respDict.getNumber(); // 回复账号(群号/好友号)
String msg = respDict.getMsg(); // 要回复的消息
// 将字符中的特殊字符进行URL编码
String encodedMsg = msg.replace(" ", "%20").replace("\n", "%0a");
String payload;
if (msgType.equals("group")) {
payload = "GET /send_group_msg?group_id=" + number + "&message=" + encodedMsg + " HTTP/1.1\r\nHost: 127.0.0.1:5700\r\nConnection: close\r\n\r\n";
} else if (msgType.equals("private")) {
payload = "GET /send_private_msg?user_id=" + number + "&message=" + encodedMsg + " HTTP/1.1\r\nHost: 127.0.0.1:5700\r\nConnection: close\r\n\r\n";
} else {
throw new IllegalArgumentException("Invalid message type");
}
System.out.println("发送" + payload);
OutputStream output = client.getOutputStream();
output.write(payload.getBytes(StandardCharsets.UTF_8));
output.flush();
output.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
# 接收信息
接收消息的代码演示
不限制语言,这里使用java展示
```java
public class ReceiveMessage {
private static ObjectMapper objectMapper = new ObjectMapper();
public static String svTime(String timestamp){
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
// 将时间戳转换为 Date 对象
Date date = new Date(Long.parseLong(timestamp));
// 使用 SimpleDateFormat 格式化 Date 对象
return sdf.format(date);
}
public static void main(String[] args) {
ServerSocket listenSocket = null;
try {
listenSocket = new ServerSocket();
SocketAddress socketAddress = new InetSocketAddress(ip, 5701);
listenSocket.bind(socketAddress);
while (true){
Socket clientSocket = listenSocket.accept();
// System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream();
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
String request = new String(buffer, 0, length, "UTF-8");
// 过滤请求
Pattern pattern = Pattern.compile("\\{.*\\}");
Matcher matcher = pattern.matcher(request);
JSONObject response = null;
if (matcher.find()) {
String json = matcher.group(0);
response = JSON.parseObject(json);
}
// 收到信息事件
if(response.getString("post_type").equals("message")){
String message_type = response.getString("message_type");
String time = response.getString("time");
String message_id = response.getString("message_id");
String message = response.getString("message");
String sender_user_id = response.getJSONObject("sender").getString("user_id");
String sender_nickname = response.getJSONObject("sender").getString("nickname");
System.out.println("信息类型是: "+ message_type);
System.out.println( "收到的信息是: " + message);
System.out.println( "发送的用户id是: " + sender_user_id);
System.out.println( "发送给的用户id是: " + sender_nickname);
System.out.println( "时间是: " + svTime(time));
System.out.println( "消息id是: " + message_id);
System.out.println( "========================================");
System.out.println();
System.out.println();
}
// System.out.println("Request: " + request);
clientSocket.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
```