Skip to content

Commit

Permalink
Merge pull request #174 from yvasyliev/dev
Browse files Browse the repository at this point in the history
Fixed ts duplication when fetching updates
  • Loading branch information
yvasyliev authored Sep 19, 2023
2 parents eaeea35 + e7edd5d commit 2da1412
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
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>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<packaging>jar</packaging>
<version>4.1.3</version>
<version>4.1.4</version>
<name>Java VK Bots Long Poll API</name>
<description>A Java library to create VK bots using Bots Long Poll API</description>
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/api/longpoll/bots/LongPollBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ public abstract class LongPollBot extends VkBot {
private static final long DEFAULT_SESSION_DURATION = 9;

/**
* Gets VK updates.
* Server URL.
*/
private GetUpdates getUpdates;
private String server;

/**
* Session key.
*/
private String key;

/**
* Latest received event.
*/
private int ts;

/**
* Whether infinite loop should be continued.
Expand Down Expand Up @@ -52,8 +62,8 @@ public void startPolling() throws VkApiException {
if (isSessionExpired()) {
initialize();
}
GetUpdates.ResponseBody updates = getUpdates.execute();
getUpdates.setTs(updates.getTs());
GetUpdates.ResponseBody updates = getUpdates().execute();
ts = updates.getTs();
handle(updates.getEvents());
} catch (VkResponseException e) {
if (!e.getMessage().contains("failed")) {
Expand Down Expand Up @@ -85,9 +95,9 @@ public void initialize() throws VkApiException {
.getResponse()
.getAsJsonObject();

getUpdates = new GetUpdates(longPollServer.get("server").getAsString())
.setKey(longPollServer.get("key").getAsString())
.setTs(longPollServer.get("ts").getAsInt());
server = longPollServer.get("server").getAsString();
key = longPollServer.get("key").getAsString();
ts = longPollServer.get("ts").getAsInt();
}

/**
Expand All @@ -107,4 +117,15 @@ public void setSessionDuration(long sessionDuration) {
private boolean isSessionExpired() {
return ChronoUnit.HOURS.between(initializedAt, LocalDateTime.now()) >= sessionDuration;
}

/**
* Produces new {@link GetUpdates} instance based on {@link LongPollBot#server}, {@link LongPollBot#key} and {@link LongPollBot#ts}.
*
* @return {@link GetUpdates} instance.
*/
private GetUpdates getUpdates() {
return new GetUpdates(server)
.setKey(key)
.setTs(ts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private String getQueryParams(RequestBody requestBody) {
if (size > 0) {
return "?" + IntStream.range(0, size)
.mapToObj(i -> formBody.encodedName(i) + "=" + formBody.encodedValue(i))
.collect(Collectors.joining());
.collect(Collectors.joining("&"));
}
}
return "";
Expand Down

0 comments on commit 2da1412

Please sign in to comment.