Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.zcraft</groupId>
<artifactId>Seira</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>

<properties>
<maven.compiler.source>25</maven.compiler.source>
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/xyz/zcraft/seira/bot/QQApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,24 @@ public static String getWSSEndpoint(AccessToken accessToken) {
}
}

public static AccessToken getAccessToken(String appId, String appSecret) {
try {
JsonObject payload = new JsonObject();
payload.addProperty("appId", appId);
payload.addProperty("clientSecret", appSecret);
public static AccessToken getAccessToken(String appId, String appSecret) throws Exception {
JsonObject payload = new JsonObject();
payload.addProperty("appId", appId);
payload.addProperty("clientSecret", appSecret);

HttpRequest request = HttpRequest.newBuilder()
.header("Content-Type", "application/json")
.uri(URI.create("https://bots.qq.com/app/getAppAccessToken"))
.POST(HttpRequest.BodyPublishers.ofString(payload.toString()))
.build();
HttpRequest request = HttpRequest.newBuilder()
.header("Content-Type", "application/json")
.uri(URI.create("https://bots.qq.com/app/getAppAccessToken"))
.POST(HttpRequest.BodyPublishers.ofString(payload.toString()))
.build();

final JsonElement jsonElement = JsonParser.parseString(CLIENT.send(request, HttpResponse.BodyHandlers.ofString()).body());
final JsonElement jsonElement = JsonParser.parseString(CLIENT.send(request, HttpResponse.BodyHandlers.ofString()).body());

return new AccessToken(
jsonElement.getAsJsonObject().get("access_token").getAsString(),
System.currentTimeMillis(),
jsonElement.getAsJsonObject().get("expires_in").getAsLong()
);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
return new AccessToken(
jsonElement.getAsJsonObject().get("access_token").getAsString(),
System.currentTimeMillis(),
jsonElement.getAsJsonObject().get("expires_in").getAsLong()
);
}

public static void sendPrivateMessage(AccessToken accessToken, String openId, Message message) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/zcraft/seira/bot/QQBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public QQBot(AppConfig config) {

while (true) {
try {
if (tokenManager.isExpired()) {
LOG.warn("Waiting for token renewal");
continue;
}

LOG.info("Getting wss endpoint");
String wssEndpoint = QQApi.getWSSEndpoint(tokenManager.getToken());
LOG.info("Endpoint: {}", wssEndpoint);
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/xyz/zcraft/seira/util/TokenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ public TokenManager(String clientId, String clientSecret) {
}

public boolean isExpired() {
return System.currentTimeMillis() - token.tokenGrantTime() >= (token.expiresIn() - 60) * 1000;
return (token == null) || (System.currentTimeMillis() - token.tokenGrantTime() >= (token.expiresIn() - 60) * 1000);
}

public void renew() {
token = QQApi.getAccessToken(clientId, clientSecret);
LOG.info("Token renewed, expire in {}", token.expiresIn());
try {
token = QQApi.getAccessToken(clientId, clientSecret);
LOG.info("Token renewed, expire in {}", token.expiresIn());
} catch (Exception e) {
token = null;
LOG.error("Failed to renew token, will retry in 60 sec", e);
}

timer.schedule(new java.util.TimerTask() {
@Override
Expand All @@ -38,6 +43,6 @@ public void run() {
renew();
}
}
}, Math.min(token.expiresIn() - 60, 60) * 1000L);
}, Math.min((token != null ? token.expiresIn() : 0) - 60, 60) * 1000L);
}
}
Loading