Skip to content

Commit 2da1412

Browse files
authored
Merge pull request #174 from yvasyliev/dev
Fixed ts duplication when fetching updates
2 parents eaeea35 + e7edd5d commit 2da1412

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.github.yvasyliev</groupId>
77
<artifactId>java-vk-bots-longpoll-api</artifactId>
88
<packaging>jar</packaging>
9-
<version>4.1.3</version>
9+
<version>4.1.4</version>
1010
<name>Java VK Bots Long Poll API</name>
1111
<description>A Java library to create VK bots using Bots Long Poll API</description>
1212
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>

src/main/java/api/longpoll/bots/LongPollBot.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ public abstract class LongPollBot extends VkBot {
2020
private static final long DEFAULT_SESSION_DURATION = 9;
2121

2222
/**
23-
* Gets VK updates.
23+
* Server URL.
2424
*/
25-
private GetUpdates getUpdates;
25+
private String server;
26+
27+
/**
28+
* Session key.
29+
*/
30+
private String key;
31+
32+
/**
33+
* Latest received event.
34+
*/
35+
private int ts;
2636

2737
/**
2838
* Whether infinite loop should be continued.
@@ -52,8 +62,8 @@ public void startPolling() throws VkApiException {
5262
if (isSessionExpired()) {
5363
initialize();
5464
}
55-
GetUpdates.ResponseBody updates = getUpdates.execute();
56-
getUpdates.setTs(updates.getTs());
65+
GetUpdates.ResponseBody updates = getUpdates().execute();
66+
ts = updates.getTs();
5767
handle(updates.getEvents());
5868
} catch (VkResponseException e) {
5969
if (!e.getMessage().contains("failed")) {
@@ -85,9 +95,9 @@ public void initialize() throws VkApiException {
8595
.getResponse()
8696
.getAsJsonObject();
8797

88-
getUpdates = new GetUpdates(longPollServer.get("server").getAsString())
89-
.setKey(longPollServer.get("key").getAsString())
90-
.setTs(longPollServer.get("ts").getAsInt());
98+
server = longPollServer.get("server").getAsString();
99+
key = longPollServer.get("key").getAsString();
100+
ts = longPollServer.get("ts").getAsInt();
91101
}
92102

93103
/**
@@ -107,4 +117,15 @@ public void setSessionDuration(long sessionDuration) {
107117
private boolean isSessionExpired() {
108118
return ChronoUnit.HOURS.between(initializedAt, LocalDateTime.now()) >= sessionDuration;
109119
}
120+
121+
/**
122+
* Produces new {@link GetUpdates} instance based on {@link LongPollBot#server}, {@link LongPollBot#key} and {@link LongPollBot#ts}.
123+
*
124+
* @return {@link GetUpdates} instance.
125+
*/
126+
private GetUpdates getUpdates() {
127+
return new GetUpdates(server)
128+
.setKey(key)
129+
.setTs(ts);
130+
}
110131
}

src/main/java/api/longpoll/bots/http/LoggerInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private String getQueryParams(RequestBody requestBody) {
7676
if (size > 0) {
7777
return "?" + IntStream.range(0, size)
7878
.mapToObj(i -> formBody.encodedName(i) + "=" + formBody.encodedValue(i))
79-
.collect(Collectors.joining());
79+
.collect(Collectors.joining("&"));
8080
}
8181
}
8282
return "";

0 commit comments

Comments
 (0)