import nano.http.d2.json.NanoJSON;
import nano.http.d2.utils.Request;
import nano.http.d2.utils.WebSocketClient;
import java.util.Properties;
@SuppressWarnings({"BusyWait"})
public class Main {
public static void main(String[] args) throws Exception {
final String appid = "******";
final String secret = "******";
NanoJSON nj = new NanoJSON();
nj.put("appId", appid);
nj.put("clientSecret", secret);
NanoJSON resp = new NanoJSON(Request.jsonPost("https://bots.qq.com/app/getAppAccessToken", nj.toString(), null));
String accessToken = resp.getString("access_token");
System.out.println("[信息] 令牌获取成功,为" + accessToken + "。(不要截图!如果需要截图,从下面一行开始!)");
Properties headers = new Properties();
headers.setProperty("Authorization", "QQBot " + accessToken);
headers.setProperty("X-Union-Appid", appid);
NanoJSON wssEp = new NanoJSON(Request.get("https://api.sgroup.qq.com/gateway", headers));
WebSocketClient wsc = new WebSocketClient(wssEp.getString("url"));
NanoJSON heartbeat = new NanoJSON(wsc.read());
long interval = heartbeat.getJSONObject("d").getLong("heartbeat_interval");
System.out.println("[信息] 心跳建立成功(" + interval + "ms)。");
int[] ack = {-1};
new Thread(() -> {
while (true) {
try {
Thread.sleep(interval);
wsc.send("{"op": 3,"d": " + (ack[0] == -1 ? "null" : ack[0]) + "}");
} catch (Exception e) {
break;
}
}
}).start();
wsc.send("{"op": 2,"d": {"token": "QQBot " + accessToken + "","intents": 33554432,"shard": [0, 1],"properties": {"lib":"BukkitHTTP"}}}");
new Thread(() -> {
while (true) {
if (!wsc.isClosed()) {
String msg = wsc.read();
NanoJSON nj2 = new NanoJSON(msg);
ack[0] = nj2.getInt("s");
int op = nj2.getInt("op"); // 0-事件推送
String type = nj2.getString("t"); // 事件类型
NanoJSON d = nj2.getJSONObject("d"); // 事件数据
if (op == 0) // 只处理事件推送
{
if (type.equals("READY")) {
System.out.println("[信息] 机器人已经上线。");
} else if (type.equals("GROUP_AT_MESSAGE_CREATE")) {
String group_openid = d.getString("group_openid"); // 群号,构建endpoint用
String member_openid = d.getJSONObject("author").getString("member_openid"); // 发送者QQ号,填数据库用
String message = d.getString("content"); // 消息内容
String id = d.getString("id"); // 回复避免扣主动消息次数用。
System.out.println("[信息] 收到群消息:" + message + ",来自" + member_openid);
String endpoint = "https://api.sgroup.qq.com/v2/groups/" + group_openid + "/messages";
NanoJSON reply = new NanoJSON();
reply.put("content", "你说的是 > " + message + " ! [BukkitHTTP QBot Lib]");
reply.put("msg_id", id);
reply.put("msg_type", 0);
try {
NanoJSON send = new NanoJSON(Request.jsonPost(endpoint, reply.toString(), headers));
if (send.has("message")) {
System.out.println("[错误] 消息发送失败!" + send.getString("message"));
}
} catch (Exception ex) {
System.out.println("[错误] 消息发送异常!" + ex.getMessage());
}
}
}
} else {
System.exit(0);
}
}
}).start();
}
}
复制代码接入申请 https://q.qq.com/
用到的库 https://github.com/BukkitHTTP/BukkitHTTP
import nano.http.d2.utils.Request;
import nano.http.d2.utils.WebSocketClient;
import java.util.Properties;
@SuppressWarnings({"BusyWait"})
public class Main {
public static void main(String[] args) throws Exception {
final String appid = "******";
final String secret = "******";
NanoJSON nj = new NanoJSON();
nj.put("appId", appid);
nj.put("clientSecret", secret);
NanoJSON resp = new NanoJSON(Request.jsonPost("https://bots.qq.com/app/getAppAccessToken", nj.toString(), null));
String accessToken = resp.getString("access_token");
System.out.println("[信息] 令牌获取成功,为" + accessToken + "。(不要截图!如果需要截图,从下面一行开始!)");
Properties headers = new Properties();
headers.setProperty("Authorization", "QQBot " + accessToken);
headers.setProperty("X-Union-Appid", appid);
NanoJSON wssEp = new NanoJSON(Request.get("https://api.sgroup.qq.com/gateway", headers));
WebSocketClient wsc = new WebSocketClient(wssEp.getString("url"));
NanoJSON heartbeat = new NanoJSON(wsc.read());
long interval = heartbeat.getJSONObject("d").getLong("heartbeat_interval");
System.out.println("[信息] 心跳建立成功(" + interval + "ms)。");
int[] ack = {-1};
new Thread(() -> {
while (true) {
try {
Thread.sleep(interval);
wsc.send("{"op": 3,"d": " + (ack[0] == -1 ? "null" : ack[0]) + "}");
} catch (Exception e) {
break;
}
}
}).start();
wsc.send("{"op": 2,"d": {"token": "QQBot " + accessToken + "","intents": 33554432,"shard": [0, 1],"properties": {"lib":"BukkitHTTP"}}}");
new Thread(() -> {
while (true) {
if (!wsc.isClosed()) {
String msg = wsc.read();
NanoJSON nj2 = new NanoJSON(msg);
ack[0] = nj2.getInt("s");
int op = nj2.getInt("op"); // 0-事件推送
String type = nj2.getString("t"); // 事件类型
NanoJSON d = nj2.getJSONObject("d"); // 事件数据
if (op == 0) // 只处理事件推送
{
if (type.equals("READY")) {
System.out.println("[信息] 机器人已经上线。");
} else if (type.equals("GROUP_AT_MESSAGE_CREATE")) {
String group_openid = d.getString("group_openid"); // 群号,构建endpoint用
String member_openid = d.getJSONObject("author").getString("member_openid"); // 发送者QQ号,填数据库用
String message = d.getString("content"); // 消息内容
String id = d.getString("id"); // 回复避免扣主动消息次数用。
System.out.println("[信息] 收到群消息:" + message + ",来自" + member_openid);
String endpoint = "https://api.sgroup.qq.com/v2/groups/" + group_openid + "/messages";
NanoJSON reply = new NanoJSON();
reply.put("content", "你说的是 > " + message + " ! [BukkitHTTP QBot Lib]");
reply.put("msg_id", id);
reply.put("msg_type", 0);
try {
NanoJSON send = new NanoJSON(Request.jsonPost(endpoint, reply.toString(), headers));
if (send.has("message")) {
System.out.println("[错误] 消息发送失败!" + send.getString("message"));
}
} catch (Exception ex) {
System.out.println("[错误] 消息发送异常!" + ex.getMessage());
}
}
}
} else {
System.exit(0);
}
}
}).start();
}
}
复制代码接入申请 https://q.qq.com/
用到的库 https://github.com/BukkitHTTP/BukkitHTTP